@[TOC]GUI编程1
①.GUI简介
- GUI的核心技术: Swing AWT编程,因为界面不美观,运行需要jre环境,所以不太流行
- 但是为什么要学习:
GUI是MVC的基础
平时自己可以写一些小工具
工作的时候可能会有几率维护到swing界面
②.AWT
- AWT简介
- AWT(Abstract Windowing Toolkit-抽象窗口工具):本质是Java中一系列的类,
提供了很多的类和接口- GUI(Graphical User Interface-图形用户接口)
- 元素:窗口、按钮、文本框
- 包名:java.awt
- 第一个Frame窗口
- 组件和容器
Frame
第一个Frame窗口
import java.awt.*;
//第一个GUI界面
public class TestFrame {
public static void main(String[] args) {
//获得Frame对象
Frame frame = new Frame("第一个Java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(500,500);
//设置背景颜色
// frame.setBackground(Color.blue);
frame.setBackground(new Color(66, 86, 163));
//弹出的初识位置
frame.setLocation(200,200);
//设置大小固定 默认为true 可以改变
frame.setResizable(false);
}
}
画出了第一个Frame窗口,但是无法关闭,只能通过结束Java程序运行来结束进程。
创建多个窗口
package com.GUILearn;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.black);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.gray);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);
}
}
class MyFrame extends Frame{
static int id = 1; //可能存在多个窗口,需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame" + (++id));
//可以直接调用父类当中的方法
setVisible(true);
setBounds(x,y,w,h);
setBackground(color);
}
}
Panel
Panel可以看成一个空间,但是不能够单独的存在
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
//创建Frame窗口
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(234, 220, 96));
//panel的设置,相对于Frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(241, 104, 104));
//将Panel添加到Frame当中去
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听窗口关闭事件System.exit(0)
//适配器模式 适配器继承了某一个类,不用的方法就可以略过不写
frame.addWindowListener(new WindowAdapter() {
//点击窗口关闭时要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
布局管理
- 流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.RIGHT));
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 东西南北中(相对布局)
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(500,500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 表格式布局Grid
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
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");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack(); //Java函数自动选择最优布局
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
作业:写一个下图所示的窗口
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class HomeWork {
public static void main(String[] args) {
Frame frame = new Frame("HomeWork");
frame.setLayout(new GridLayout(2,1)); //两行一列
//设置Frame大小
frame.setBounds(200,200,500,320);
frame.setVisible(true);
//创建面板
Panel up = new Panel(new BorderLayout());
Panel upCenter = new Panel(new GridLayout(2,1));
Panel down = new Panel(new BorderLayout());
Panel downCenter = new Panel(new GridLayout(2,2));
//上半部分
up.add(new Button("Btn01"),BorderLayout.WEST);
up.add(new Button("Btn04"),BorderLayout.EAST);
upCenter.add(new Button("Btn02"));
upCenter.add(new Button("Btn03"));
up.add(upCenter,BorderLayout.CENTER);
//下半部分
down.add(new Button("Btn05"),BorderLayout.WEST);
down.add(new Button("Btn10"),BorderLayout.EAST);
for (int i = 0; i < 4; i++) {
String name = "Btn0" + (i + 6);
downCenter.add(new Button(name));
}
down.add(downCenter,BorderLayout.CENTER);
//将结构Panel放到Frame当中
frame.add(up);
frame.add(down);
//设置点击关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结
- Frame:是一个顶级窗口
- Panel:无法单独显示,必须添加到某一个容器中
- 布局管理器:
1.流式
2.东西南北中
3.表格- 其它属性:大小,定位,背景颜色,可见性,监听
- 事件监听
理解:当某一个事情发生的时候,要做些什么
简单的事件监听
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 TestAction {
public static void main(String[] args) {
//设置监听,按下按钮时触发某种事件
Frame frame = new Frame("TestAction");
Button btn = new Button("Btn1");
//addActionListener需要一个ActionListener所以需要构造一个ActionListener
btn.addActionListener(new MyActionListener());
frame.add(btn,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗体的事件
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("Button has Click!");
}
}
不同按钮实现同一个监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestAction2 {
public static void main(String[] args) {
//两个按钮,实现同一个监听
//开始 停止
Frame frame = new Frame("Action2");
Button btn1 = new Button("Start");
Button btn2 = new Button("Stop");
//可以显示定义触发会返回的信息,如果不定义,则会定义为默认值
btn2.setActionCommand("Btn2");
MyMonitor myMonitor = new MyMonitor();
btn1.addActionListener(myMonitor);
btn2.addActionListener(myMonitor);
frame.setLayout(new GridLayout(2,1));
frame.setSize(500,200);
frame.add(btn1);
frame.add(btn2);
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
//点击按钮1计数输出
if (command.equals("Start")){
for (int i = 0; i < 500; i++) {
System.out.print("\rStart Click" + i);
try {
Thread.sleep(10);
} catch (Exception ex) {
ex.printStackTrace();
}
}
System.out.println();
//点击按钮2,延迟后退出
}else{
System.out.println(command + "close");
try {
Thread.sleep(1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.exit(0);
}
}
}
TextField
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 TextTest {
public static void main(String[] args) {
//main方法中只管启动,尽量少些功能性代码
new IFrame();
}
}
class IFrame extends Frame{
public IFrame(){
TextField textField = new TextField();
//不需要Frame对象,因为这个类直接继承了Frame
add(textField);
//监听文本框输入的文字,按下回车触发该事件
textField.addActionListener(new MyAction());
//设置替换编码
textField.setEchoChar('·');
setVisible(true);
setBounds(500,500,300,200);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();//获得一些资源,返回一个对象
String text = textField.getText(); //获得输入框中的文本
System.out.println(text);
//激活事件之后,将文本清空
textField.setText("");
}
}
简易计算器
OOP:组合大于继承
//继承
class A extends B{
}
//组合,减少耦合性
class A{
public B b;
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//V1.0未优化版本
public class SimpleCalculator {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
super("Demo01");
//3个文本框
TextField num1 = new TextField(10);//最大能够填入的字符数
TextField num2 = new TextField(10);//最大能够填入的字符数
TextField num3 = new TextField(20);//最大能够填入的字符数
//一个标签
Label label = new Label(" +");
//一个按钮
Button btn = new Button("=");
btn.addActionListener(new MyActionListenerC(num1,num2,num3));
//设置布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
setVisible(true);
setResizable(false);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyActionListenerC implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyActionListenerC(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数与被加数
double add1 = Double.parseDouble(num1.getText());
double add2 = Double.parseDouble(num2.getText());
//2.将这个值加法运算后放到num3
num3.setText("" + (add1 + add2));
//3.清除前两个num
num1.setText("");
num2.setText("");
}
}
import java.awt.*;
import java.awt.event.*;
//改造为面向对象之后
public class SimpleCalculator {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//最大能够填入的字符数
num2 = new TextField(10);//最大能够填入的字符数
num3 = new TextField(20);//最大能够填入的字符数
//一个标签
Label label = new Label(" +");
//一个按钮
Button btn = new Button("=");
btn.addActionListener(new MyActionListenerC(this));
//设置布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
setVisible(true);
setResizable(false);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public Calculator(){
super("Demo01");
}
}
//监听器类
class MyActionListenerC implements ActionListener{
//获取计算器对象,在一个类中组合另一个类
Calculator calculator = null;
public MyActionListenerC(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数与被加数
Double add1 = Double.parseDouble(calculator.num1.getText());
Double add2 = Double.parseDouble(calculator.num2.getText());
//2.将这个值加法运算后放到num3
calculator.num3.setText("" + (add1 + add2));
//3.清除前两个num
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:为了更好的包装
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);//最大能够填入的字符数
num2 = new TextField(10);//最大能够填入的字符数
num3 = new TextField(20);//最大能够填入的字符数
//一个标签
Label label = new Label(" +");
//一个按钮
Button btn = new Button("=");
btn.addActionListener(new MyActionListenerC());
//设置布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(btn);
add(num3);
setVisible(true);
setResizable(false);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public Calculator(){
super("Demo01");
}
//监听内部类,可以畅通无阻的访问外部类的属性与方法
private class MyActionListenerC implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数与被加数
Double add1 = Double.parseDouble(num1.getText());
Double add2 = Double.parseDouble(num2.getText());
//2.将这个值加法运算后放到num3
num3.setText("" + (add1 + add2));
//3.清除前两个num
num1.setText("");
num2.setText("");
}
}
}
Paint
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setVisible(true);
setBounds(200,200,600,500);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//画笔需要有颜色,画笔可以进行绘画
g.setColor(Color.RED);
g.drawOval(100,100,100,100); //空心圆
g.fillOval(100,200,100,100); //实心圆
g.setColor(Color.GREEN);
g.fillRect(200,100,100,100); //矩形
//养成习惯:画笔画完,将画笔还原到最初的颜色
}
}
鼠标监听
目的:想要实现鼠标绘画(点击绘画)
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 MyFrameM("画图");
}
}
class MyFrameM extends Frame{
//画画需要画笔,并且需要监听鼠标当前的额位置
private ArrayList points;
//需要集合来存储这些点
public MyFrameM(String title){
super(title);
setBounds(200,200,400,300);
setVisible(true);
//存储鼠标点击的点
points = new ArrayList();
//鼠标监听器是针对窗口而言的
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);
}
}
//适配器模式,继承该类的适配器实现类
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
//Frame调用的监听,所以source是Frame对象
MyFrameM frame = (MyFrameM) e.getSource();
//点击的时候,会在界面上产生一个点
//这个点就是鼠标的点
Point point = new Point(e.getX(), e.getY());
//添加一个点到界面上
points.add(point);
//每次点击鼠标都需要重新画一遍
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(){
setVisible(true);
setBounds(200,200,300,300);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("Window Has Opened");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Window Has Closed");
}
@Override
public void windowActivated(WindowEvent e) { //窗口激活
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("Running!");
System.out.println("Window Has Activated");
}
@Override
public void windowDeactivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("Resting!");
System.out.println("Window Has Deactivated");
}
@Override
public void windowClosing(WindowEvent e) { //窗口关闭
System.out.println("Has Click X");
System.exit(0);
}
});
}
}
键盘监听
package com.GUILearn;
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(200,200,300,300);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键值,当前的code值
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode == KeyEvent.VK_UP){
System.out.println("Has pressed Up Key!");
}
}
});
}
}