当幸福莱敲门

导航

 

窗体组件的第一种写法

 1 /**
 2  * JFrame 界面开发
 3  */
 4 package com.test2;
 5 //引包
 6 
 7 import javax.swing.JButton;
 8 import javax.swing.JFrame;
 9 
10 public class day13 {
11     public static void main(String []args){
12         //JFrame 是一个顶层容器类(可以添加其它swing组件的类)
13         JFrame jFrame=new JFrame(); //把容器实例化
14         
15         //给窗口设置标题
16         jFrame.setTitle("这是第一个窗体程序");
17         
18         //给窗口设置大小
19         jFrame.setSize(400, 400);
20         
21         //设置窗口初始位置
22         jFrame.setLocation(800, 300);
23         
24         //设置当关闭窗口的时候,保证JVM也退出
25         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26         //把窗口显示出来
27         jFrame.setVisible(true);
28     }
29 }

窗体组件的第二种写法

 1 /**
 2  * JFrame 界面开发
 3  */
 4 package com.test2;
 5 //引包
 6 
 7 import javax.swing.JButton;
 8 import javax.swing.JFrame;
 9 
10 public class day13 extends JFrame{
11     //把需要添加的组件添加在这里
12     JButton jb1=null;
13     public static void main(String []args){
14         day13 d1=new day13(); //实例化面板
15     }
16     //做一个构造函数
17     public day13()
18     {
19         //创建一个按钮
20         jb1=new JButton("按钮");
21         
22         //把按钮添加到JFrame面板里面
23         this.add(jb1);
24         
25         //设置窗口标题
26         this.setTitle("我是标题");
27         
28         //设置窗口大小
29         this.setSize(500,500);
30         
31         //设置窗口初始显示位置
32         this.setLocation(600, 400);
33         
34         //设置窗口关闭后关闭JAVA虚拟机
35         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36         
37         //设置窗口显示
38         this.setVisible(true);
39     }
40 }

五类布局管理器

1.流式布局管理器: FlowLayout

 1 /**
 2  *  FlowLayout  流式布局案例
 3  * 1.继承JFrame
 4  * 2.定义组件
 5  * 3.创建组件
 6  * 4.添加组件
 7  */
 8 package com.test2;
 9 
10 import java.awt.BorderLayout;
11 import java.awt.FlowLayout;
12 
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 
16 public class day14 extends JFrame {
17     //定义组件
18     JButton jb1,jb2,jb3,jb4,jb5;
19     public static void main(String []args) {
20         day14 dyDay14=new day14();
21     }
22     //构造函数
23     public day14() {
24         //创建组件
25         jb1=new JButton("中部");
26         jb2=new JButton("东部");
27         jb3=new JButton("南部");
28         jb4=new JButton("西部");
29         jb5=new JButton("北部");
30         //添加各个组件
31         this.add(jb1);
32         this.add(jb2);
33         this.add(jb3);
34         this.add(jb4);
35         this.add(jb5);
36         //设置布局管理器
37         this.setLayout(new FlowLayout(FlowLayout.LEFT)); //流式布局默认居中对齐
38         
39         //设置组件属性
40         this.setTitle("流式布局管理");//设置标题
41         this.setSize(400,300);//设置窗口大小
42         this.setLocation(600,300);//设置窗口初始化显示位置
43         this.setResizable(false);//禁止用户修改窗口大小
44         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45         this.setVisible(true);
46     }
47 }

 

2.边界布局管理器: BorderLayout

 1 /**
 2  * BorderLayout  边界布局案例
 3  * 1.继承JFrame
 4  * 2.定义组件
 5  * 3.创建组件
 6  * 4.添加组件
 7  */
 8 package com.test2;
 9 
