我知道您的答案已被接受,但我必须插话,因为我觉得虽然答案在技术上是正确的,但它可能不是最佳解决方案。
请注意,首先,您的问题与 .validate() 与 .revalidate() 的关系不大,而与您调用它的位置有关。它必须在所有更改完成后调用,这意味着在所有组件都被删除和添加之后。
还请注意,在您的容器上使用 repaint() 并在调用 revalidate() 之后调用它通常是件好事,尤其是在您删除组件时,因为这将有助于清除任何潜在的“脏”像素在您的 GUI 中。
现在我回答的重点是:最好不要推出自己的解决方案,而是使用 GUI 库为您提供的更高级别的工具,这里是 CardLayout(教程:CardLayout Tutorial)。使用此工具,您可以轻松地在“卡片”组件中向前或向后滚动,此处 JPanel 使用 .previous(...) 和 .next(...) 方法,或者通过调用 .show(...) 方法以灵活的方式交换组件。布局还将调整使用该布局并保存“卡片”组件的容器的大小,以确保所有卡片都适合。
.next(...)、.previous(...) 和 .show(...) 的使用示例可以在下面的可编译和可运行代码中看到。另外,请看一下 cmets:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.*;
public class SwapPanels extends JPanel {
private static final long serialVersionUID = 1L;
private CardLayout cardLayout = new CardLayout(); // the layout
private JPanel cardHolderPanel = new JPanel(cardLayout); // the JPanel that holds the cards
// a combo box and model for allowing random-access swapping
private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>();
private JComboBox<String> swapPanelCombo = new JComboBox<>(comboModel);
public SwapPanels() {
// listener added to combobox to allow swapping of panels
swapPanelCombo.addActionListener(e -> swapPanel());
JButton previousBtn = new JButton("Previous");
previousBtn.addActionListener(e -> cardLayout.previous(cardHolderPanel));
JButton nextBtn = new JButton("Next");
nextBtn.addActionListener(e -> cardLayout.next(cardHolderPanel));
// panel to hold buttons and combo box
JPanel controlPanel = new JPanel();
controlPanel.add(previousBtn);
controlPanel.add(nextBtn);
controlPanel.add(swapPanelCombo);
// create and add our "card" JPanels, showing different colors
addColorPanel("White", Color.WHITE);
addColorPanel("Red", Color.RED);
addColorPanel("Orange", Color.ORANGE);
addColorPanel("Yellow", Color.YELLOW);
addColorPanel("Green", Color.GREEN);
addColorPanel("Blue", Color.BLUE);
addColorPanel("Cyan", Color.CYAN);
// add components to the main *this* JPanel
setPreferredSize(new Dimension(600, 450));
setLayout(new BorderLayout());
add(cardHolderPanel);
add(controlPanel, BorderLayout.PAGE_END);
}
private void addColorPanel(String text, Color color) {
// create color JPanel
JPanel colorPanel = new JPanel();
colorPanel.setBackground(color);
// label to display color name
JLabel label = new JLabel(text, SwingConstants.CENTER);
// make label big
label.setFont(label.getFont().deriveFont(Font.BOLD, 24f));
// place label in the center of the color JPanel
colorPanel.setLayout(new GridBagLayout());
colorPanel.add(label);
// add text to your jcombo box as well
comboModel.addElement(text);
// add color panel to the card-holder JPanel
cardHolderPanel.add(colorPanel, text);
}
private void swapPanel() {
// get combo box's text
String text = (String) swapPanelCombo.getSelectedItem();
if (text != null) {
// use the text to swap jpanels
cardLayout.show(cardHolderPanel, text);
}
}
// be sure to show our GUI on the Swing event thread
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
SwapPanels mainPanel = new SwapPanels();
JFrame frame = new JFrame("Swap Panels");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}