Java swing; How to toggle panel's visibility?

2019-01-19 08:37发布

问题:

i made this code to navigate trough panel1 and panel2 with buttons.

(button1 and button2) but when i run my code the frame stays empty.

Can somebody explain me what i'm doing wrong and how i can accomplish

toggling between panel1 and panel2 in this way? Starting with panel1 first

Code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class togglepanel {

    public static void main(String[] args) {

        final JFrame frame = new JFrame();
        final JPanel panel1 = new JPanel();
        final JPanel panel2 = new JPanel();
        JButton button1 = new JButton("previous frame!");
        JButton button2 = new JButton("next frame");

        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setSize(600, 400);
        frame.add(panel1);
        frame.add(panel2);

        panel1.add(button2);
        panel1.setVisible(true);

        panel2.add(button1);
        panel2.setVisible(false);

        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                panel1.setVisible(true);
                panel2.setVisible(false);

            }
        });


        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                panel1.setVisible(false);
                panel2.setVisible(true);
            }
        });
    }
}

thanks in advance

回答1:

Use a layout manager.

frame.setLayout(new FlowLayout());



回答2:

Another useful way to do this, and I think better is to use a CardLayout and to add both JPanels to a container that uses this CardLayout. You can then easily swap views by calling the CardLayout methods.

e.g.,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TogglePanel {
   public static void main(String[] args) {
      final CardLayout cardlayout = new CardLayout();
      final JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      final Container contentPane = frame.getContentPane();
      contentPane.setLayout(cardlayout);

      final JPanel panel1 = new JPanel();
      final JPanel panel2 = new JPanel();
      JButton button1 = new JButton("previous frame!");
      JButton button2 = new JButton("next frame");
      contentPane.setPreferredSize(new Dimension(600, 400));
      contentPane.add(panel1, "Panel 1");
      contentPane.add(panel2, "Panel 2");
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setResizable(true);
      frame.setVisible(true);

      panel1.add(button2);
      panel2.add(button1);

      ActionListener btnListener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardlayout.next(contentPane);
         }
      };

      button1.addActionListener(btnListener);
      button2.addActionListener(btnListener);

   }
}