在上面的JPanel显示四个Button,然后点击分别在下面的JPanel显示不同的布局,但就是不能实现,请高手指教!package pra3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ShowLayout extends JFrame implements ActionListener{
int count = 0;
protected JButton button1,button2,button3,button4,button5,button6; protected JLabel label;
protected JPanel panel;

JPanel pNorth = new JPanel();
JPanel pSouth = new JPanel();

public ShowLayout(){

setTitle("查看不同的Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,400);

    button1 = new JButton("观看FlowLayout布局");
button2 = new JButton("观看BorderLayout布局");
button3 = new JButton("观看BoxLayout布局");
button4 = new JButton("观看null布局");
button5 = new JButton("观看JSplitPane布局");
button6 = new JButton("观看JLayeredPane布局");

label = new JLabel("你还没有单击按钮");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this); Container cp = getContentPane();
cp.setLayout(new BorderLayout()); pNorth.add(button1);
pNorth.add(button2);
pNorth.add(button3);
pNorth.add(button4);
pNorth.add(button5);
pNorth.add(button6);
cp.add(pNorth,"North");

cp.add(pSouth);

pSouth.add(label);
}
public void showTest(String type){
    //pSouth.removeAll();
    //this.pSouth.validate();
button1 = new JButton("Test01");
button2 = new JButton("Test02");
button3 = new JButton("Test03");
button4 = new JButton("Test04");
button5 = new JButton("Test05");
button6 = new JButton("Test06");

label.setText(type);
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
pSouth.add(button4);
pSouth.add(button5);
pSouth.add(button6);
}
public void actionPerformed(ActionEvent e){ Object source = e.getSource();
//pSouth.removeAll();
if(source == button1) {
System.out.println("test1");
pSouth.setLayout(new FlowLayout());
    showTest("FlowLayout布局");
    
    
}

else if(source == button2) {

pSouth.setLayout(new BorderLayout());
pSouth.add(new Button("Test01"),BorderLayout.NORTH);
pSouth.add(new Button("Test02"),BorderLayout.SOUTH);
System.out.println("test2");
//showTest("BorderLayout布局"); }
else if(source == button3){
pSouth.setLayout(new BoxLayout(pSouth, BoxLayout.LINE_AXIS));//找不到BoxLayout布局
showTest("BoxLayout布局");
}
else if (source == button4){
pSouth.setLayout( null);
showTest("null布局");
}
/*else if (source == button5) {
JSplitPane();
showTest("JSplitPane布局");
}
else if (source == button6){
//pSouth.set
showTest("观看JLayeredPane布局");
}*/

}
public static void main(String args[]){
ShowLayout frame = new ShowLayout();
frame.setVisible(true);

}
}

解决方案 »

  1.   

    package test;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Test extends JFrame implements ActionListener {
    protected JButton button1, button2, button3, button4, button5, button6; protected JLabel label; protected JPanel panel; JPanel pNorth = new JPanel(); JPanel pSouth = new JPanel(); public Test() { setTitle("查看不同的Layout");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800, 400); button1 = new JButton("观看FlowLayout布局");
    button2 = new JButton("观看BorderLayout布局");
    button3 = new JButton("观看BoxLayout布局");
        button4 = new JButton("观看null布局");
    // button5 = new JButton("观看JSplitPane布局");
    // button6 = new JButton("观看JLayeredPane布局"); label = new JLabel("你还没有单击按钮"); button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    // button5.addActionListener(this);
    // button6.addActionListener(this); Container cp = getContentPane();
    cp.setLayout(new BorderLayout()); pNorth.add(button1);
    pNorth.add(button2);
    pNorth.add(button3);
        pNorth.add(button4);
    pNorth.add(label); // pNorth.add(button5);
    // pNorth.add(button6); cp.add(pNorth, "North");
    cp.add(pSouth);
    } public void addButton() {
    JButton button11 = new JButton("Test11");
    JButton button12 = new JButton("Test12");
    JButton button13 = new JButton("Test13");
    JButton button14 = new JButton("Test14");
    JButton button15 = new JButton("Test15");
    JButton button16 = new JButton("Test16"); pSouth.add(button11);
    pSouth.add(button12);
    pSouth.add(button13);
    pSouth.add(button14);
    pSouth.add(button15);
    pSouth.add(button16);
    } public void showTest(String type) {
    label.setText(type);
    } public void actionPerformed(ActionEvent e) { Object source = e.getSource();
    if (source == button1) {
    pSouth.removeAll();
    pSouth.repaint();
    pSouth.validate();
    addButton();
    pSouth.setLayout(new FlowLayout());
    showTest("FlowLayout布局");
    } else if (source == button2) {
    pSouth.removeAll();
    pSouth.repaint();
    pSouth.validate();
    // addButton();
    pSouth.setLayout(new BorderLayout());
    pSouth.add(new Button("Test01"), BorderLayout.NORTH);
    pSouth.add(new Button("Test02"), BorderLayout.SOUTH); showTest("BorderLayout布局"); } else if (source == button3) {
    pSouth.removeAll();
    pSouth.repaint();
    pSouth.validate();
    addButton();
    pSouth.setLayout(new BoxLayout(pSouth, BoxLayout.LINE_AXIS));// 找不到BoxLayout布局
    showTest("BoxLayout布局");
    }else if (source == button4) {
    pSouth.removeAll();
    pSouth.repaint();
    pSouth.validate();
    addButton();
    pSouth.setLayout(null);// 找不到BoxLayout布局
    showTest("Null布局");
    } } public static void main(String args[]) {
    Test frame = new Test();
    frame.setVisible(true); }
    }对另外的布局不清楚,所以没加上,也不知道是不是你想要的结果。