Java GUI程序打开一个新的JPanel而不是替换当前面板

时间:2014-07-16 14:32:19

标签: java swing

我正在学习java GUI,我正在研究一个非常简单的程序。

我有4个类,一个初始化gui,一个有一些随机方法,最后两个是gui相关的,这是我感兴趣的。所以这是我的GUI类:

public class GUI extends JFrame{
    Container container;
    CardLayout cl;
    GridBagLayout gl;
    GridBagConstraints gbc;


public GUI(){

    super("Game Finder");
    cl = new CardLayout();
    gl = new GridBagLayout();
    gbc = new GridBagConstraints();
    setLayout(cl);    
    container = this.getContentPane();

        //File>New
    JPanel newPanel = new JPanel();
    newPanel.setLayout(gl); 
    newPanel.setBackground(Color.YELLOW);

    JLabel newLabel = new JLabel("Time to find the game!");

    JButton newButton = new JButton("Begin!");
    newButton.setPreferredSize(new Dimension(200,75));
    gbc.weightx=1;
    gbc.weighty=1;      
    gbc.gridx=0;
    gbc.gridy=1;
    gbc.anchor = GridBagConstraints.PAGE_START;
    gl.setConstraints(newButton, gbc);      
    newPanel.add(newButton, gbc);

    newLabel.setFont(new Font("Serif", Font.BOLD, 24));
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.anchor = GridBagConstraints.CENTER;

    gl.setConstraints(newLabel, gbc);
    newPanel.add(newLabel, gbc);

    container.add("New", newPanel);

        //File>Load
    JPanel loadPanel = new JPanel();
    loadPanel.setBackground(Color.BLUE);
    JLabel loadLabel = new JLabel("Feature yet to be implemented");
    loadPanel.add(loadLabel);
    container.add("Load", loadPanel);
        //File>Save
    JPanel savePanel = new JPanel();
    savePanel.setBackground(Color.RED);
    JLabel saveLabel = new JLabel("Feature yet to be implemented");
    savePanel.add(saveLabel);
    container.add("Save", savePanel);       
        //Help>About
    JPanel aboutPanel = new JPanel();
    JLabel aboutLabel = new JLabel();
    String s = "Lorem iasd asd .";
    aboutLabel.setText("<html><body style='width:230px'>" + s);
    aboutPanel.setBackground(Color.WHITE);
    aboutLabel.setBorder(new EmptyBorder(0,20,0,20));
    aboutPanel.add(aboutLabel);
    container.add("About", aboutPanel);

    // Construct objects  
    JMenuBar menuBar = new JMenuBar();  
    JMenu menuFile = new JMenu("File"); // File menu
    JMenu menuHelp = new JMenu("Help"); // Help menu

    JMenuItem menuFileNew = new JMenuItem("New Game"); // New Game
    JMenuItem menuFileLoad = new JMenuItem("Load Game"); // Load Game  
    JMenuItem menuFileSave = new JMenuItem("Save Game"); // Save Game  
    JMenuItem menuFileExit = new JMenuItem("Exit"); // Exit
    JMenuItem menuHelpAbout = new JMenuItem("About"); // About   

    // Add action listener.for the menu button     
    menuFileExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new Methods().windowClosed();
        }          
    }  
    );

    menuFileNew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.show(container, "New");
        }  
    }  
    );
    menuFileLoad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.show(container, "Load");  
        }  
    }  
    );   
    menuFileSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.show(container, "Save"); 
        }  
    }  
    );

    menuHelpAbout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cl.show(container, "About"); 

        }  
    }  
    );  
/*HERE in particular*/
    newButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){             
            GamePanels g = new GamePanels(); 
            g.begin();                          
            container.add("Begin", g.getPanel());
            cl.show(container, "Begin");
        }
    });

    // Add menu items to menu bar  
    menuFile.add(menuFileNew);  
    menuFile.add(menuFileLoad);  
    menuFile.add(menuFileSave);  
    menuFile.add(menuFileExit);  
    menuBar.add(menuFile);         
    menuHelp.add(menuHelpAbout);
    menuBar.add(menuHelp);

    // Create window    
    setJMenuBar(menuBar);  
    setSize(new Dimension(350, 350));  
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}  
public Container getContainer(){
    return this.container;
}

这是我的GamePanels课程:

public class GamePanels {
    GUI gui = new GUI();    
    JPanel begin;

public void begin(){

    begin = new JPanel();
    begin.setBackground(Color.MAGENTA);
    JLabel beginLabel = new JLabel("Do you want to avoid using much of your brain?");
    JButton beginYes = new JButton("Yes");
    JButton beginNo = new JButton("No");
    begin.add(beginLabel);
    begin.add(beginYes);
    begin.add(beginNo);         
}

对于GUI类中的监听器,如果我点击文件&gt; about之类的东西,它就可以完美地工作,即用选定的容器面板切换内容。但是,当我尝试使用“外部”面板时,它会打开一个全新的GUI界面,因此我最终会得到两个这样的界面:

enter image description here

假设我的实施有问题?

1 个答案:

答案 0 :(得分:1)

我认为你的问题在这一行:

 GamePanels g = new GamePanels(); //your gamePanel has a new GUI

 public class GamePanels {
   GUI gui = new GUI();  // your GUI is a new container
   //...

所以在actionPerformed中,你不应该新建一个GamePanel,而是最后一个GamePanel。

在您的情况下,您可能希望使用 Layered Pane Tabbed Pane