JComboBox - Java (241591)
Формулировка задачи:
Помогите разобраться с JCOMBOBOX. Есть два combobox. Когда пользователь выбирает объект, как поймать его выбор.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EShop {
private JTextField df;
public void setDisplayValue(String val){
df.setText(val);
}
public String getDisplayValue(){
return df.getText();
}
public static void main(String[] args) {
JPanel p1;
JButton button0;
JPanel esh = new JPanel();
JTextField dfield;
button0 = new JButton("Buy");
p1 = new JPanel();
String [] items1 = {
"Bike Arlenok",
"Bike Ukraine"
};
JComboBox comboBox = new JComboBox(items1);
Integer [] items2 = {
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
15,
20,
25,
};
JComboBox cb = new JComboBox(items2);
JLabel label1 = new JLabel("Enter bike");
JLabel label2 = new JLabel("how much");
JTextField field = new JTextField(10);
esh.add(label1);
esh.add(comboBox);
esh.add(label2);
esh.add(cb);
esh.add(button0);
esh.add(field);
p1 = new JPanel();
GridLayout gl = new GridLayout(10,30);
p1.setLayout(gl);
JFrame frame = new JFrame("E-Shop");
frame.setContentPane(esh);
frame.setVisible(true);
frame.setSize(250, 300);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
};
comboBox.addActionListener(actionListener);
cb.addActionListener(actionListener);
button0.addActionListener(actionListener);
}
}Решение задачи: «JComboBox»
textual
Листинг программы
String st=cb.getSelectedItem().toString();