2013-11-27 4 views
0

나는 메뉴 옵션을 클릭하면 윈도우가 '환영 텍스트'에서 사용자가 클릭 할 수있는 4 개의 버튼으로 바뀌도록하려고 시도하고있다. 그러나 시뮬레이션 버튼을 클릭하면 아무 일도 일어나지 않습니다. 윈도우가 전혀 변경되지 않습니다. Ive는 기본적으로 내 코드를 요약했습니다. 아무도 내가 볼 수없는 것을 본다?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

는 당신이 그것을 보여주는 때 JFrame의 (GUI)에 새로운 패널 (thePanel)를 추가,하지만 당신은 반드시이 경우 JFrame (GUI)의 revalidate() 메소드를 호출하기 위해, 다음에 문제를 보인다.

GUI.add(thePanel); 이후에 GUI.revalidate()을 추가하면 도움이됩니다.

관련 문제