10 import java.awt.BorderLayout;
11 import java.awt.FlowLayout;
12 
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 
16 public class day14 extends JFrame {
17     //定义组件
18     JButton jb1,jb2,jb3,jb4,jb5;
19     public static void main(String []args) {
20         day14 dyDay14=new day14();
21     }
22     //构造函数
23     public day14() {
24         //创建组件
25         jb1=new JButton("中部");
26         jb2=new JButton("东部");
27         jb3=new JButton("南部");
28         jb4=new JButton("西部");
29         jb5=new JButton("北部");
30         //添加各个组件
31         this.add(jb1,BorderLayout.CENTER);
32         this.add(jb2,BorderLayout.NORTH);
33         this.add(jb3,BorderLayout.EAST);
34         this.add(jb4,BorderLayout.SOUTH);
35         this.add(jb5,BorderLayout.WEST);
36         
37         //设置组件属性
38         this.setTitle("边界布局管理");//设置标题
39         this.setSize(400,300);//设置窗口大小
40         this.setLocation(600,300);//设置窗口初始化显示位置
41         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
42         this.setVisible(true);
43     }
44 


1
/* 2 * 网格布局 3 * */ 4 package com.test2; 5 import java.awt.GridLayout; 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 class day16 extends JFrame{ 9 int size=9; 10 private JButton jbtn[]=new JButton[size]; 11 public static void main(String[] args){ 12 day16 glout=new day16(); 13 } 14 15 public day16(){ 16 this.setTitle("网格布局"); 17 //设置大小 18 this.setSize(400,200); 19 //设置位置 20 this.setLayout(new GridLayout(3,3)); //设置布局方式GridLayout 21 //把按钮添加到JFrame中 22 for(int i=0;i<size;i++){ 23 jbtn[i]=new JButton(String.valueOf(i)); 24 this.add(jbtn[i]); 25 } 26 this.setLocation(100,100);//设置初始货显示位置 27 //设置关闭时关掉进程 28 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 29 this.setVisible(true); 30 } 31 }

一些常用组件

 1 /**
 2  * 多种布局管理器的使用
 3  */
 4 package com.test3;
 5 import com.img.*;
 6 import java.awt.GridLayout;
 7 import java.awt.Image;
 8 
 9 import javax.imageio.ImageIO;
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JPasswordField;
15 import javax.swing.JTextArea;
16 import javax.swing.JTextField;
17 
18 
19 
20 public class day17 extends JFrame{
21     JPanel jp1,jp2,jp3;
22     JLabel jlb1,jlb2;
23     JButton jb1,jb2;
24     JTextField jtf1;
25     JPasswordField jps1;
26     public static void main(String []args){
27         day17 ddDay17=new day17();
28     }
29     public day17()
30     {
31         //
32         jp1=new JPanel();
33         jp2=new JPanel();
34         jp3=new JPanel();
35         
36         jlb1=new JLabel("用户名");
37         jlb2=new JLabel("密    码");
38         
39         jb1=new JButton("登陆");
40         jb2=new JButton("取消");
41         
42         jtf1=new JTextField(10);
43     
44         jps1=new JPasswordField(10);
45         
46         //设置布局管理
47         this.setLayout(new GridLayout(3,1));
48         //加入各个组件
49         jp1.add(jlb1);
50         jp1.add(jtf1);
51         jp2.add(jlb2);
52         jp2.add(jps1);
53         jp3.add(jb1);
54         jp3.add(jb2);
55         
56         this.add(jp1);
57         this.add(jp2);
58         this.add(jp3);
59         
60         /*String path="/img/YHM.png";
61         try {
62             Image img=ImageIO.read(this.getClass().getResource(path));
63         } catch (Exception e) {
64             // TODO: handle exception
65             e.printStackTrace();
66         }*/
67         this.setTitle("会员登陆窗口");
68         this.setSize(300,200);
69         this.setLocation(300,300);
70         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71         this.setVisible(true);
72     }
73 }

 教程有QQ登陆窗口,计事本

posted on 2020-08-16 09:15  当幸福莱敲门  阅读(131)  评论(0)    收藏  举报