通过各种资源(该网站,一本书,一个朋友),我设法制作了一个响应按钮的JFrame(JFrame1)。它制作另一个JFrame(JFrame2),并将JFrame.setVisible()更改为false

我正在尝试做的是,这样一来,当按下声明的“后退”按钮时,它将关闭JFrame2并将JFrame1可见性设置为true。没关系,但是当我执行JFrame2.setVisiblity(false)时,JFrame2仍然可见。我已经尝试过dispose();但这也不起作用。

我还想知道,由于我已经阅读了stackoverflow,因此创建多个JFrames是不好的编程。那我应该改用JDialogs吗?

我试图显示大量信息,并允许您与GUI交互以浏览信息。该信息将按字母顺序排列。

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

最佳答案

您最好使用单个框架,并使用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"));
        }

    }
}

10-04 19:49