更新时间:2023-12-05 23:19:58
除了您不发布完整源代码或 SSCCE 我无法完全测试.
Well besides the fact that you dont post your full source or an SSCCE I cannot fully test.
查看代码,您正在使用removeAll()
方法在单个JFrame
/容器上的多个JPanel
之间切换.
Looking at your code you are using the removeAll()
method to switch between multiple JPanel
s on a single JFrame
/container.
我看到的错误是逻辑上的,因为您试图在另一个JPanel
而不是实际的JFrame
中的JPanel
之间切换.
The mistake I see you are making though is in the logic, as you attempt to switch between JPanel
s within another JPanel
rather than within your actual JFrame
.
请参阅我创建的这个小示例(允许用户在一个实现JPanel
之间切换". wikipedia.org/wiki/Singleton_pattern"rel =" nofollow noreferrer>单一设计模式,可以很容易地进行更改,以便将MainGUI
类的实例传递给每个JPanel
和MainGUI
生成的对象,从而使MainGUI
s swapPanel(JPanel p)
方法):
See this small example I created (allows a user to alternate between 2 JPanel
s on a single JFrame
/container which implements a Singleton design pattern, can easily be changed for passing instance of MainGUI
class to each JPanel
the MainGUI
spawns so that the JPanel
s will have access to the single MainGUI
s swapPanel(JPanel p)
method):
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
MainGUI mainGUI = MainGUI.getInstance();
}
}
class MainGUI {
private JFrame frame;
private static class SingletonHolder {
public static final MainGUI INSTANCE = new MainGUI();
}
public static MainGUI getInstance() {
return SingletonHolder.INSTANCE;
}
private MainGUI() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame = createJFrame();
frame.setVisible(true);
}
});
}
private JFrame createJFrame() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Test");
frame.add(new Panel1());//initial Jpanel to display
frame.pack();
return frame;
}
public void swapPanel(JPanel p) {
frame.getContentPane().removeAll();
frame.add(p);
frame.pack();
frame.revalidate();
frame.repaint();
}
}
class Panel1 extends JPanel {
public Panel1() {
JLabel labelPanel1 = new JLabel("Panel 1");
JButton switchPanelButton = new JButton("Goto Panel 2");
switchPanelButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
MainGUI.getInstance().swapPanel(new Panel2());
}
});
add(labelPanel1);
add(switchPanelButton);
}
}
class Panel2 extends JPanel {
public Panel2() {
JLabel labelPanel1 = new JLabel("Panel 2");
JButton switchPanelButton = new JButton("Goto Panel 1");
switchPanelButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
MainGUI.getInstance().swapPanel(new Panel1());
}
});
add(labelPanel1);
add(switchPanelButton);
}
}
上述的更高级替代方法是 CardLayout
CardLayout类管理共享相同显示空间的两个或更多组件(通常是JPanel实例).:
An higher-level alternative to the above is a CardLayout
The CardLayout class manages two or more components (usually JPanel instances) that share the same display space.:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
private final static String PANEL1 = "panel 1";
private final static String PANEL2 = "panel 2";
public Test() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
private void initComponents() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Panel 1"));
JPanel panel2 = new JPanel();
panel2.add(new JLabel("Panel 2"));
//Create the panel that contains the "cards".
final JPanel cards = new JPanel(new CardLayout());
cards.add(panel1, PANEL1);
cards.add(panel2, PANEL2);
//create button to allow chnage to next card
JButton buttonNext = new JButton(">");
buttonNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.next(cards);
}
});
//create button to allow chnage to previous card
JButton buttonPrev = new JButton("<");
buttonPrev.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
CardLayout cl = (CardLayout) (cards.getLayout());//get cards
cl.previous(cards);
}
});
//create panel to hold buttons which will allow switching between cards
JPanel buttonPanel = new JPanel();
buttonPanel.add(buttonPrev);
buttonPanel.add(buttonNext);
frame.add(cards);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}