GUI核心技术之AWT:
GUI介绍: 标题图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
Awt:
组件和容器:
1.Frame`
public class TestFrame {
public static void main(String[] args) {
//创建一个frame()对象
Frame frame = new Frame();
//设置窗口的大小(长和宽)
frame.setSize(300,200);
//设置窗口的位置
frame.setLocation(200,200);
//设置背景板的颜色
frame.setBackground(Color.blue);
//设置窗口的标题
frame.setTitle("我的第一个窗口");
//将窗口的大小设为不可改变的
frame.setResizable(false);
//设置窗口为可见的,
frame.setVisible(true);
//设置窗口监听事件,关闭窗口
frame.addWindowListener(new WindowAdapter() {
//窗口点击 关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.Pane面板:Panel是无法单独显示的,必须添加到某个容器中
public class TestPanel {
public static void main(String[] args) {
//创建窗口及设置窗口的一些属性
Frame frame = new Frame("我的窗口");
frame.setSize(300,200);
frame.setLocation(100,100);
frame.setBackground(Color.BLUE);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//创建面板对象
Panel panel = new Panel();
//设置面板的背景颜色
panel.setBackground(Color.GREEN);
//设置面板的大小
panel.setSize(100,50);
//设置面板在窗口中的位置
panel.setLocation(50,50);
//将窗口的布局设置为自由布局
frame.setLayout(null);
//将面板添加进窗口中
frame.add(panel);
}
}
布局管理器
1.流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("流式布局");
frame.setSize(200, 100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//从左边开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
/* 从右边开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
*/
/*//从中间开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);*/
//将流式布局传入
frame.setLayout(flowLayout);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
2.东南西北中布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("东南西北中布局");
frame.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//创建按钮
Button east = new Button("east");
Button west = new Button("west");
Button center = new Button("center");
Button north = new Button("north");
Button south = new Button("south");
//将按钮添加进窗口中按照东南西北中添加进去
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(east,BorderLayout.EAST);
frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
}
3.表格布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GridLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("表格布局");