1

我正在处理一项 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
    }
4

1 回答 1

1

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

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

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

这个答案更详细地介绍了 GUI 模型和 GUI 视图。

于 2012-12-11T15:52:46.250 回答