GUI核心技术之AWT:
GUI介绍: 标题图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
Awt:
组件和容器:
1.Frame`
public class TestFrame {
public static void main(String[] args) {
//创建一个frame()对象
Frame frame = new Frame();
//设置窗口的大小(长和宽)
frame.setSize(300,200);
//设置窗口的位置
frame.setLocation(200,200);
//设置背景板的颜色
frame.setBackground(Color.blue);
//设置窗口的标题
frame.setTitle("我的第一个窗口");
//将窗口的大小设为不可改变的
frame.setResizable(false);
//设置窗口为可见的,
frame.setVisible(true);
//设置窗口监听事件,关闭窗口
frame.addWindowListener(new WindowAdapter() {
//窗口点击 关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.Pane面板:Panel是无法单独显示的,必须添加到某个容器中
public class TestPanel {
public static void main(String[] args) {
//创建窗口及设置窗口的一些属性
Frame frame = new Frame("我的窗口");
frame.setSize(300,200);
frame.setLocation(100,100);
frame.setBackground(Color.BLUE);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//创建面板对象
Panel panel = new Panel();
//设置面板的背景颜色
panel.setBackground(Color.GREEN);
//设置面板的大小
panel.setSize(100,50);
//设置面板在窗口中的位置
panel.setLocation(50,50);
//将窗口的布局设置为自由布局
frame.setLayout(null);
//将面板添加进窗口中
frame.add(panel);
}
}
布局管理器
1.流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FlowLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("流式布局");
frame.setSize(200, 100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//从左边开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
/* 从右边开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
*/
/*//从中间开始流式布局
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);*/
//将流式布局传入
frame.setLayout(flowLayout);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
}
}
2.东南西北中布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BorderLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("东南西北中布局");
frame.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//创建按钮
Button east = new Button("east");
Button west = new Button("west");
Button center = new Button("center");
Button north = new Button("north");
Button south = new Button("south");
//将按钮添加进窗口中按照东南西北中添加进去
frame.add(west,BorderLayout.WEST);
frame.add(north,BorderLayout.NORTH);
frame.add(east,BorderLayout.EAST);
frame.add(center,BorderLayout.CENTER);
frame.add(south,BorderLayout.SOUTH);
}
}
3.表格布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GridLayoutDemo {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("表格布局");
frame.setSize(300,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//设置布局格式为表格布局 设置为3行两列
GridLayout gridLayout = new GridLayout(3, 2);
frame.setLayout(gridLayout);
//创建按钮
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.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
}
}
布局练习题:
实现窗口:
public class ExDemo {
public static void main(String[] args) {
/*
* 分:要实现这个窗口我们需要将这个窗口拆分出来
* 1.将其分为上下两个大的面板采用表格布局分为两行一列
* 2.将上面的面板分为两个按钮和一个面板
* 按钮 采用东南西北中布局布局为一个东一个西
* 面板 放在中间 面板采用表格布局为一行一列将按钮添加进去
* 3.将下面的面板分为两个按钮和一个面板
* 按钮 采用东南西北中布局布局为一个东一个西
* 面板 放在中间 面板采用表格布局为两行两列将按钮添加进去
* */
//总 Frame
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//4个面板
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));
//上面
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
//中间4个
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
事件监听
事件监听的概述: 当某件事发生时需要干什么?
//当按钮被点击时要干的事情
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("事件监听");
frame.setSize(100,70);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Button button = new Button("button");
//添加事件监听器需要一个ActionListener所以我们构造一个 MyActionListener
button.addActionListener(new MyActionListener());
frame.add(button);
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮执行了");
}
}
多个按钮共享一个监听器
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 TestMonitor {
public static void main(String[] args) {
//创建窗口
Frame frame = new Frame("start-stop");
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//创建按钮
Button buttonstart = new Button("start");
Button buttonstop = new Button("stop");
//给按钮设置的信息buttonstop.setActionCommand("设置");
//添加监听器
MyMonitor myMonitor = new MyMonitor();
buttonstart.addActionListener(myMonitor);
buttonstop.addActionListener(myMonitor);
//添加按钮
frame.add(buttonstart,BorderLayout.WEST);
frame.add(buttonstop,BorderLayout.EAST);
//窗口自适应布局
frame.pack();
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand();得到按钮上的信息(按钮的名字,也可以自己给按纽设置信息)
String s = e.getActionCommand();
if(s.equals("start")){
System.out.println("开始执行");
}
else if(s.equals("stop")){
System.out.println("停止执行");
}
}
}
输入框TextFile的监听
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 TextFileDemo {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
//将文本框输入的内容替换成*,以实现保密
textField.setEchoChar('*');
//实现文本框的监听
textField.addActionListener(new TextFileActionListener());
setVisible(true);
add(textField);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class TextFileActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得文本框的资源
TextField textField = (TextField) e.getSource();
//输出文本框的资源
System.out.println(textField.getText());
//将文本框置空
textField.setText("");
}
}
**练习:**简易计算器实现加法功能
目前代码将其三个文本框传入监听器中
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 MyCalc {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator(){
//三个文本框三个文本框都需要监听
TextField field1 = new TextField();
TextField field2 = new TextField();
TextField field3 = new TextField();
//将文本框传递给监听器
Monitor1 monitor1 = new Monitor1(field1, field2, field3);
//监听文本框
field1.addActionListener(monitor1);
field2.addActionListener(monitor1);
field3.addActionListener(monitor1);
//两个按钮但只有=需要监听
Button b1 = new Button("+");
Button b2 = new Button("=");
//监听等号
b2.addActionListener(monitor1);
setLayout(new FlowLayout());
add(field1);
add(b1);
add(field2);
add(b2);
add(field3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Monitor1 implements ActionListener{
private TextField field1,field2,field3;
public Monitor1(TextField field1, TextField field2, TextField field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
@Override
public void actionPerformed(ActionEvent e) {
//得到加数与被加数的内容
int i1 = Integer.parseInt(field1.getText());
int i2 = Integer.parseInt(field2.getText());
//设置第三个文本框的内容
field3.setText(String.valueOf((i1+i2)));
//置空前两个文本框
field1.setText("");
field2.setText("");
}
}
将calculator对象传入:
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 MyCalc2 {
public static void main(String[] args) {
new Calculator2();
}
}
class Calculator2 extends Frame {
public static TextField field1;
public static TextField field2;
public static TextField field3;
public Calculator2(){
//三个文本框三个文本框都需要监听
field1 = new TextField();
field2 = new TextField();
field3 = new TextField();
//将calculator传递给监听器
Monitor2 monitor2 = new Monitor2(this);
//监听文本框
field1.addActionListener(monitor2);
field2.addActionListener(monitor2);
field3.addActionListener(monitor2);
//两个按钮但只有=需要监听
Button b1 = new Button("+");
Button b2 = new Button("=");
//监听等号
b2.addActionListener(monitor2);
setLayout(new FlowLayout());
add(field1);
add(b1);
add(field2);
add(b2);
add(field3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Monitor2 implements ActionListener {
private Calculator2 calculator;
public Monitor2(Calculator2 calculator) {
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数与被加数
int i1 = Integer.parseInt(calculator.field1.getText());
int i2 = Integer.parseInt(calculator.field2.getText());
//设置加数值
calculator.field3.setText(String.valueOf((i1+i2)));
//置空加数与被加数
calculator.field1.setText("");
calculator.field2.setText("");
}
}
将监听器设置为内部类更好的包装
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 MyCalc3 {
public static void main(String[] args) {
new Calculator3();
}
}
class Calculator3 extends Frame {
public static TextField field1;
public static TextField field2;
public static TextField field3;
public Calculator3(){
//三个文本框三个文本框都需要监听
field1 = new TextField();
field2 = new TextField();
field3 = new TextField();
//创建内部类
Monitor3 monitor3 = new Monitor3();
//监听文本框
field1.addActionListener(monitor3);
field2.addActionListener(monitor3);
field3.addActionListener(monitor3);
//两个按钮但只有=需要监听
Button b1 = new Button("+");
Button b2 = new Button("=");
//监听等号
b2.addActionListener(monitor3);
setLayout(new FlowLayout());
add(field1);
add(b1);
add(field2);
add(b2);
add(field3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class Monitor3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//得到加数与被加数
int i1 = Integer.parseInt(field1.getText());
int i2 = Integer.parseInt(field2.getText());
//设置之和
field3.setText(String.valueOf((i1+i2)));
//置空加数与被加数
field1.setText("");
field2.setText("");
}
}
}
画笔
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint();
}
}
class MyPaint extends Frame{
public MyPaint(){
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//重写Frame中的paint方法
@Override
public void paint(Graphics g) {
//得到画笔g,可以给画笔设置颜色,可以用画笔去画图
g.drawRect(100,100,50,50);
g.setColor(Color.red);
g.drawOval(30,50,50,50);
}
}
鼠标监听 实现鼠标画画
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
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.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点;
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
窗口监听
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(){
setSize(200,200);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//当点击窗口时退出窗口
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
}
});
}
}
键盘监听
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyLinstener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setSize(200,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode ==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}