2014-07-16 23 views
0

我正在学习java GUI,我正在开发一个非常简单的程序。Java GUI程序打开一个新的JPanel而不是替换当前面板

我有4个班,一个初始化图形界面,其中有一定的随机方法,和去年2相关的图形用户界面,这是我很感兴趣的是那些因此,这里是我的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类中的听众,如果我点击像文件>有关,它完美的作品即内容与所选择的容器面板切换。然而,当我尝试使用“外部”面板,它开辟了一个全新的GUI界面,所以我当时有两个这样结束了:

enter image description here

假设有什么不对我的执行?

回答

1

我认为你的问题是在这条线:那么在actionPerformed

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

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

,你不应该新一的GamePanel但最后的GamePanel。

而在你的情况下,你可能需要使用Layered PaneTabbed Pane

+0

我觉得你在那里的东西,有道理!也许如果我看看我的主类,它是我初始化GUI的地方,如果我再次使用那个gui或者返回gui:'public static void .... {GUI initialize = new GUI(); '那么也许我可以使用一种方法来返回现有的gui并使用那个呢? – gudthing

+0

在这种情况下,你可以将它设置为静态或将它发送到你需要的参数或使用上面的组件(如果你只想使用一个帧) – Tony

+0

已解决,我不知道为什么我有'GUI gui = GamePanels类中的新GUI。它实际上一直在工作,但它正在创建一个新的类实例。无论如何谢谢托尼,你让我看得更清楚! – gudthing

相关问题