728x90
반응형
package gui;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import myobj.Prize;
public class S10_JComboBox {
public static void main(String[] args) {
JFrame f = new JFrame("ComboBox Example");
Prize[] prizes = new Prize[] {
new Prize("텀블러", 10000, 100, 0.1),
new Prize("안마의자", 2000000, 1, 0.01),
new Prize("손소독제", 5000, 500, 0.3),
};
// 제네릭 사용 가능
JComboBox<Prize> combo = new JComboBox<>(prizes);
combo.setBounds(50, 50, 100, 30);
combo.addItem(new Prize("제로콜라", 1000, 10, 0.2)); // 항목 추가
combo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == e.SELECTED) {
System.out.println("지금 선택된 것: " + e.getItem());
} else if (e.getStateChange() == e.DESELECTED) {
System.out.println("선택이 해제된 것: " + e.getItem());
}
}
});
f.add(combo);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 500, 500);
f.setVisible(true);
}
}
728x90
반응형
'JAVA > Swing' 카테고리의 다른 글
[JAVA] JPanel (0) | 2023.05.02 |
---|---|
[JAVA] GridLayout (0) | 2023.05.02 |
[JAVA] JRadioButton (0) | 2023.05.02 |
[JAVA] JCheckBox (0) | 2023.05.02 |
[JAVA] JPasswordField (0) | 2023.05.02 |