gpt4 book ai didi

java - 从另一个类调用 JFrame 来更改 JPanel

转载 作者:行者123 更新时间:2023-12-01 18:33:00 27 4
gpt4 key购买 nike

我想将 BreadPanel.java 更改为 MeatPanel.java

这是我的“主”类的代码。

    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);

//

调用 Action 监听器时是否有更好的方法来更改这些面板?

谢谢

最佳答案

使用CardLayout并将两个JFrame添加到其中。根据事件,显示适当的框架。 查看更多How to Use CardLayout

关于java - 从另一个类调用 JFrame 来更改 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com