BorderLayout(隐藏方向)

 源代码:

package Test12;

import java.awt.*;

importjavax.swing.*;

importjava.awt.event.*;

 

public class Test02extends JFrame implements ActionListener{

privateBorderLayout f1;

privateContainer c;

privateStringname[]={"隐藏北方","隐藏南方","隐藏东方","隐藏西方","隐藏中央"};

privateJButton button[]=new JButton[name.length];

publicTest02()

{

super("Thistest02.java");

c=getContentPane();

f1=newBorderLayout(1,1);

c.setLayout(f1);

for(inti=0;i

{

button[i]=newJButton(name[i]);

button[i].addActionListener(this);

}

c.add(button[0],BorderLayout.NORTH);

c.add(button[1],BorderLayout.SOUTH);

c.add(button[2],BorderLayout.EAST);

c.add(button[3],BorderLayout.WEST);

c.add(button[4],BorderLayout.CENTER);

setSize(300,150);

setVisible(true);

}

publicvoid actionPerformed(ActionEvent e)

{

if(e.getSource()==button[0])

c.remove(button[0]);

if(e.getSource()==button[1])

c.remove(button[1]);

if(e.getSource()==button[2])

c.remove(button[2]);

if(e.getSource()==button[3])

c.remove(button[3]);

if(e.getSource()==button[4])

c.remove(button[4]);

c.validate();

}

publicstatic void main(String args[])

{

Test02app=new Test02();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

运行结果1:

 

 

运行结果2:

 

 

 

 

你可能感兴趣的:(JAVA)