gpt4 book ai didi

java - 在单独的类中重新绘制/更新 JPanel 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:31 24 4
gpt4 key购买 nike

我正在处理 Java 作业,并且创建了一个 JFrame 小程序,该小程序具有在单独的类中创建的四个面板,这些面板返回到主类并添加到主面板。我面临的问题是我的南面板中有一个组合框。根据该组合框中的选择,我希望根据该选择更新一个或多个其他面板。我不知道如何实现这一点,我已经考虑重新绘制单个面板,甚至重新创建整个主面板。任何帮助和/或建议将不胜感激。

我已经从主面板类中发布了下面的代码,以供初学者使用。

我是第一次在这里发帖,所以如果没有足够的细节或者我遗漏了一些内容,请告诉我。

public class Main_GUI_Panel extends JFrame {

public static Panel_North northPanel;
public static Panel_Center centerPanel;
public static Panel_West westPanel;
public static Panel_South southPanel;
private int index = -1;

public Main_GUI_Panel() {
super("Java 2: Final Project");
getContentPane().setBackground(Color.BLACK); //set the applet background color
setLayout(new BorderLayout()); //set the Layout for the applet

//START - call methods to create & set panels
northPanel = new Panel_North();
westPanel = new Panel_West();
centerPanel = new Panel_Center();
southPanel = new Panel_South();

add(northPanel.setNorth(),BorderLayout.NORTH); //set the North portion of the applet
add(westPanel.setWest(index),BorderLayout.WEST); //set the West portion of the applet
add(centerPanel.setCenter(),BorderLayout.CENTER); //set the Center portion of the applet
add(southPanel.setSouth(),BorderLayout.SOUTH); //set the South portion of the applet
//END - call methods to set panels
}

最佳答案

当您创建 GUI 时,您应该(必须)创建一个或多个模型类来保存 GUI 的数据。这是模型。你的 GUI 组件就是 View 。这些部分构成了应用程序的模型/ View / Controller (MVC)。

您将顶级模型类的实例传递给 GUI 的所有面板。每个面板都可以更新模型类,反过来,面板从模型中获取数据。

因此,您在一个面板中执行的操作会影响不同面板的显示。

This answer详细介绍 GUI 模型和 GUI View 。

关于java - 在单独的类中重新绘制/更新 JPanel 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13823198/

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