How can I replace one of two JPanels with another JPanel in Java?

前端 未结 3 608
小蘑菇
小蘑菇 2021-01-24 08:48

I designed an interface for the welcome screen with one JFrame included two JPanels (JPanel1 on right and JPanel2 on left). The buttons on the left is to switch the Panels in JP

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-24 09:23

    Here is a very simple example of something that should approximate your description. On the left, we have a hug button to toggle the content of the right panel. On the right, you have a panel with a given border and a label. When you press the button, the content on the right is swapped with the other panel.

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class TestCardLayout2 {
    
        protected void initUI() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel leftPanel = new JPanel(new BorderLayout());
            JLabel label = new JLabel("Left panel");
            leftPanel.add(label, BorderLayout.NORTH);
            JButton button = new JButton("Toggle right panel");
            leftPanel.add(button);
            frame.add(leftPanel, BorderLayout.WEST);
    
            final CardLayout cardLayout = new CardLayout();
            final JPanel rightPanel = new JPanel(cardLayout);
            rightPanel.setPreferredSize(new Dimension(200, 500));
    
            JPanel rightPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
            rightPanel1.setBorder(BorderFactory.createLineBorder(Color.RED));
            JPanel rightPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            rightPanel2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
            JLabel label1 = new JLabel("Right panel 1 with a red border");
            JLabel label2 = new JLabel("Right panel 2 with a blue borer");
            rightPanel1.add(label1);
            rightPanel2.add(label2);
    
            rightPanel.add(rightPanel1, "panel1");
            rightPanel.add(rightPanel2, "panel2");
            frame.add(rightPanel, BorderLayout.EAST);
    
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    cardLayout.next(rightPanel);
                }
            });
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TestCardLayout2().initUI();
                }
            });
        }
    
    }
    

提交回复
热议问题