Java的GUI编程(1)— AWT
前言:
了解AWT得先了解什么是GUI编程,GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口。GUI指的就是採用图形方式显示的计算机操作用户界面,打个例如吧。我们点击QQ图标,就会弹出一个QQ登陆界面的对话框。这个QQ图标就能够被称作图形化的用户界面。GUI的核心技术:AWT和Swing.
AWT
1.1、AWT的三个核心概念:
1.java.awt包提供了基本的GUI设计工具,主要包括组件(Component)、容器(Container)和布局管理器(LayoutManager);
2.组件(Component)类的部分重要的成员方法有:
getComponentAt(int x,int y) //获得坐标(x,y)上的组件对象
getFont() //获得组件的字体
paint(Grahics g) //绘制笔
repaint() //重新绘制组件
setVisible(Boolean b) //设置组件是否可见
Container是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,
但是它的主要功能是用来放置其他组件和容器;
1.2、Frame
package org.lesson01;/*
*/
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个java图像界面窗口");
//先设置界面的可见性
frame.setVisible(true);
//进一步设置窗口大小
frame.setSize(500,500);
//设置窗口的背景颜色(颜色自调)
frame.setBackground(new Color(143, 203, 83));
//设置弹出的初始位置
frame.setLocation(600,300);
//此时窗口大小可以调,可设置固定
frame.setResizable(false);
}
}
1.3 、Panel面板
package org.lesson01;/*
*/
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标,初始位置及弹窗大小
frame.setBounds(600,300,200,200);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
//panel设置坐标,在frame之上
panel.setBounds(50,50,150,150);
panel.setBackground(Color.cyan);
frame.add(panel);
//监听事件,监听弹窗的关闭事件
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
1.4 、布局管理器
- 流式布局
package com.kuang.lesson01;
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);
}
}
- 东南西北中
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);
}
}
- 表格布局
package com.kuang.lesson01;
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(); //Java函数!
frame.setVisible(true);
}
}
总结:
1.Frame是一个顶级的窗口
2.Panel无法单独显示,必须添加到某个容器中(Frame)
初始位置,大小,颜色,可见性,监听(关闭窗户)
3.布局管理器
a.流式布局
b.东南西北中
c.表格
1.5、事件监听
事件源:事件发生的地方
事件:就是要发生的事情
事件处理:就是针对发生的事情做出的处理方案
事件监听器:就是把事件源和事件关联起来
(1)按钮触发事件
package org.lesson01;
import javax.swing.*;
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 TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame("Test");
Button button = new Button("Press me");
//因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 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");
}
}
执行后,单击按钮就会在命令行中输出“aaa”.
(2) 多个按钮,共享一个事件
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionTwo {
public static void main(String[] args) {
// 两个按钮,实现同一个监听
// 开始 停止
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
//可以多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// e.getActionCommand() 获得按钮的信息
System.out.println("按钮被点击了:msg=> "+e.getActionCommand());
if (e.getActionCommand().equals("start")){
}
}
}
(3) 输入框 TextField 监听
package com.kuang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource(); //获得一些资源,返回的一个对象
System.out.println(field.getText()); //获得输入框的文本
field.setText(""); //null ""
}
}