2013-11-27 145 views
0

我试图得到它,所以当我点击一个菜单选项时,窗口从“欢迎文本”更改为4个按钮,然后用户可以点击。然而,当我点击模拟按钮时,什么都没有发生..窗口根本不会改变..顺便说一下,我总结了我的代码到基本的东西。任何人都看到我不能说的东西?JPanel java没有显示

JFrame GUI = new JFrame("Graphical User Interface"); 
public gui() 
    { 
    JMenuBar menubar = new JMenuBar(); 
    JMenu Simulation = new JMenu("Simulation"); 

    theLabel = new JLabel("Welcome to the Main Menu. ",JLabel.CENTER); 
    GUI.add(theLabel); 

    menubar.add(Simulation); 
    Simulation.add(Simulationmenu); 

    Simulationmenu.addActionListener(this); 

    GUI.setJMenuBar(menubar); 
    GUI.setLocation(500,250); 
    GUI.setSize(300, 200); 
    GUI.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent E){ 

if(E.getSource() == Simulationmenu){ 
    // Buttons in the menu i want to output once clicked on 'simulation' 
       thePanel = new JPanel(new GridLayout(4,0)); 

       Run = new JButton("Run"); 
       Pause = new JButton("Pause"); 
       Reset = new JButton("Reset"); 
       DisplayMaps = new JButton("Display Maps?"); 

       // Add the components to the panel: 
       thePanel.add("West", Run); 
       thePanel.add("Center", Pause); 
       thePanel.add("East", Reset); 
       thePanel.add("West", DisplayMaps); 

       // Add the panel to the contentPane of the frame: 
       GUI.add(thePanel); 

       // add this object as listener to the two buttons: 
       Run.addActionListener(this); 
+0

1)为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 2)对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。 3)对于一个空间中的许多组件,使用['CardLayout'](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) ](http://stackoverflow.com/a/5786005/418556)。 –

回答

2

似乎在接下来你的问题,你添加一个新的面板(thePanel)到您的JFrame(GUI)时,它显示,但在这种情况下,你必须调用JFrameGUIrevalidate()方法。

GUI.add(thePanel);之后加上GUI.revalidate(),它可以帮助你。