在第二个面板上时删除面板

时间:2018-11-01 20:26:02

标签: java swing

我正在从事以下项目,并且遇到了两个障碍: 打开第二个面板时,我不知道如何删除第一个面板。 另外,在第二个面板上,我想显示7号门,但是像在第一个面板中的第一个门一样,被遮盖了。请帮忙,我只是在学习,正变得非常沮丧。谢谢。 这是我的代码:

打包无线电组;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


 // @author Laura

public class RadioGroup extends JFrame  {
    private static final long serialVersionUID = 1L;
    private ButtonGroup group;
    private JPanel radioPanel = new JPanel(); //panel A
    private JPanel radioPanelTwo = new JPanel();  // panel B

    public RadioGroup() {
        super("Simulator");
        radioPanel.setLayout(new GridLayout(2,2));
        group = new ButtonGroup();
        JRadioButton one = new JRadioButton("Door 1", true);
        one.setEnabled(false);  //  close door forever
        JRadioButton two = new JRadioButton("Door 2");
        JRadioButton seven = new JRadioButton("Door 7");
        JRadioButton six = new JRadioButton("Door 6");


        one.setActionCommand("1");
        two.setActionCommand("2");
        seven.setActionCommand("7");
       // seven.setEnabled(false); 
        six.setActionCommand("6");

        group.add(one);
        group.add(two);
        group.add(seven); 
        group.add(six);

        radioPanel.add(one);
        radioPanel.add(two);
        radioPanel.add(seven);
        radioPanel.add(six);

        radioPanelTwo.setLayout(new GridLayout(2,2));
        JRadioButton three = new JRadioButton("Door 3");
        JRadioButton four = new JRadioButton("Door 4");
        JRadioButton Seven = new JRadioButton("Door 7");
        JRadioButton five = new JRadioButton("Door 5");
        three.setActionCommand("3");
        four.setActionCommand("4");
        five.setActionCommand("5");
        Seven.setActionCommand("7");
        //seven.setActionCommand("7");

        group.add(three);
        group.add(four);
        group.add(five);
        group.add(Seven);

        radioPanelTwo.add(three);
        radioPanelTwo.add(four);

        radioPanelTwo.add(Seven);

        radioPanelTwo.add(five);
       // radioPanelTwo.add(seven);


        getContentPane().add(radioPanel, BorderLayout.CENTER);
        // add panelTwo 
        getContentPane().add(radioPanelTwo, BorderLayout.EAST);
        radioPanelTwo.setVisible(false);
        JPanel buttonPanel = new JPanel();
        JButton showDialog = new JButton("Submit");
        showDialog.addActionListener(new ShowDialogListener());
        buttonPanel.add(showDialog);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(400, 300);
        setVisible(true);
    }

    public static void main(String[] arg) {
        new RadioGroup();    
    }

    private class ShowDialogListener implements ActionListener  {
        public void actionPerformed(ActionEvent e)  {

            String msg1 = "You are int the clutches of Baron Evil!";
            String msg2 = "You have entered the forbidden abyss!";
            String msg3 = "You have entered the mysterious room B!";
            String msg4 = "You are in the clutches of Harry Mudd!";
            String msg5 = "You have entered Sagittarius A!";
            String msg6 = "You have won!";

            String door = group.getSelection().getActionCommand();

            JOptionPane.showMessageDialog(RadioGroup.this,
                    "You have chosen door " + door +".");

            //  Decision making process
            if (door == "2")    {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg1 + " Game over!"); 
               // two.setEnabled(false); 
                //  terminate JVM successfully
                System.exit(0);
            } //  end door 2
            else if (door == "6")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg2 + " Game over!");
                System.exit(0);
            }  //  end door 6
             else if (door == "7")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg3 + " Be cautious!"); 


        //  Show the second panel
        radioPanelTwo.setVisible(true);

        }  // end door 7

            else if (door == "3")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg6 + " You have exited safely!");
                System.exit(0);
            }  // end door 3 (winning door)
            else if (door == "4")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg4 + " Game over!"); 
                System.exit(0); 
            }  // end door 4
            else if (door == "5")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg5 + " Game over!"); 
                System.exit(0);
            }  //  end door 5
            else if (door == "7")   {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        msg4 + " Game over!"); 
            }  
            else {
                JOptionPane.showMessageDialog(RadioGroup.this,
                        "You are in, select another door!");
            }

        }         

    }
}

0 个答案:

没有答案