3

如何使添加JPanel的内容在父级中可见JPanel

我正在使用 Netbeans 来设计我的 UI。

我有一个MainFrame.java,其中包含两个面板;即headerPanelbodyPanel

headerPanel我放了三个按钮,让它成为button1button2button3

此外,我还创建了三个单独的文件扩展、JPanel命名它panel1和.panel2panel3

然后我在构造函数中添加了所有三个bodypanel面板MainFrame.java

bodyPanel.add(panel1);
bodyPanel.add(panel2);
bodyPanel.add(panel3);

我希望在单击相应的按钮时,只有相应的面板应该出现在bodypanel大型机中,即如果我点击,button1那么panel1应该显示。

我已经在button1鼠标侦听器方法中尝试了以下代码:

bodyPanel.validate();
bodyPanel.getComponent(0).setVisible(true);

panel1不会出现。我这样做是因为面板中添加的组件被分配了索引。因此,首先我尝试获取组件,然后使其可见。它不起作用。

4

3 回答 3

6

使用CardLayout ,如此所示。

游戏视图 高分视图

于 2012-07-05T05:31:40.703 回答
3

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");
    }

}

}

输出

输出

于 2012-07-05T05:32:10.067 回答
2

使用卡片布局。下面是我写的一个 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();
    }
}
于 2012-07-05T05:55:00.897 回答