[英]Make an added JPanel visible inside a parent JPanel
如何使添加的JPanel
在父JPanel
可見?
我正在使用 Netbeans 來設計我的 UI。
我有一個MainFrame.java
,它包含兩個面板; 即headerPanel
和bodyPanel
。
在headerPanel
我放置了三個按鈕,分別是button1
、 button2
和button3
。
我還創建了三個擴展JPanel
單獨文件,將其命名為panel1
、 panel2
和panel3
。
然后我在構造函數的MainFrame.java
中的bodypanel
中添加了所有三個面板。
bodyPanel.add(panel1);
bodyPanel.add(panel2);
bodyPanel.add(panel3);
我希望在單擊相應的按鈕時,只有相應的面板應出現在大型機的bodypanel
中,即,如果我單擊button1
則應顯示panel1
。
我已經在button1
鼠標偵聽器方法中嘗試了以下代碼:
bodyPanel.validate();
bodyPanel.getComponent(0).setVisible(true);
但是panel1
沒有出現。 我這樣做是因為面板中添加的組件被分配了索引。 所以首先我嘗試獲取組件,然后使其可見。 這沒用。
使用CardLayout ,如圖所示這里。
您的要求完全由 CARD LAYOUT 填寫,請參閱此示例鏈接
您的問題案例的完美代碼是
package panels.examples;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
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;
public class MainFrame extends JFrame implements ActionListener
{
JPanel headerPanel;
JPanel bodyPanel;
JPanel panel1,panel2,panel3;
JButton button1,button2,button3;
Container con;
CardLayout clayout;
public MainFrame()
{
//con=getContentPane();
clayout=new CardLayout();
headerPanel=new JPanel();
bodyPanel=new JPanel(clayout);
button1=new JButton("button1");
button2=new JButton("button2");
button3=new JButton("button3");
//add three buttons to headerPanel
headerPanel.add(button1);
headerPanel.add(button2);
headerPanel.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
panel1=new JPanel();
panel1.add(new JLabel("Panel1"));
panel1.setBackground(Color.pink);
panel2=new JPanel();
panel2.add(new JLabel("Panel2"));
panel2.setBackground(Color.gray);
panel3=new JPanel();
panel3.add(new JLabel("Panel3"));
//add above three panels to bodyPanel
bodyPanel.add(panel1,"one");
bodyPanel.add(panel2,"two");
bodyPanel.add(panel3,"three");
setLayout(new BorderLayout());
setSize(600,450);
add(headerPanel,BorderLayout.NORTH);
add(bodyPanel,BorderLayout.CENTER);
// headerPanel.setBounds(0,0,600,100);
bodyPanel.setBounds(0,100, 600, 500);
setVisible(true);
}
public static void main(String args[])
{
new MainFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1)
{
clayout.show(bodyPanel, "one");
}
else if(e.getSource()==button2)
{
clayout.show(bodyPanel, "two");
}
else if(e.getSource()==button3)
{
clayout.show(bodyPanel, "three");
}
}
}
輸出
使用 CardLayout。 下面是我寫的一個 Helper 類。 希望能幫助到你。
import java.awt.CardLayout;
import javax.swing.JPanel;
/**
*
* @author Dchan(Dulitha Wijewantha)
*
* This class is used to switch Cards in a CardLayout
*
* @version $Revision: 1.0 $
*/
public class CardLayoutHelper {
private JPanel panel;
private CardLayout layout;
/**
*
* @param panel JPanel
*/
public CardLayoutHelper(JPanel panel) {
this.panel = panel;
this.layout = (CardLayout) this.panel.getLayout();
}
public CardLayoutHelper(JPanel panel, JPanel... panels){
this(panel);
for (int i = 0; i < panels.length; i++) {
JPanel jPanel = panels[i];
panel.add(jPanel.getName(), jPanel);
}
}
/**
*
* @param currentPanel
* - The panel that will be switched into the view
*/
public void switchPanel(JPanel currentPanel) {
panel.removeAll();
panel.add(currentPanel, currentPanel.getName());
layout.show(panel, currentPanel.getName());
panel.revalidate();
panel.repaint();
}
public void switchPanel(String name){
layout.show(panel, name);
panel.revalidate();
panel.repaint();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.