2

From a variety of resources (this website, a book, a friend) I have managed to make a JFrame(JFrame1) that responds to a button. It makes another JFrame (JFrame2), and changes the JFrame.setVisible() to false.

What I'm trying to do is make it so that when a declared Back button is pressed, it closes JFrame2 and sets JFrame1 visibility to true. That's all fine, but when I do JFrame2.setVisiblity(false), JFrame2 is still visible. I've tried dispose(); but that doesn't work either.

I'm also wondering, since I've read on stackoverflow, that creating multiple JFrames is bad programming. So should I use JDialogs instead?

I am trying to display a bunch of information, and allow you to interact with the GUI to navigate around the information. The information would be arranged by alphabetical order.

Also, I'm not sure how to post code on here, so if you need to see what I currently have, just tell me how to post code :D

4

1 回答 1

2

You are better of using a single frame, using something like JPanels to hold you UI components. This way you can simply switch it the panels as you need, possibly with something like CardLyout

By moving your UI to panels, you are also decoupling your code, giving your more flexibility and reuse potential.

Updated with basic example

This basically uses a simple model to change out different views. You could use a different style of model that listens to changes to the views and then makes choices on there behalf (which would generally be my preferred method), it depends on what you want to do...

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

    }
}
于 2013-05-26T02:55:16.487 回答