I'm trying to switch between displaying different JPanels in BorderLayout.CENTER
in a JFrame. The JPanels each contain a JTextField and a JTextPane. Calling revalidate()
and repaint()
on the JFrame, I get a very strange result: the first time I switch to secondPanel
by clicking secondButton
, everything works as expected. Then when I try to switch back using firstButton
, nothing happens. It only switches back when I click secondButton
again, and after that it works just as expected, except in reverse: clicking firstButton
displays secondPanel
and clicking secondButton
displays firstPanel
.
Calling revalidate()
and repaint()
on the JPanel to be displayed instead, everything works fine, until I click either text area. With every click, it's like part of the text "falls off" like it was just a facade, revealing the text of the corresponding area of the other JPanel underneath. If I type something, the text changes instantly to that of the other JPanel, and whatever I typed is added to it even after switching JPanels. It's really weird and hard to describe properly, so just see for yourselves.
I have three questions: 1. what the hell, 2. why the hell, and 3. how do I fix it?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame {
class MyActionListener implements ActionListener {
JPanel panel;
MyActionListener(JPanel _panel) {
panel = _panel;
}
@Override
public void actionPerformed(ActionEvent ae) {
MyFrame.this.add(panel, BorderLayout.CENTER);
//MyFrame.this.revalidate();
//MyFrame.this.repaint();
panel.revalidate();
panel.repaint();
}
}
JPanel buttonPanel = new JPanel();
JButton firstButton = new JButton("first");
JPanel firstPanel = new JPanel();
JTextPane firstPane = new JTextPane();
JTextField firstField = new JTextField("first field");
JButton secondButton = new JButton("second");
JPanel secondPanel = new JPanel();
JTextPane secondPane = new JTextPane();
JTextField secondField = new JTextField("second field");
MyFrame() {
firstPane.setText("first pane");
firstPanel.setLayout(new BorderLayout());
firstPanel.add(firstPane, BorderLayout.CENTER);
firstPanel.add(firstField, BorderLayout.SOUTH);
firstButton.addActionListener(new MyActionListener(firstPanel));
secondPane.setText("second pane");
secondPanel.setLayout(new BorderLayout());
secondPanel.add(secondPane, BorderLayout.CENTER);
secondPanel.add(secondField, BorderLayout.SOUTH);
secondButton.addActionListener(new MyActionListener(secondPanel));
buttonPanel.setLayout(new GridLayout(0,1));
buttonPanel.add(firstButton);
buttonPanel.add(secondButton);
add(buttonPanel, BorderLayout.WEST);
add(firstPanel, BorderLayout.CENTER);
setSize(new Dimension(300,300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
how do I fix it?
Use a CardLayout
. See the section from the Swing tutorial on How to Use CardLayout for more information and examples.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.