多个JFrame或JDialog

时间:2013-05-26 02:52:23

标签: java swing jframe awt jdialog

从各种资源(这个网站,一本书,一位朋友)我设法制作一个响应按钮的JFrame(JFrame1)。它会生成另一个JFrame(JFrame2),并将JFrame.setVisible()更改为false

我要做的是让它按下,当按下一个声明的后退按钮时,它会关闭JFrame2并将JFrame1的可见性设置为true。这一切都很好,但当我做JFrame2.setVisiblity(false)时,JFrame2仍然可见。我试过dispose();但这也不起作用。

我也想知道,因为我读过stackoverflow,创建多个JFrames是不好的编程。那么我应该使用JDialogs吗?

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

此外,我不确定如何在此处发布代码,因此如果您需要查看我目前的内容,请告诉我如何发布代码:D

1 个答案:

答案 0 :(得分:2)

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

之类的东西

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

更新了基本示例

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

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

    }
}