java Java摇摆; 如何切换面板的可见性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5600051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:50:54  来源:igfitidea点击:

Java swing; How to toggle panel's visibility?

javaswingtogglevisibilityjpanel

提问by Opoe

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

我制作了此代码以使用按钮在面板 1 和面板 2 中导航。

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

(button1 和 button2)但是当我运行我的代码时,框架保持为空。

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

以这种方式在 panel1 和 panel2 之间切换?首先从 panel1 开始

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

提前致谢

回答by Eric Milas

Use a layout manager.

使用布局管理器。

frame.setLayout(new FlowLayout());

frame.setLayout(new FlowLayout());

回答by Hovercraft Full Of Eels

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.

另一种有用的方法是使用 CardLayout 并将两个 JPanel 添加到使用此 CardLayout 的容器中。然后,您可以通过调用 CardLayout 方法轻松地交换视图。

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

   }
}