组件:窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具
简介
GUI的核心技术:Swing AWT
界面不美观 需要jre环境
AWT
AWT介绍
(1)包含了很多类和接口。GUI:图形用户界面
(2)元素:窗口,按钮,文本框
(3)java.awt
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KYlBhMzI-1651325762435)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20220424214022434.png)]
组件和容器
Frame
package FrameDemo;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame ("我的第一个Java图像界面窗口");
//需要设置可见性 w h
frame.setVisible (true);
//设置窗口大小
frame.setSize (300, 300);
//设置背景颜色 Color
frame.setBackground (new Color (85,150,68));
//弹出的初始位置
frame.setLocation (300, 300);
//设置大小固定
frame.setResizable (false);
}
}
package FrameDemo;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame1=new MyFrame(100,100,200,200,Color.BLUE);
MyFrame myFrame2=new MyFrame(300,100,200,200,Color.YELLOW);
MyFrame myFrame3=new MyFrame(100,300,200,200,Color.RED);
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));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
}
}
面板Panel
package FrameDemo;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestPanel {
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 (5, 183, 226));
//pannel设置坐标,相当于frame
panel.setBounds (50, 50, 400, 400);
panel.setBackground (new Color (226, 5, 5));
//添加组件
frame.add (panel);
//显示
frame.setVisible (true);
//监听事件,监听窗口关闭事件 System.exit(0);
//适配器模式
frame.addWindowListener (new WindowAdapter () {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局管理器
流式布局
package FrameDemo;
import java.awt.*;
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 (FlowLayout.LEFT));
//把按钮添加上去
frame.add (button1);
frame.add (button2);
frame.add (button3);
frame.setVisible (true);
}
}
东西南北中
package FrameDemo;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame ("TestBorderLayout");
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.setSize (300, 300);
frame.setVisible (true);
}
}
表格布局
package FrameDemo;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame ("TestGridLayout");
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.setLayout (new GridLayout (3, 2));
frame.add (btn1);
frame.add (btn2);
frame.add (btn3);
frame.add (btn4);
frame.add (btn5);
frame.add (btn6);
frame.pack ();
frame.setVisible (true);
}
}
GridLayout (3, 2));
frame.add (btn1);
frame.add (btn2);
frame.add (btn3);
frame.add (btn4);
frame.add (btn5);
frame.add (btn6);
frame.pack ();
frame.setVisible (true);
}
}