本人想做一个界面,有2个panel,同时只能一个显示,每一个panel上有一个button按钮能隐藏自己所在的panel并且显示另一个panel,而且每次显示都要在panel上新增一个button
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;public class TestAwt extends Panel {

private Panel pan1;
private Panel pan2;

public void init() {
pan1 = new Panel(new FlowLayout(FlowLayout.LEFT));
pan2 = new Panel(new FlowLayout(FlowLayout.LEFT));
this.setLayout(new BorderLayout());
this.add(pan1, BorderLayout.CENTER);
this.add(pan2, BorderLayout.CENTER);
Button bt1 = new Button("bt1");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pan2.add(new Button("aa"));
pan2.setVisible(true);
pan1.setVisible(false);
pan2.repaint();
System.out.println("pan2 : " + pan2.getComponentCount());
}
});
Button bt2 = new Button("bt2");
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
pan1.add(new Button("aa"));
pan2.setVisible(false);
pan1.setVisible(true);
pan1.repaint();
System.out.println("pan1 : " + pan1.getComponentCount());
}
});
pan1.add(bt1);
pan2.add(bt2);
System.out.println(this.getComponentCount());
}

public static void main(String[] args) {
JFrame frame = new JFrame();
TestAwt panel = new TestAwt();
panel.init();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(300, 400);
Dimension paneSize = frame.getSize();
Dimension screenSize = frame.getToolkit().getScreenSize();
frame.setLocation((screenSize.width - paneSize.width) / 2,
(screenSize.height - paneSize.height) / 2);
frame.setVisible(true);
frame.show();
}}

解决方案 »

  1.   


    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JFrame;public class TestAwt extends Panel {private Panel pan1;
    private Panel pan2;public void init() {
    pan1 = new Panel(new FlowLayout(FlowLayout.LEFT));
    pan2 = new Panel(new FlowLayout(FlowLayout.LEFT));
    this.setLayout(new BorderLayout());
    this.add(pan1, BorderLayout.CENTER);
    Button bt1 = new Button("bt1");
    bt1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    pan2.add(new Button("aa"));
    remove(pan1);
    add(pan2, BorderLayout.CENTER);
    validate();
    System.out.println("pan2 : " + pan2.getComponentCount());
    }
    });
    Button bt2 = new Button("bt2");
    bt2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    pan1.add(new Button("aa"));
    remove(pan2);
    add(pan1, BorderLayout.CENTER);
    validate();
    System.out.println("pan1 : " + pan1.getComponentCount());
    }
    });
    pan1.add(bt1);
    pan2.add(bt2);
    System.out.println(this.getComponentCount());
    }public static void main(String[] args) {
    JFrame frame = new JFrame();
    TestAwt panel = new TestAwt();
    panel.init();
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.setSize(300, 400);
    Dimension paneSize = frame.getSize();
    Dimension screenSize = frame.getToolkit().getScreenSize();
    frame.setLocation((screenSize.width - paneSize.width) / 2,
    (screenSize.height - paneSize.height) / 2);
    frame.setVisible(true);
    frame.show();
    }}
      

  2.   

    我觉得你的布局管理器摄制的有些问题。
    你应该先使用一个JPanel,将其Layout设置为CardLayout,然后将你要显示的另外两个JPanel加在其上,这样就方便多了。
    建议搂主看看swing里关于CardLayout的介绍,比你这样add,remove要好得多,代码量也要少得多。
      

  3.   

    红板上有一个蓝色按钮,蓝板上有一个红色按钮,点击按钮显示相应颜色的板
    import java.awt.*;
    import java.awt.event.*;public class CardLayoutExample extends Frame{
    Panel p1 = new Panel();
    Panel panelRed = new Panel();
    Panel panelBlue = new Panel();
    CardLayout card = new CardLayout();
        Button red = new Button("red");
    Button blue = new Button("blue"); public CardLayoutExample(){
            
    this.add(p1);
            p1.setLayout(card);
            p1.add(panelRed,"red");
            p1.add(panelBlue,"blue");
            panelRed.add(blue);
    blue.setBackground(Color.blue);
            panelRed.setBackground(Color.red);
    panelBlue.add(red);
            red.setBackground(Color.red);
            panelBlue.setBackground(Color.blue);        red.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
      card.show(p1,"red");
        }
        }); blue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
      card.show(p1,"blue");
        }
         });
            
        this.setSize(300, 300);        
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
    System.exit(0);
    }
    });
    this.setVisible(true);
      
    }
    public static void main(String args[]){     CardLayoutExample c1 = new CardLayoutExample(); } 
    }
      

  4.   

    "本人想做一个界面,有2个panel,同时只能一个显示,每一个panel上有一个button按钮能隐藏自己所在的panel并且显示另一个panel,"这个功能就是JTabbedPane所做的啊。只要再设计这个功能就可以了。  “而且每次显示都要在panel上新增一个button”确实不用那么复杂。