我目前正在学习 java 并坚持使用 JComboBox。我有一种感觉,我正在尝试并在 4 小时内撞墙。
我试图让用户从 ComboBox 中选择 1-10。如何获得组合框的值?组合框的值相当于数量。
所以我有另一个价值,可能是 10 美元。如果用户选择数量 2。
我想获得用户选择的价值,然后取 10 美元的价值并将其乘以 2。
20 美元的结果将显示在 JTextField 上。
请帮忙 :(
public class Panel extends JPanel {
public Panel(){
JPanel test = new JPanel(new GridBagLayout());
String[] quantities1 = {"0","1","2","3","4","5","6","7","8","9","10"};
JComboBox quantitiesCB = new JComboBox(quantities1);
quantitiesCB.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JComboBox combo = (JComboBox)e.getSource();
String currentQuantity = (String)combo.getSelectedItem();
}
}
);
JTextField result = new JTextField();
setLayout(new GridBagLayout());
setPreferredSize(new Dimension(640,480));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.1;
gbc.weighty = 0.1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTH;
add(quantitiesCB, gbc);
}
}