窗口
package com.interface_1;
import javax.swing.*;
public class Interface_1 extends JFrame{
//把需要的swing组件,定义到这里
JButton jb1=null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_1 interface_1=new Interface_1();
}
//构造函数
public Interface_1(){
//JFrame是一个顶层容器类(可以添加其他swing)
jb1=new JButton("确认");
//给窗体设置标题
this.setTitle("第一个java_窗口程序");
//设置大小,按像素
this.setSize(200, 200);
//添加JButton组件
this.add(jb1);
//设置初始位置
this.setLocation(200,200);
//设置当关闭窗口时,保证JVM也关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示
this.setVisible(true);
}
}
三大布局之边界布局
package com.interface_1;
/**
* @author 菊丿外人
*1、继承JFrame
*2、创建组件构造函数
*4、添加组件
*5、对窗体设置
*6、显示窗体
*
*边界布局
*/
import java.awt.*;
import javax.swing.*;
public class Interface_2 extends JFrame{
//定义组件
JButton jb1=null;
JButton jb2=null;
JButton jb3=null;
JButton jb4=null;
JButton jb5=null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_2 interface_2=new Interface_2();
}
public Interface_2(){
jb1=new JButton("中部");
jb2=new JButton("北边");
jb3=new JButton("东边");
jb4=new JButton("南部");
jb5=new JButton("西部");
//添加各个组件
this.add(jb1,BorderLayout.CENTER);
this.add(jb2,BorderLayout.NORTH);
this.add(jb3,BorderLayout.EAST);
this.add(jb4,BorderLayout.SOUTH);
this.add(jb5,BorderLayout.WEST);
//设置窗体属性
this.setTitle("边界布局");
this.setSize(300,200);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
this.setVisible(true);
}
}
三大布局之流式布局
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
*流式布局
*/
public class Interface_3 extends JFrame{
//定义需要的组件
JButton jb1,jb2,jb3,jb4,jb5;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_3 interface_3=new Interface_3();
}
Interface_3(){
//创建组件
jb1=new JButton("关羽");
//1流式布局不限制他所管理的大小,允许他们有最佳大小
//2当容器被缩放时,组件位置可能发生变化,但组件大小不变
//3默认组件是居中对齐,可以通过FlowLayout(int align)函数来指定对齐方式
jb1.setSize(100,20);
jb2=new JButton("张飞");
jb3=new JButton("赵云");
jb4=new JButton("马超");
jb5=new JButton("黄忠");
//添加组件
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);
//设置布局管理器
//流式布局默认居中对齐
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//设置窗体属性
this.setTitle("边界布局");
this.setSize (300,200);
this.setLocation(200,200);
//禁止用户改变窗体大小
this.setResizable(false);
//退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
this.setVisible(true);
}
}
三大布局之网格布局
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
*功能 网格布局
*注意事项:
*1、组件的相对位置不随容器的缩放而变化,但是大小会变化
*2、所有组件的大小相同
*3、可以通过GridLayout(int rows, int cols, int hgap, int vgap)来指定网格的行/列,
* 水平间隙/垂直间隙
*
*/
public class Interface_4 extends JFrame{
static int size=9;
JButton jbs[]=new JButton[size];
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_4 interface_4=new Interface_4();
}
//构造
Interface_4(){
//创建组件
for(int i=0;i<size;i++)
{
jbs[i]=new JButton(String.valueOf(i+1));
}
//设置网格布局管理器
this.setLayout(new GridLayout(3,3,5,5));
//3行3列
//5为各个按钮的间距
//添加组件
for(int i=0;i<size;i++)
{
this.add(jbs[i]);
}
//设置窗体属性
this.setTitle("网格布局");
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200,200);
this.setVisible(true);
}
}
面板组件
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
* 功能:多种布局管理器的使用
* JPanel 面板组件
*
*/
public class Interface_5 extends JFrame {
//定义组件
JPanel jp1, jp2;
JButton jbs[] = new JButton[6];
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_5 interface_5= new Interface_5();
}
//构造函数
Interface_5(){
//创建组件
//JPanel布局是默认FlowLayout
jp1=new JPanel();
jp2=new JPanel();
jbs[0]=new JButton("西瓜");
jbs[1]=new JButton("苹果");
jbs[2]=new JButton("荔枝");
jbs[3]=new JButton("香蕉");
jbs[4]=new JButton("葡萄");
jbs[5]=new JButton("橘子");
//设置布局管理器
//添加JPanel
jp1.add(jbs[0]);
jp1.add(jbs[1]);
jp2.add(jbs[2]);
jp2.add(jbs[3]);
jp2.add(jbs[4]);
//把panel加入JFrame
this.add(jp1,BorderLayout.NORTH);
this.add(jbs[5],BorderLayout.CENTER);
this.add(jp2, BorderLayout.SOUTH);
this.setSize(300,200);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
复选框和单选框
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
*用户注册页面
*复选框和单选框
*
*/
public class Interface_6_Register extends JFrame{
//定义
//面板组件
JPanel jp1,jp2, jp3;
//标签
JLabel jl1, jl2;
//按钮
JButton jb1, jb2;
JCheckBox jcb1, jcb2,jcb3;
JRadioButton jrb1, jrb2;
ButtonGroup bg;
public static void main(String[] args) {
Interface_6_Register interface_6_Register = new Interface_6_Register();
}
Interface_6_Register(){
//创建组件
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jl1=new JLabel("你喜欢的运动");
jl2=new JLabel("你的性别");
jb1 = new JButton("注册用户");
jb2 = new JButton("取消注册");
jcb1=new JCheckBox("足球");
jcb2=new JCheckBox("篮球");
jcb3=new JCheckBox("乒乓球");
jrb1 = new JRadioButton("男");
jrb2 = new JRadioButton("女");
//一定要把jrb1,jrb2放入一个ButtonGroup中管理
bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
this.setTitle("注册界面");
this.setLayout(new GridLayout(3,1));
//添加组件
jp1.add(jl1);
jp1.add(jcb1);
jp1.add(jcb2);
jp1.add(jcb3);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
//添加到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
实例__登录界面
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
*文本框/密码框/标签组件
*登录界面 。, -。,
*/
public class Interface_6_Signin extends JFrame{
JPanel jp1, jp2, jp3;
JLabel jlb1, jlb2;
JButton jb1, jb2;
//登录框
JTextField jtf1, jtf2;
//密码框
JPasswordField jpf1;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_6_Signin interface_6 = new Interface_6_Signin();
}
//构造函数
Interface_6_Signin(){
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jlb1 = new JLabel("用户名");
jlb2 = new JLabel("密 码");
jb1 = new JButton("登录");
jb2 = new JButton("取消");
jtf1 = new JTextField(10);
jpf1 = new JPasswordField(10);
//设置布局管理
this.setLayout(new GridLayout(3,1));
//加入各个组件
jp1.add(jlb1);
jp1.add(jtf1);
jp2.add(jlb2);
jp2.add(jpf1);
jp3.add(jb1);
jp3.add(jb2);
//加入到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
下拉框,列表框,滚动窗格组件
package com.interface_1;
import java.awt.*;
import javax.swing.*;
/**
* @author 菊丿外人
*功能:下拉框,列表框,滚动窗格组件
*/
public class Interface_7 extends JFrame{
//面板
JPanel jp1,jp2;
//标签
JLabel jl1, jl2;
//下拉框组件
JComboBox jcb1;
//列表框组件
JList jlist;
//滚动窗格组件
JScrollPane jsp;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_7 interface_7 = new Interface_7();
}
Interface_7(){
jp1=new JPanel();
jp2=new JPanel();
jl1 = new JLabel("你的籍贯");
jl2 = new JLabel("旅游地点");
String []jp={"北京","上海","安徽"};
jcb1 = new JComboBox(jp);
String []dd={"西湖","九寨沟","黄山","故宫"};
jlist = new JList(dd);
//设置你希望显示多少个选项
jlist.setVisibleRowCount(1);
jsp = new JScrollPane(jlist);
//设置布局
this.setLayout(new GridLayout(3,1));
//添加组件
jp1.add(jl1);
jp1.add(jcb1);
jp2.add(jl2);
jp2.add(jsp);
this.add(jp1);
this.add(jp2);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
可以变化
package com.interface_1;
import java.awt.*;
import javax.swing.*;
public class Interface_8_case_1 extends JFrame{
//定义组件
JSplitPane jsp;
JList jlist;
//标签
JLabel jll;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Interface_8_case_1 interface_8 = new Interface_8_case_1();
}
Interface_8_case_1(){
String []words={"boy", "girl", "good"};
jlist= new JList(words);
//图片
jll = new JLabel(new ImageIcon("img/timg.jpg"));
//拆分窗格
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jlist,jll);
//可以变化
jsp.setOneTouchExpandable(true);
//设置布局管理器
//添加组件
this.add(jsp);
//
this.setSize(400,300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}