1.GUI背景
Java的用户图形界面处理技术。
缺点:
- 图形界面不够美观
- 需要jre环境,不够轻量化
学习目的:
- 巩固Javase语言
- 进行Java小型项目开发(小游戏)
- 维护swing界面
- 便于了解MVC架构,便于了解监听
AWT(抽象窗口工具包)
包括各种小组件,为用户与程序员提供了一套图形交互的工具,使用前需要import java.awt
Frame
创建窗口。
package com.company.lesson01;
import java.awt.*;
public class frame_work {
public static void main(String[] args) {
//窗口
Frame frame = new Frame("java第一个窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,300);
//设置颜色
//frame.setBackground(Color.BLACK);
frame.setBackground(new Color(21, 121, 208));
//弹出得初始化位置
frame.setLocation(200,300);
//设置大小固定
frame.setResizable(false);
}
}
可以重写Frame类,添加静态变量id进行计数。
在初始化过程中直接定义窗口的大小位置属性。
package com.company.lesson01;
import java.awt.*;
public class fram_work1 {
public static void main(String[] args) {
Frame myframe1 = new Myframe(100,100,156,156,Color.BLUE);
Frame myframe2 = new Myframe(200,100,156,156,Color.black);
Frame myframe3 = new Myframe(100,200,156,156,Color.darkGray);
Frame myframe4 = new Myframe(200,200,156,156,Color.yellow);
}
}
class Myframe extends Frame{
static int id = 0;
public Myframe(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
Frame直接创建的窗口无法直接关闭,需要手动使程序停止运行后才能结束。
Panel
Panel的原理和Frame基本相同,但其所有操作都要在一Frame中进行,Panel继承了container,container继承了component,而component可以通过Frame的add方法添加。
package com.company.lesson02;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看作一个空间,但不能单独存在
public class Test_Panel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(200,200,600,400);
frame.setBackground(Color.yellow);
//设置panel
panel.setBounds(50,50,500,300);
panel.setBackground(Color.cyan);
//frame.add panel
frame.add(panel);
frame.setVisible(true);
//监听时间
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
上面这段代码利用了适配器达成窗口的关闭功能,实现了窗口关闭的同时程序结束,具体操作原理会在后面的学习详细介绍。
布局管理
流式布局
大概就是一排一排的摆,摆完一行换一行,可以设置靠左对齐还是靠右对齐或者居中对齐,这里直接上代码
package com.company.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFLowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件,按钮
Button button1 = new Button("BUTTON_1");
Button button2 = new Button("BUTTON_2");
Button button3 = new Button("BUTTON_3");
//设置为流式布局
//frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//frame.setLayout(new FlowLayout(FlowLayout.TRAILING));
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
//Frame基本设置
frame.setSize(700,600);
frame.setBackground(Color.CYAN);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
居中的布局大概这样
流式布局
这个也没什么好说的,看代码,看图,一目了然
package com.company.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button east = new Button("East");
Button north = new Button("North");
Button south = new Button("South");
Button west = new Button("West");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(north,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(west,BorderLayout.WEST);
frame.add(center,BorderLayout.CENTER);
south.setBackground(Color.cyan);
west.setBackground(Color.green);
north.setBackground(Color.MAGENTA);
east.setBackground(Color.yellow);
center.setBackground(Color.pink);
frame.setBackground(Color.MAGENTA);
frame.setBounds(100,100,800,700);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
不同区域用颜色做了区分,运行结果如下
表格布局
顾名思义,是将组件以表格的形式填入。注意在给Frame或者Panel设置表格布局时,一般需要手动输入行列。frame.setLayout(new GridLayout(2,1));
闲话少说,直接上代码
package com.company.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("BUTTON_1");
Button button2 = new Button("BUTTON_2");
Button button3 = new Button("BUTTON_3");
Button button4 = new Button("BUTTON_4");
Button button5 = new Button("BUTTON_5");
Button button6 = new Button("BUTTON_6");
frame.setBackground(Color.LIGHT_GRAY);
frame.setBounds(100,200,800,600);
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
综合运用
构建一个稍微复杂一点点的组件集
package com.company.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame();
//按钮初始化操作
Button button1 = new Button("BUTTON_1");
Button button2 = new Button("BUTTON_2");
Button button3 = new Button("BUTTON_3");
Button button4 = new Button("BUTTON_4");
Button button5 = new Button("BUTTON_5");
Button button6 = new Button("BUTTON_6");
Button button7 = new Button("BUTTON_7");
Button button8 = new Button("BUTTON_8");
Button button9 = new Button("BUTTON_9");
Button button10 = new Button("BUTTON_10");
//两个子面板初始化
Panel panel1 = new Panel();
Panel panel2 = new Panel();
frame.setBounds(100,200,800,600);
panel1.setBounds(0,0,800,300);
panel2.setBounds(0,100,800,300);
//填入两个子面板,以两行一列的方式
frame.setLayout(new GridLayout(2,1));
frame.add(panel1);
frame.add(panel2);
//panel1的操作过程
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.WEST);
panel1.add(button2,BorderLayout.EAST);
//创建更低一级的子面板
Panel panel1_1 = new Panel();
//填入表格形式的按钮
panel1.add(panel1_1,BorderLayout.CENTER);
panel1_1.setLayout(new GridLayout(2,1));
panel1_1.add(button3);
panel1_1.add(button4);
//panel2的操作
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.WEST);
panel2.add(button6,BorderLayout.EAST);
//创建子面板
Panel panel2_1 = new Panel();
panel2.add(panel2_1,BorderLayout.CENTER);
//填入表格形式的按钮
panel1.add(panel1_1,BorderLayout.CENTER);
panel2_1.setLayout(new GridLayout(2,2));
panel2_1.add(button7);
panel2_1.add(button8);
panel2_1.add(button9);
panel2_1.add(button10);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
运用好Panel之间的嵌套,可以完成各种想要的组件集合