How can I switch between jpanels?

后端 未结 3 927
独厮守ぢ
独厮守ぢ 2020-12-11 21:28

I\'m still very new to java programming, so please help me to correct any mistakes I might have overlooked or give tips on how to improve this program.

Okay, so a lo

3条回答
  •  無奈伤痛
    2020-12-11 21:49

    The best way to approach this is using a CardLayout.

    enter image description here

    enter image description here

    Notes

    • A button with an ActionListener is far better than a MouseListener over a rectangle.
      • The button will show focus when the mouse is pointed at it, or the component is tabbed to via the keyboard.
      • The button is keyboard accessible.
      • The button has facility to support multiple icons built in (e.g. for 'initial look', focused, pressed etc.)
    • White space in the GUI is provided around the menu panel and game by adding an EmptyBorder
    • The button is made larger by setting a margin.
    • Adjust margins, borders and preferred size according to need. These sizes were set by me so as not to make the screenshots too large.
    • See more tips in the code comments.

    Code

    Here is the MCTaRE (Minimal Complete Tested and Readable Example) that produced the above screenshots.

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class PipesGame {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    // the GUI as seen by the user (without frame)
                    final CardLayout cl = new CardLayout();
                    final JPanel gui = new JPanel(cl);
                    // remove if no border is needed
                    gui.setBorder(new EmptyBorder(10,10,10,10));
    
                    JPanel menu = new JPanel(new GridBagLayout());
                    JButton playGame = new JButton("Play!");
                    ActionListener playGameListener = new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            cl.show(gui, "game");
                        }
                    };
                    playGame.addActionListener(playGameListener);
                    Insets margin = new Insets(20, 50, 20, 50);
                    playGame.setMargin(margin);
                    menu.add(playGame);
                    gui.add(menu);
                    cl.addLayoutComponent(menu, "menu");
    
                    JPanel pipes = new Pipes();
                    gui.add(pipes);
                    cl.addLayoutComponent(pipes, "game");
    
                    JFrame f = new JFrame("Pipes Game");
                    f.add(gui);
                    // Ensures JVM closes after frame(s) closed and
                    // all non-daemon threads are finished
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // See https://stackoverflow.com/a/7143398/418556 for demo.
                    f.setLocationByPlatform(true);
    
                    // ensures the frame is the minimum size it needs to be
                    // in order display the components within it
                    f.pack();
                    // should be done last, to avoid flickering, moving,
                    // resizing artifacts.
                    f.setVisible(true);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
            SwingUtilities.invokeLater(r);
        }
    }
    
    class Pipes extends JPanel {
    
        Pipes() {
            setBackground(Color.BLACK);
            setForeground(Color.WHITE);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString("Pipes game appears here..", 170, 80);
        }
    
        @Override 
        public Dimension getPreferredSize() {
            // adjust to need
            return new Dimension(500,150);
        }
    }
    

提交回复
热议问题