簡體   English   中英

使用子菜單創建 GUI

[英]Creating GUI with sub menus

我正在嘗試構建一個具有子菜單並可能返回到主菜單和選項的 GUI,到目前為止我已經創建了一個主菜單但是當我嘗試單擊這些框時什么也沒有打開也只有 4 個項目在我編碼時顯示 5項目。 有誰知道解決這個問題的方法? 另外如果有人知道如何創建不同的框而不是復選框。 非常感謝

public class CoinSorterGUI {

public static void main(String[] args) {
    final JFrame frame=new JFrame("CoinSorterGUI");
    
    class MyItemListener implements ItemListener{
        
        public void itemStateChanged(ItemEvent ev) {
            boolean selected = (ev.getStateChange()== ItemEvent.SELECTED);
            AbstractButton button =(AbstractButton) ev.getItemSelectable();
            String command = button.getActionCommand();
            if (selected) {
                int messageType = -1;
                String message = "";
                if (command.equals("CoinCalculator")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Welcome to the CoinCalculator";
            }else if (command.equals("Multiple CoinCalculator")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Welcome to the Multiple CoinCalculator";
            }else if (command.equals("Print Coin List")) {
                    messageType = JOptionPane.INFORMATION_MESSAGE;
                    message = "Current coin denominations are; £2, £1, 50p. 20p and 10p";
            }else if (command.equals("Set details")) {
                messageType = JOptionPane.INFORMATION_MESSAGE;
                message = " ** Set details Sub Menu ** "
                        + "    1 -  Set Currency       "
                        + "    2 -  Set minimum input value"
                        + "    3 -  Set Maximum input value"
                        + "    4 -  Return to main menu";
            }else if (command.equals("Display program configuarations")) {
                messageType = JOptionPane.INFORMATION_MESSAGE;
                message = "The current currency is £, the minimum input value is 0 and the maximum input value is 10000";
            }
                JOptionPane.showMessageDialog(frame,
                        message,
                        "CoinSorter",
                        messageType);
            }
                    
          }
        }

        JRadioButton r1 = new JRadioButton("CoinCalculator");
        r1.setActionCommand("Welcome to the CoinCalculator");
 
        JRadioButton r2 = new JRadioButton("Multiple CoinCalculator");
        r2.setActionCommand("Welcome to the Multiple CoinCalculator");
 
        JRadioButton r3 = new JRadioButton("Print Coin List");
        r3.setActionCommand("Current coin denominations are; £2, £1, 50p. 20p and 10p");
 
        JRadioButton r4 = new JRadioButton("Set details");
        r4.setActionCommand(" ** Set details Sub Menu ** "
                + "    1 -  Set Currency       "
                + "    2 -  Set minimum input value"
                + "    3 -  Set Maximum input value"
                + "    4 -  Return to main menu");
        JRadioButton r5 = new JRadioButton("Display program configuarations");
        r5.setActionCommand("The current currency is £, the minimum input value is 0 and the maximum input value is 10000");
    
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);
        group.add(r3);
        group.add(r4);
        group.add(r5);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        Container cont = frame.getContentPane();
 
        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Please choose from the options: "));
        cont.add(r1);
        cont.add(r2);
        cont.add(r3);
        cont.add(r4);
 
        frame.setVisible(true);

}
}

當我編碼了 5 個項目時,只顯示了 4 個項目

    cont.add(r4);

    frame.setVisible(true);

應該:

    cont.add(r4);
    cont.add(r5); // add ALL the buttons!

    frame.setVisible(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM