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
Java swing; How to toggle panel's visibility?
提问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);
}
}