当前位置: 代码迷 >> J2SE >> 帮小弟我看下代码,很简单的~
  详细解决方案

帮小弟我看下代码,很简单的~

热度:54   发布时间:2016-04-24 13:18:45.0
帮我看下代码,很简单的~~
在上面的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);

}
}


------解决方案--------------------
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;
  相关解决方案