边框布局:
package swing_BorderLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing3 extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
JButton jb1=new JButton("东");
JButton jb2=new JButton("南");
JButton jb3=new JButton("西");
JButton jb4=new JButton("北");
JButton jb5=new JButton("中");
public swing3()
{
this.setTitle("创建边框布局");
jp.setLayout(newBorderLayout(5,5));
//jp.setLayout(newBorderLayout());//默认
//jp.setLayout(newBorderLayout(int Hgap,int Vgap));
jb1.setMnemonic('d');
jb1.addActionListener(this);
jb2.setMnemonic('s');
jb2.addActionListener(this);
jb3.setMnemonic('a');
jb3.addActionListener(this);
jb4.setMnemonic('w');
jb4.addActionListener(this);
jb5.setMnemonic('q');
jb5.addActionListener(this);
jp.add(jb1,BorderLayout.EAST);
jp.add(jb2,BorderLayout.SOUTH);
jp.add(jb3,BorderLayout.WEST);
jp.add(jb4,BorderLayout.NORTH);
jp.add(jb5,BorderLayout.CENTER);
this.add(jp);
this.setBounds(300,250,300,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
jb5.setText("EAST");
}
elseif(e.getSource()==jb2)
{
jb5.setText("SOUTH");
}
elseif(e.getSource()==jb3)
{
jb5.setText("WEST");
}
elseif(e.getSource()==jb4)
{
jb5.setText("NORTH");
}
elseif(e.getSource()==jb5)
{
jb5.setText("CENTER");
}
}
}
public class C
{
public static void main(String[] args)
{
swing3 sb=new swing3();
}
}
卡片布局:
package swing_CardLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing5 extends JFrame implements ActionListener
{
JPanel jp=new JPanel();
CardLayout cl=newCardLayout();//卡片布局需要通过类来创建
//CardLayout cl=new CardLayout();
//CardLayout cl=newCardLayout(int控件和容器边界水平距离, int 控件和容器边界垂直距离, int Hgap, intVgap);
JButton jbf=new JButton("第一个");
JButton jbl=new JButton("最后一个");
JButton jbn=new JButton("下一个");
JButton jbp=new JButton("上一个");
JButton jbc=new JButton("中间那个");
public swing5()
{
this.setTitle("创建卡片布局");
this.setLayout(null);//窗口的布局设为空布局
jbf.setBounds(120, 40, 100,20);
jbl.setBounds(120, 70, 100,20);
jbn.setBounds(120, 100, 100,20);
jbp.setBounds(120, 130, 100,20);
jbc.setBounds(120, 160, 100,20);
this.add(jbf);
this.add(jbl);
this.add(jbn);
this.add(jbp);
this.add(jbc);
jbf.addActionListener(this);
jbl.addActionListener(this);
jbn.addActionListener(this);
jbp.addActionListener(this);
jbc.addActionListener(this);
jp.setBounds(10,40,100,100);
jp.setLayout(cl);
for(int i=0;i<50; i++)
{
JButtonjb=new JButton("按钮"+i);
jp.add(jb,""+i);
}
this.add(jp);
this.setBounds(200, 200, 300,220);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbf)
{
cl.first(jp);
}
elseif(e.getSource()==jbl)
{
cl.last(jp);
}
elseif(e.getSource()==jbn)
{
cl.next(jp);
}
elseif(e.getSource()==jbp)
{
cl.previous(jp);
}
elseif(e.getSource()==jbc)
{
cl.show(jp,"25");
}
}
}
public class E
{
public static void main(String[]args)
{
swing5 sc=new swing5();
}
}
流布局:
package swing_FlowLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class swing1 extends JFrame implements ActionListener
{
int i=0;
JPanel jp=new JPanel();
JButton jb=new JButton("创建按钮");
public swing1()
{
this.setTitle("创建流布局管理器");
jp.setLayout(newFlowLayout(1,5,5));
//this.setLayout(newFlowLayout());//默认为居中对齐,水平和垂直间距为5
//this.setLayout(newFlowLayout((int)指定对齐方式));
//this.setLayout(newFlow Layout((int)指定对齐方式,(int)控件间水平间距,(int)控件间垂直间距));
jb.setMnemonic('a');
jp.add(jb);
jb.addActionListener(this);
jp.setToolTipText("提示而已");
this.add(jp);
this.setBounds(300,250,300,200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
++i;
JButton jbi=newJButton("按钮"+i);
jp.add(jbi);
this.show(true);
}
}
public class A
{
public static void main(String[]args)
{
swing1 sf=new swing1();
}
}
网格布局:
package swing_GridLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing2 extends JFrame
{
JPanel jp=new JPanel();
JButton jb=new JButton("创建按钮");
int i=0;
public swing2()
{
jp.setLayout(newGridLayout(3,2));//注意!!!是面板设置布局流
//jp.setLayout(newGridLayout());//默认行默认列
//jp.setLayout(newGridLayout(int Rows, int Columns));//设置行和列,常用
//jp.setLayout(newGridLayout(int Rows, int Columns, int Hgap, intVgap));//设置行和列,控件间水平和垂直间距
jb.setMnemonic('a');
jb.addActionListener(newActionListener()
{
public voidactionPerformed(ActionEvent e)
{
++i;
JButtonjbi=new JButton("按钮"+i);
jp.add(jbi);
swing2.this.show(true);//注意!!!是窗口中的对象刷新
}
});
jp.add(jb);
this.add(jp);
this.setTitle("创建网格布局管理器");
this.setBounds(300,250,300,200);
this.setVisible(true);
}
}
public class B
{
public static void main(String[] args)
{
swing2 sg=new swing2();
}
}
空布局:
package swing_null_Layout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class swing4 extends JFrame
{
JPanel jp=new JPanel();
JButton jb1=new JButton("动");
JLabel jl=new JLabel("标签");
int i=10;
int j=10;
public swing4()
{
this.setTitle("空布局管理器");
jp.setLayout(null);
jb1.setBounds(50, 50, 70,20);
jl.setBounds(150, 70, 100,20);
jb1.addActionListener(newActionListener()
{
public voidactionPerformed(ActionEvent e)
{
i+=10;
j+=5;
jb1.setBounds(50,50, 70+i, 20+j);
jl.setBounds((150+i),(70+j), 100, 20);//空布局中的控件可以改变大小
}
});
jp.add(jb1);
jp.add(jl);
this.add(jp);
this.setBounds(300, 250, 300,200);
this.setVisible(true);
}
}
public class D
{
public static void main(String[]args)
{
swing4 sn=new swing4();
}
}