GUI
- AWT
- 包含了很多类和接口!GUI!
- 元素,窗口,按钮,文本框
- java.awt;
Frame
package lession01;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK,看源码
Frame frame = new Frame("我的第一个图像界面");
// 设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
// 设置背景颜色
frame.setBackground(Color.BLACK);
// 弹出初始位置
frame.setLocation(200,200);
// 设置固定大小
frame.setResizable(false);
}
}
封装到一个类中
package lession01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(200,200,400,400);
MyFrame myFrame2 = new MyFrame(700,200,400,400);
MyFrame myFrame3 = new MyFrame(200,700,400,400);
MyFrame myFrame4 = new MyFrame(700,700,400,400);
}
}
class MyFrame extends Frame{
static int num = 0;
MyFrame(int x, int y, int w, int h){
super("我的第"+(++num)+"个窗口");
setSize(w,h);
setBackground(Color.blue);
setLocation(x,y);
setResizable(false);
setVisible(true);
}
}
面板Panel
解决了关闭事件
package lession01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// panel 可以看成一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("我的frame");
Panel panel = new Panel();
// 设置布局
frame.setLayout(null);
// 坐标
// 一次性设置所有参数,更加快捷
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(149, 25, 158));
// panel设置
panel.setBounds(20,40,300,300);
panel.setBackground(new Color(38, 72, 187));
// frame.add(panel)
frame.add(panel);
frame.setVisible(true);
// 接听事件,监听窗口关闭事件 System.exit(0)
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 点击关闭时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局管理器
- 流式布局
package lession01;
import javax.xml.stream.FactoryConfigurationError;
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());
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
// 把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东南西北中
package lession01;
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(200,200);
frame.setVisible(true);
}
}
- 表格布局 Grid
package lession01;
import java.awt.*;
public class TestGrid {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
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();// java 函数 自动布局
frame.setVisible(true);
}
}
总结:
1. Frame是一个顶级窗口
2. Panel无法单独显示,必须添加到某一个容器中
3. 布局管理器
1.流式
2.东南西北中
3.表格
- 大小,定位,背景颜色,可见性,监听!
事件监听
当某个事件发生的时候,该干什么?
package lession01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.Buffer;
public class TestActionEvent {
public static void main(String[] args) {
// 按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button("监听器");
// 因为addActionListener需要ActionListener,所以我们构造一个
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
// 文本框
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);//关闭窗口
frame.setVisible(true);
}
// 关闭窗体的方法
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮公用一个监听器
package lession01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent2 {
public static void main(String[] args) {
// 两个按钮,实现同一个监听
// 开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("end");
// 可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
// 可以多个按钮公用一个监听
button2.setActionCommand("开始-停止");
MyAction myAction = new MyAction();
button1.addActionListener(myAction);
button2.addActionListener(myAction);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// e.getActionCommand() 获取按钮上的信息
System.out.println("按钮被点击了"+e.getActionCommand());
}
}
输入框 TextField 监听
package lession01;
import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestText01 {
public static void main(String[] args) {
// 启动!
MyFrame1 MyFrame1= new MyFrame1();
windowClose(MyFrame1);
}
// 关闭窗体的方法
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField = new TextField();
add(textField);
// 监听这个文本框输入的文字
MyActionLinsenter myActionLinsenter = new MyActionLinsenter();
// 按下回车就会获取输入框中的内容
textField.addActionListener(myActionLinsenter);
// 设置成替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionLinsenter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();// 获得一些资源,返回了一个对象
String s = field.getText();// 获得输入框中的文本
System.out.println(s);
// 设置点击回车后就会消掉
field.setText("");
}
}
目前代码-自制计算器
package lession01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
// 计算器类
class Calculator extends Frame{
public Calculator(){
// 三个文本框
TextField num1 = new TextField(10);// 字符数
TextField num2 = new TextField(10);// 字符数
TextField num3 = new TextField(10);// 字符数
// 一个按钮
Button button = new Button("=");
button.addActionListener(new MyActionListener2(num1,num2,num3));
// 一个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
// 监听器类
class MyActionListener2 implements ActionListener{
// 获取三个变量
private TextField num1,num2,num3;
public MyActionListener2(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取两个加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num1.getText();
// 结果放到第三个框
num3.setText(String.valueOf(n1+n2));
// 清除前两个框
num1.setText("");
num2.setText("");
}
}
利用组合的方法
package lession01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
// 计算器类
class Calculator extends Frame{
// 属性
TextField num1,num2,num3;
// 方法
public void loadFrame(){
this.num1 = new TextField(10);// 字符数
this.num2 = new TextField(10);// 字符数
this.num3 = new TextField(10);// 字符数
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyActionListener2(this));
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
public Calculator(){
}
}
// 监听器类
class MyActionListener2 implements ActionListener{
// 获取计算器对象。在一个类中组合另外一个类
private Calculator calculator;
public MyActionListener2(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取两个加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
- 内部类
- 为了更好地包装
package lession01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
// 计算器类
class Calculator extends Frame{
// 属性
TextField num1,num2,num3;
// 方法
public void loadFrame(){
num1 = new TextField(10);// 字符数
num2 = new TextField(10);// 字符数
num3 = new TextField(10);// 字符数
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyActionListener2());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
private class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// 获取两个加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
画笔
类似于Python中的海龟库
package lesson03;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g){
// super.paint(g);
// 画笔,颜色,画画
g.setColor(Color.red);
//画圆圈
// g.drawOval(100,100,100,100);
//实心的圆圈
g.fillOval(100,100,100,100);
// 矩形
// g.fillRect(300,300,100,100);
}
}
鼠标监听
package lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("11");
}
}
// 自己的类
class MyFrame extends Frame{
// 画画需要画笔,需要监听鼠标当前的位置,用集合来存储点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,400);
points = new ArrayList<>();
setVisible(true);
// 鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g){
// 画画,监听鼠标事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLACK);
g.fillOval(point.x,point.y,5,5);
}
}
// 添加一个点到界面上
public void addPoint(Point point){
points.add(point);
}
// 适配器模式
private class MyMouseListener extends MouseAdapter{
// 鼠标 按下,弹起,按住不放
@Override
public void mousePressed(Mo![请添加图片描述](https://img-blog.csdnimg.cn/69a28f45287440bdb3b934ac36b39945.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5aWL6L-b55qE5bCP6I-c6bih,size_19,color_FFFFFF,t_70,g_se,x_16)
useEvent e){
MyFrame frame = (MyFrame) e.getSource();
// 这个我们点击的时候就会在界面上产生一个点,画
// 这个点就是鼠标的点
frame.addPoint(new Point(e.getX(),e.getY()));
// 每次点击鼠标,都需要重画一次
frame.repaint();
}
}
}
窗口监听
package lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
// addWindowListener(new MyWindowListener());
this.addWindowListener(
// 匿名内部类-比内部类更好的方法
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了关闭按钮");
System.exit(0);
}
}
);
}
// // 内部类
// class MyWindowListener extends WindowAdapter {
// @Override
// public void windowClosing(WindowEvent e){
// System.exit(0);// 正常退出
// }
// }
}
键盘监听
package lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 获得键盘下的键是哪一个,当前的码
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_0){
System.out.println("00");
}
}
});
}
}