BreadPanel.java
을 MeatPanel.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가 호출 될 때이 패널을 변경하는 더 좋은 방법이 있습니까?
감사
고마워요! CardLayout는가는 길과 같습니다! 나는 당신의 답을 곧 표시 할 것입니다. – Michael