我想知道如何设置与 Jbutton 一起使用的 JComboBox。在 JcomboBox 中选择某个对象会在按下按钮时更改计算。这是我到目前为止所拥有的,但它似乎不起作用,我不确定它有什么问题。
//JComboBox objectList = new JComboBox();
String[] objectStrings = { "Triangle", "Box", "Done" };
JComboBox objectList = new JComboBox(objectStrings);
//objectList.setModel(new DefaultComboBoxModel(new String[]{"Triangle", "Box", "Done"}));
objectList.setSelectedIndex(0);
final int object = objectList.getSelectedIndex();
objectList.setBounds(180, 7, 86, 20);
objectList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (object == 2) {
System.exit(0);
}
}
});
frmPrestonPalecekWeek.getContentPane().add(objectList);
JButton btnCalculate = new JButton("Calculate!");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String box;
String done;
Box a;
Triangle b;
b = new Triangle(Double.parseDouble(txtSidea.getText()), Double.parseDouble(txtSideb.getText()), Double.parseDouble(txtSidec.getText()));
a = new Box(Double.parseDouble(txtSidea.getText()), Double.parseDouble(txtSideb.getText()), Double.parseDouble(txtSidec.getText()));
if (object == 0) {
txtOutput.setText("this is the volume " + a.getVolume());
}
else if (object == 2) {
System.exit(0);
}
}