GUI编程
1、简介
GUI核心技术:Swing和AWT
为什么没有流行起来?
- 界面不美观
- 需要jre环境
2、AWT
Awt介绍:
- 包含了很多类和接口,GUI即用户图形界面
- 元素:窗口,按钮,文本框
- 有两个核心类:组件component和容器container
组件和容器:
2.1Frame
public static void main(String[] args) {
Frame frame = new Frame("我的第一个Java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小width,height
frame.setSize(500,500);
//设置背景颜色
frame.setBackground(new Color(59, 87, 225));
//弹出的初始位置
frame.setLocation(250,250);
//设置大小固定
frame.setResizable(false);
}
问题:
窗口无法关闭,只能通过java结束程序运行
展示多个窗口:
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.pink);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.gray);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
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);
}
2.2面板Panel
可以看成一个空间,但不能单独存在
解决了关闭窗口的问题
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局,如果不设置默认为空
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(166, 248, 165));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(232, 232, 126));
//frame.add(panel)
frame.add(panel);
//设置可见性,默认为false
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(e)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时候要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
2.3布局管理器
-
流式布局
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.LEFT)); frame.setSize(500,500); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }
-
东西南北中
public static void main(String[] args) { Frame frame = new Frame(); Button button1 = new Button("East"); Button button2 = new Button("West"); Button button3 = new Button("South"); Button button4 = new Button("North"); Button button5 = new Button("Center"); frame.add(button1,BorderLayout.EAST); frame.add(button2,BorderLayout.WEST); frame.add(button3,BorderLayout.SOUTH); frame.add(button4,BorderLayout.NORTH); frame.add(button5,BorderLayout.CENTER); frame.setSize(500,500); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }
-
表格布局
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.setLayout(new GridLayout(3,2)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.pack();//Java函数,自动选择一个优秀布局确定 frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override