JComboBox:选择后出现错误的表单

时间:2017-04-06 13:22:35

标签: java swing jframe actionlistener jcombobox

我对JComboBox有一些问题,需要选择不同的项目。当选择一个项目时,我想要一个新的相关表格出现在用户可以输入所选项目的更多信息的位置(每个选项只有一个表格)。在我的程序中,出现错误的表单,或者几个表单以错误的顺序出现。例如,如果我运行程序并选择“Smycke”(珠宝,我的部分代码是瑞典语),在我选择其他选项之前没有任何反应,例如“Aktie”(股票) - 然后出现“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!");
            }
        }

    }

1 个答案:

答案 0 :(得分:3)

在BoxLis列表器中,您没有显示适当的表单,而是将动作列表添加到组合框中。

对您的代码进行更改将解决问题。

box.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (box.getSelectedIndex() == 0) {
                showJewlryForm();
            } else if (box.getSelectedIndex() == 1) {
                showStockForm();
            } else if (box.getSelectedIndex() == 2) {
                showDeviceForm();
            }

        }
    });    

    private void showJewlryForm() {
        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!");
        }
    }

    private void showStockForm() {

        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!");
        }

    }

    private void showDeviceForm() {

        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!");
        }

    }