1、GUI简介
GUI的核心技术:Swing、AWT。
现在基本没人使用。
为什么学习GUI:
1、可以写一些小工具
2、工作时候,有可能需要维护到Swing界面
3、了解MVC架构,了解监听。
2、AWT(抽象的窗口工具)
包含了很多的类和接口。存在于java.awt包下。
元素:窗口、按钮、文本框……
2.1、组件和容器
Frame(窗口)
//gui的第一个界面
public class FrameTest {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个gui窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(300,300);
//设置背景颜色
frame.setBackground(new Color(9, 9,255));
//设置弹出的初始位置
frame.setLocation(200,200);
//设置窗口大小固定
frame.setResizable(false);//默认是true,不固定 false固定
}
}
停止java程序,才可以关掉窗口。
public class FrameTest2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 300, 300, Color.blue);
MyFrame myFrame2 = new MyFrame(400, 100, 300, 300, Color.red);
MyFrame myFrame3 = new MyFrame(100, 400, 300, 300, Color.green);
MyFrame myFrame4 = new MyFrame(400, 400, 300, 300, Color.yellow);
}
}
class MyFrame extends Frame{
static int num =0;//窗口计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("我的窗口:"+(++num));
setBackground(color);//设置颜色
setBounds(x,y,w,h);
setVisible(true);
}
}
Panel(面板)
panel无法单独显示,必须添加到某个容器中
public class PanelTest {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//设置窗口坐标
frame.setBounds(100,100,300,300);
frame.setBackground(Color.green);
frame.setVisible(true);
//设置面板坐标
panel.setBounds(50,50,200,200);
panel.setBackground(new Color(110, 5, 99));
//添加面板
frame.add(panel);
//监听器,监听窗口关闭事件 解决了窗口关闭的时间
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
});
}
}
2.2、布局管理器
- 流式布局 FlowLayout
//流式布局
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("流式布局");
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());//默认居中
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//居右
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//居左
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中布局
//东西南北中布局
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("东西南北中布局");
//组件-按钮
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.setSize(200,200);
//添加按钮,设置布局
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setVisible(true);
}
}
- 表格布局 GridLayout
//表格布局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("表格布局");
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setSize(200,200);
//设置表格布局
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);
}
}
练习:
public class TestLayout {
public static void main(String[] args) {
Frame frame = new Frame("练习布局");
frame.setSize(300,100);
frame.setLocation(100,200);
frame.setBackground(Color.red);
frame.setVisible(true);
frame