java - 多个 JFrame 或 JDialog

标签 java swing jframe awt jdialog

通过各种资源(这个网站、一本书、一个 friend ),我成功地制作了一个响应按钮的 JFrame(JFrame1)。它创建另一个 JFrame (JFrame2),并将 JFrame.setVisible() 更改为 false

我想做的是,当按下声明的“后退”按钮时,它会关闭 JFrame2 并将 JFrame1 可见性设置为 true。一切都很好,但是当我执行 JFrame2.setVisibity(false) 时,JFrame2 仍然可见。我试过dispose();但这也行不通。

我还想知道,因为我在 stackoverflow 上读过,创建多个 JFrame 是糟糕的编程。那么我应该使用 JDialogs 来代替吗?

我正在尝试显示一堆信息,并允许您与 GUI 交互以浏览信息。信息将按字母顺序排列。

另外,我不知道如何在这里发布代码,所以如果您需要查看我当前拥有的内容,请告诉我如何发布代码:D

最佳答案

您最好使用单个框架,使用诸如 JPanels 之类的东西来容纳 UI 组件。这样您就可以根据需要简单地切换面板,可能使用诸如 CardLyout

之类的东西

通过将 UI 移至面板,您还可以解耦代码,从而提供更大的灵活性和重用潜力。

更新了基本示例

这基本上使用一个简单的模型来改变不同的 View 。您可以使用不同风格的模型来监听 View 的更改,然后代表那里做出选择(这通常是我的首选方法),这取决于您想要做什么...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CardLayoutDemo {

    public static void main(String[] args) {
        new CardLayoutDemo();
    }

    public CardLayoutDemo() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MasterPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class GameModel {

        private JPanel view;
        private JPanel lastView;
        private JPanel currentView;
        private WelcomePane welcomePane;
        private GamePane gamePane;
        private SettingsPane settingsPane;

        public GameModel(JPanel view) {
            this.view = view;

            welcomePane = new WelcomePane(this);
            gamePane = new GamePane(this);
            settingsPane = new SettingsPane(this);
        }

        public void welcome() {
            lastView = currentView;
            view.removeAll();
            view.add(welcomePane);
            view.revalidate();
            view.repaint();
            currentView = welcomePane;
        }

        public void newGame() {
            lastView = currentView;
            view.removeAll();
            view.add(gamePane);
            view.revalidate();
            view.repaint();
            currentView = gamePane;
        }

        public void settings() {
            lastView = currentView;
            view.removeAll();
            view.add(settingsPane);
            view.revalidate();
            view.repaint();
            currentView = settingsPane;
        }

        public void back() {
            if (lastView != null) {
                view.removeAll();
                view.add(lastView);
                view.revalidate();
                view.repaint();
                currentView = lastView;
                lastView = null;
            }
        }

    }

    public class MasterPane extends JPanel {

        public MasterPane() {
            setLayout(new BorderLayout());
            GameModel model = new GameModel(this);
            model.welcome();
        }

    }

    public class WelcomePane extends JPanel {

        private GameModel model;

        public WelcomePane(GameModel gameModel) {
            this.model = gameModel;
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JButton btnStart = new JButton("New Game");
            JButton btnSettings = new JButton("Settings");

            add(new JLabel("Welcome"), gbc);
            add(btnStart, gbc);
            add(btnSettings, gbc);

            btnSettings.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.settings();
                }

            });

            btnStart.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.newGame();
                }

            });
        }

    }

    public class SettingsPane extends JPanel {

        private GameModel model;

        public SettingsPane(GameModel gameModel) {
            this.model = gameModel;
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("Go ahead, make some changes..."), gbc);

            JButton back = new JButton("Back");
            back.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.back();
                }
            });
            add(back, gbc);
        }

    }

    public class GamePane extends JPanel {

        private GameModel model;

        public GamePane(GameModel model) {
            this.model = model;
            setLayout(new GridBagLayout());
            add(new JLabel("All your base are belong to us"));
        }

    }
}

关于java - 多个 JFrame 或 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755796/

相关文章:

java - 如何从应用程序中的每个文件中删除特定的导入?

java - 在java中绘制两个不同位置和颜色的圆圈

java - 不同组件的布局问题?

java - 为什么我的碰撞检测无法正常工作?

java - 将 JFrame 转换为 JPanel

java - ConcurrentHashMap 的例子

java - 如何从ArrayList中获取所需的格式

java - 使用 Spring ApplicationContext 类路径的绝对路径加载资源文件(如(sql、txt 等))

Java Swing - 使用 TransferHandler 动态更改 JList

java - 存储应用程序设置的最佳方式是什么? (MVC)