GUI的核心技术:AWT,Swing
2.Awt 2.1 AWT简介
AWT:Abstract Window Toolkit,抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的GUI的基本工具
包含了很多类和接口
元素:窗口,按钮,文本框
java.awt
package com.ym.lesson01;
import java.awt.*;
public class FrameTest {
public static void main(String[] args) {
//Frame类 看源码
Frame frame = new Frame("我的第一个图形用户界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(Color.blue);
//设置弹出的初始位置
frame.setLocation(200,200);
//设置窗口大小固定
frame.setResizable(false);
}
}
生成多个Frame
package com.ym.lesson01;
import java.awt.*;
public class FrameTest2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.green);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.green);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.green);
MyFrame myFrame4 = new MyFrame(300, 300, 400, 400, 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));
//setLocation(200,200);
//setSize(400,400);
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
setResizable(false);
}
}
2.Panel
面板添加,解决窗口关闭问题(监听)
package com.ym.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class PanelTest {
public static void main(String[] args) {
Frame frame = new Frame("frame");
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//frame坐标,背景颜色
frame.setBounds(100,100,200,200);
frame.setBackground(new Color(35, 128, 90));
//panel坐标,背景颜色
panel.setBounds(50,50,50,50);
panel.setBackground(new Color(55, 91, 128));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
frame.setResizable(false);
//监听事件,监听窗口关闭事件 system.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// super.windowClosing(e);
//结束进程
System.exit(0);
}
});
}
}
2.3.1 流式布局 FlowLayout
package com.ym.lesson01;
import java.awt.*;
public class FlowLayoutTest {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
frame.setBounds(100,100,200,200);
frame.setVisible(true);
frame.add(button1);
frame.add(button2);
frame.add(button3);
//FlowLayout
// frame.setLayout(new FlowLayout());//默认center
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右
}
}
2.3.2 东南西北中 BorderLayout
package com.ym.lesson01;
import java.awt.*;
public class BorderLayoutTest {
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.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.setBounds(100,100,100,100);
frame.setVisible(true);
}
}
2.3.3 表格布局 GridLayout
package com.ym.lesson01;
import java.awt.*;
public class GridLayoutTest {
public static void main(String[] args) {
Frame frame = new Frame();
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.setLayout(new GridLayout(3,2));
frame.setVisible(true);
// frame.setSize(200,200);
frame.pack();//java中的一个函数,确定frame的最佳大小
}
}
2.3.4练习
package com.ym.lesson01;
import javax.swing.border.Border;
import java.awt.*;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setBounds(100,100,400,300);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
// Panel p4 = new Panel(new GridLayout(2,2));
Panel p4 = new Panel();
p4.setLayout(new GridLayout(2,2));
p1.add(new Button("p1_btn_west"),BorderLayout.WEST);
p1.add(new Button("p1_btn_east"),BorderLayout.EAST);
p2.add(new Button("p2_btn_top"));
p2.add(new Button("p2_btn_bottom"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("p3_btn_west"),BorderLayout.WEST);
p3.add(new Button("p3_btn_east"),BorderLayout.EAST);
p4.add(new Button("p4_btn_top1"));
p4.add(new Button("p4_btn_top2"));
p4.add(new Button("p4_btn_bottom1"));
p4.add(new Button("p4_btn_bottom2"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}



