1.JPanel(面板):
package ArrayListStr;
import java.awt.*;
import javax.swing.*;
public class L5_6 extends JFrame
{
JPanel mb1,mb2;
JButton an1,an2,an3,an4,an5;
public static void main(String []args)
{
L5_6 MyL56=new L5_6();
}
L5_6()
{
mb1=new JPanel();//JPanel默认是流式布局
mb2=new JPanel();
an1=new JButton("可乐");
an2=new JButton("红茶");
an3=new JButton("绿茶");
an4=new JButton("啤酒");
an5=new JButton("矿泉水");
mb1.add(an1);
mb1.add(an2);
mb2.add(an3);
mb2.add(an4);
this.add(mb1,BorderLayout.SOUTH);
this.add(mb2,BorderLayout.NORTH);
this.add(an5);
this.setTitle("布局综合应用");
this.setSize(350, 200);
this.setLocation(200, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
2.边界布局:
package ArrayListStr;
import javax.swing.*;
import java.awt.*;
public class L5_3 extends JFrame
{
JButton an1,an2,an3,an4,an5;
public static void main(String[] args)
{
L5_3 lx=new L5_3();
}
public L5_3()
{
//创建件组件
an1=new JButton("东方");
an2=new JButton("西方");
an3=new JButton("南方");
an4=new JButton("北方");
an5=new JButton("中部");
//添加组件
this.add(an1,BorderLayout.EAST);//括号中的参数固定的,顺序不能改变
this.add(an2,BorderLayout.WEST);
this.add(an3,BorderLayout.SOUTH);
this.add(an4,BorderLayout.NORTH);
this.add(an5,BorderLayout.CENTER);
//如果不是五个按钮全部添加,则会以扩充中部为主进行填充但中部不会被其它四个填充。
this.setTitle("边界布局BorderLayout");
this.setSize(380,320);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
/*总结:
* 1.继承JFram类
* 2.在最上方定义组件
* 3.在构造方法中添加组件
* 4.在构造方法中添加组件
* 5.设置窗体属性
* 6.显示窗体
* 7.在主函数中创建组件
* */
3.单选安钮和复选框:
package ArrayListStr;
import java.awt.*;
import javax.swing.*;
public class L5_8 extends JFrame{
JPanel mb1,mb2,mb3;
JCheckBox fxk1,fxk2,fxk3;
JButton an1,an2;
JRadioButton dx1,dx2;
JLabel dq1,dq2;
ButtonGroup dxz;//把单选选项放进一个组里
public static void main(String []args)
{
L5_8 MyL5=new L5_8();
}
L5_8()
{
mb1=new JPanel();
mb2=new JPanel();
mb3=new JPanel();
fxk1=new JCheckBox("音乐");
fxk2=new JCheckBox("体育");
fxk3=new JCheckBox("文艺");
dq1=new JLabel("特长");
dq2=new JLabel("性别");
dx1=new JRadioButton("男");
dx2=new JRadioButton("女");
an1=new JButton("注册");
an2=new JButton("消取");
dxz=new ButtonGroup();
dxz.add(dx1);dxz.add(dx2);
this.setLayout(new GridLayout(3,1));
mb1.add(dq1);mb1.add(fxk1);mb1.add(fxk2);mb1.add(fxk3);
mb2.add(dq2);mb2.add(dx1);mb2.add(dx2);
mb3.add(an1);mb3.add(an2);
this.add(mb1);
this.add(mb2);
this.add(mb3);
this.setTitle("用户注册");
this.setSize(350,200);
this.setLocation(300,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
4.流式布局:
package ArrayListStr;
import java.awt.*;
import javax.swing.*;
public class L5_4 extends JFrame{
JButton[] an={null,null,null,null,null,null,null,null};
public static void main(String[] args)
{
L5_4 MyFlowLayout=new L5_4();
}
public L5_4()
{
an[0]=new JButton("话梅");
an[1]=new JButton("果脯");
an[2]=new JButton("薯片");
an[3]=new JButton("饼干");
an[4]=new JButton("巧克力");
an[5]=new JButton("腰果");
an[6]=new JButton("锅巴");
an[7]=new JButton("开心果");
//this.setLayout(new FlowLayout());//居中
this.setLayout(new FlowLayout(FlowLayout.LEFT));//向左对齐
//this.setLayout(new FlowLayout(FlowLayout.RIGHT));//向右对齐
for(int n=0;n<an.length;n++)
this.add(an[n]);
this.setTitle("流式布局FlowLayout");
this.setSize(380,120);
this.setLocation(200, 200);
this.setResizable(false);//锁定窗体尺寸
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
5.网格线:
package ArrayListStr;
import javax.swing.*;
import java.awt.*;
public class L5_5 extends JFrame
{
JButton []an={null,null,null,null,null,null,null,null,null};
public static void main(String[] args)
{
L5_5 MyGridLayout=new L5_5();
}
public L5_5()
{
an[0]=new JButton("话梅");
an[1]=new JButton("饼干");
an[2]=new JButton("巧克力");
an[3]=new JButton("腰果");
an[4]=new JButton("锅巴");
an[5]=new JButton("开心果");
an[6]=new JButton("杏仁");
an[7]=new JButton("薯片");
an[8]=new JButton("果脯");
this.setLayout(new GridLayout(3,3));
for(int i=0;i<an.length;i++) this.add(an[i]);
this.setTitle("网格布局GridLayout");
this.setSize(380,320);
this.setLocation(200,200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
6.文本框和标签:
package ArrayListStr;
import java.awt.*;
import javax.swing.*;
public class L5_7 extends JFrame
{
JPanel mb1,mb2,mb3;
JButton an1,an2;
JLabel bq1,bq2;
JTextField wbk;
JPasswordField mmk;
public static void main(String []args)
{
L5_7 MyL5=new L5_7();
}
L5_7()
{
mb1=new JPanel();
mb2=new JPanel();
mb3=new JPanel();
bq1=new JLabel("用户名");
bq2=new JLabel("密 码");
an1=new JButton("登陆");
an2=new JButton("取消");
wbk=new JTextField(10);
mmk=new JPasswordField(10);
this.setLayout(new GridLayout(3,1));
mb1.add(bq1);mb1.add(wbk);
mb2.add(bq2);mb2.add(mmk);
mb3.add(an1);mb3.add(an2);
this.add(mb1);
this.add(mb2);
this.add(mb3);
this.setTitle("用户登录");
this.setSize(300,150);
this.setLocation(300,280);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
8.下拉列表:
package WindowsJFrame;
import java.awt.*;
import javax.swing.*;
public class L5_91 extends JFrame{
JPanel mb1,mb2;
JLabel bq1,bq2;
JComboBox xlk;
JList lb;
JScrollPane gd;
public static void main(String []args)
{
L5_91 MyL5=new L5_91();
}
public L5_91()
{
mb1=new JPanel();
mb2=new JPanel();
bq1=new JLabel("籍贯");
bq2=new JLabel("学历");
String []jg={"北京","上海","广州","深圳"};
xlk=new JComboBox(jg);
String []xl={"高中","大专","本科","硕士","博士"};
lb=new JList(xl);
lb.setVisibleRowCount(3);
gd=new JScrollPane(lb);
this.setLayout(new GridLayout(2,1));
mb1.add(bq1);mb1.add(xlk);
mb2.add(bq2);mb2.add(gd);
this.add(mb1);
this.add(mb2);
this.setTitle("用户调查");
this.setSize(200,190);
this.setLocation(1920/2,1080/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}