【发布时间】:2017-04-06 13:22:34
【问题描述】:
JComboBox 有一些问题,需要选择不同的项目。选择项目时,我希望新的相关表单出现在用户可以为所选项目输入更多信息(仅每个选择的形式)。在我的程序中,出现了错误的表格,或者几个表格出现的顺序错误。例如,如果我运行程序并选择“Smycke”(珠宝,我的部分代码是瑞典语),则在我选择另一个选项之前不会发生任何事情,例如“Aktie”(Stock) - 然后出现“Smycke”表单通过“Aktie”形式。如何让正确的表格出现?
我的部分代码:
public class Program extends JFrame {
private ArrayList<Valueables> allValueables = new ArrayList<>();
private JTextArea display;
private JRadioButton sortName;
private JRadioButton sortValue;
private String[] valueables = { "Smycke", "Aktie", "Apparat" };
private JComboBox<String> box = new JComboBox<String>(valueables);
Program() {
super("Sakregister");
JPanel top = new JPanel();
top.add(new JLabel("Värdesaker"));
add(top, BorderLayout.NORTH);
JPanel left = new JPanel();
add(left, BorderLayout.WEST);
JPanel right = new JPanel();
right.add(new JLabel("Sortering"));
sortName = new JRadioButton("Namn", true);
right.add(sortName);
sortValue = new JRadioButton("Värde");
right.add(sortValue);
ButtonGroup groupb = new ButtonGroup();
groupb.add(sortName);
groupb.add(sortValue);
add(right, BorderLayout.EAST);
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
JPanel bottom = new JPanel();
bottom.add(new JLabel("Nytt:"));
bottom.add(box);
box.addActionListener(new BoxLis());
add(bottom, BorderLayout.SOUTH);
display = new JTextArea();
JScrollPane scroll = new JScrollPane(display);
display.setEditable(false);
display.setWrapStyleWord(true);
add(scroll, BorderLayout.CENTER);
setSize(600, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class BoxLis implements ActionListener {
public void actionPerformed(ActionEvent ave) {
if (box.getSelectedIndex() == 0) {
box.addActionListener(new JewelryLis());
} else if (box.getSelectedIndex() == 1) {
box.addActionListener(new StockLis());
} else if (box.getSelectedIndex() == 2) {
box.addActionListener(new DeviceLis());
}
}
}
class JewelryLis implements ActionListener {
public void actionPerformed(ActionEvent ave) {
JewelryForm f = new JewelryForm();
try {
int svar = JOptionPane.showConfirmDialog(Program.this, f, "Nytt smycke", JOptionPane.OK_CANCEL_OPTION);
if (svar != JOptionPane.OK_OPTION)
return;
String name = f.getName();
int stones = f.getStones();
boolean isGold = f.getGold();
addJewelry(name, stones, isGold);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
}
}
}
class StockLis implements ActionListener {
public void actionPerformed(ActionEvent ave) {
StockForm f = new StockForm();
try {
int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny aktie", JOptionPane.OK_CANCEL_OPTION);
if (svar != JOptionPane.OK_OPTION)
return;
String name = f.getName();
int amount = f.getAmount();
int stockPrice = f.getStockPrice();
addStock(name, amount, stockPrice);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(Program.this, "Fel inmatning!");
}
}
}
class DeviceLis implements ActionListener {
public void actionPerformed(ActionEvent ave) {
DeviceForm f = new DeviceForm();
try {
int svar = JOptionPane.showConfirmDialog(Program.this, f, "Ny apparat", JOptionPane.OK_CANCEL_OPTION);
if (svar != JOptionPane.OK_OPTION)
return;
String name = f.getName();
int purchasePrice = f.getPurchasePrice();
int wear = f.getWear();
addDevice(name, purchasePrice, wear);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(Program.this, "Fel!");
}
}
}
【问题讨论】:
标签: java swing jframe actionlistener jcombobox