![](/img/trans.png)
[英]How can I replace one JPanel with another JPanel in the same position?
[英]How can I replace one of two JPanels with another JPanel in Java?
我設計了一個歡迎屏幕界面,其中一個JFrame包含兩個JPanels(右側為JPanel1,左側為JPanel2)。 左側的按鈕用於切換JPanel1中的面板。 我想按一個按鈕,用另一個JPanel替換JPanel1內容,但我不知道如何。 請幫忙。
這是一個非常簡單的示例,它應近似於您的描述。 在左側,我們有一個擁抱按鈕可以切換右側面板的內容。 在右邊,您有一個帶有給定邊框和標簽的面板。 當您按下按鈕時,右側的內容將與另一個面板交換。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestCardLayout2 {
protected void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel leftPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Left panel");
leftPanel.add(label, BorderLayout.NORTH);
JButton button = new JButton("Toggle right panel");
leftPanel.add(button);
frame.add(leftPanel, BorderLayout.WEST);
final CardLayout cardLayout = new CardLayout();
final JPanel rightPanel = new JPanel(cardLayout);
rightPanel.setPreferredSize(new Dimension(200, 500));
JPanel rightPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
rightPanel1.setBorder(BorderFactory.createLineBorder(Color.RED));
JPanel rightPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
rightPanel2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JLabel label1 = new JLabel("Right panel 1 with a red border");
JLabel label2 = new JLabel("Right panel 2 with a blue borer");
rightPanel1.add(label1);
rightPanel2.add(label2);
rightPanel.add(rightPanel1, "panel1");
rightPanel.add(rightPanel2, "panel2");
frame.add(rightPanel, BorderLayout.EAST);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(rightPanel);
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestCardLayout2().initUI();
}
});
}
}
CardLayout
的替代方法是JRootPane
及其JRootPane.setContentPane()
方法。 這是一個例子:
final JPanel panel1 = ...;
final JPanel panel2 = ...;
boolean showingPanel1 = true;
final JRootPane rootPane = new JRootPane();
rootPane.setContentPane(panel1);
JButton switchButton = new JButton("Switch");
switchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (showingPanel1) {
rootPane.setContentPane(panel2);
} else {
rootPane.setContentPane(panel1);
}
showingPanel = !showingPanel;
}
});
將rootPane
和switchButton
組件添加到您的窗口中,然后單擊switchButton
將切換面板。
這是一個教程 。 您應該主要關注JRootPane.setContentPane
,本教程中的其他內容JRootPane.setContentPane
。
我發現的最佳答案是,我將只創建一個JFrame,然后使一個大的JPanel包含兩個JPanels(JPanelLeft包含按鈕,而JPanelRight包含按鈕的作用),然后為每個JButton復制主JPanel。 當我按下任何按鈕時,我將(JFrame.getContentPane.removeAll)刪除舊的JPanel,然后再刪除(JFrame.getContentPane.Add(NewJPanel)。
這對我有用,並保持我的設計不變。 謝謝大家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.