2014-04-25 3 views
0

BreadPanel.javaMeatPanel.java으로 변경하고 싶습니다.다른 클래스의 JFrame을 호출하여 JPanel을 변경하십시오.

다음은 "main"클래스의 코드입니다.

public class FinalProject { 
    static JFrame frame; // How do I call this in another class 

    // Get colors for example 
    private static final Color GREEN = new Color(84, 204, 126); 
    private static final Color WHITE = new Color(255, 255, 255); 
    private static final Color MENUGREEN = new Color(161, 227, 141); 

    // Create a method that creates the UI 
    static void createAndShowGui() { 
    frame = new JFrame("Subway Menu"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.getContentPane().add(new NavPanel(GREEN, 800, 60), BorderLayout.NORTH); 
    frame.getContentPane().add(new QueuePanel(MENUGREEN, 200, 440), BorderLayout.EAST); 

    // The panel I want to change on click 
    frame.getContentPane().add(new BreadPanel(WHITE, 600, 440), BorderLayout.CENTER); 


    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setResizable(false); 
    frame.setVisible(true); 
    } 

    // Main 
    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
    }); 
    } 
} 

다음은 BreadPanel에 대해 변경하려는 패널의 코드입니다.

class MeatPanel extends JPanel { 

    private static final long serialVersionUID = 1L; 
    private static final float FONT_POINTS = 16f; 
    private int prefW; 
    private int prefH; 

    public JButton m1, m2, m3, m4, m5, m6, m7, m8, m9, m10; 
    private JLabel calories, blank, choice; 

     public MeatPanel(Color color, int prefW, int prefH) 
    { 

    // Here is where I want to call it 

     frame.getContentPane().remove(new BreadPanel(color, 600, 440),  BorderLayout.CENTER); 
     frame.getContentPane().add(new MeatPanel(color, 600, 440), BorderLayout.CENTER); 

    // 

actionlistener가 호출 될 때이 패널을 변경하는 더 좋은 방법이 있습니까?

감사

답변

2

사용 CardLayout과에 JFrame의 모두를 추가 할 수 있습니다. 이벤트에 따라 적절한 프레임을 보여줍니다. 에 더 자세히보기 How to Use CardLayout

2

"actionlistener가 호출 될 때이 패널을 변경할 수있는 더 좋은 방법이 있습니까?"

예. PanelLayout을 사용하면 패널 뷰를 바꿀 수 있으므로 패널을 계속 제거하고 추가 할 필요가 없습니다. How to Use CardLayout에서 자세한 내용보기 간단한 예도 참조하십시오. here

+0

고마워요! CardLayout는가는 길과 같습니다! 나는 당신의 답을 곧 표시 할 것입니다. – Michael

관련 문제