1.GUI的核心是AWT和Swing
AWT(Abstract Window Toolkit),中文译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具.
Swing是一个用于开发Java应用程序用户界面的开发工具包。
以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格。
2.AWT
AWT包含很多类和接口
①.组件和容器
- frame
package com.yang.lesson1;
import java.awt.*;
public class FrameDemo {
public static void main(String[] args) {
/*
* GUI的第一个界面
* 1.new一个java图像界面窗口
* 2.需要设置可见性
* 3.设置大小、颜色、初始位置、设置大小固定不可更改*/
Frame frame = new Frame("JAVA");
frame.setVisible(true); //true 可见
frame.setSize(500,500); //大小
frame.setBackground(new Color(128, 5, 83)); //颜色
frame.setLocation(200,200);//初始位置
frame.setResizable(false); //false 大小不可更改
}
}
但是发现窗口点击右上角×关闭不了,需要停止程序才能关闭窗口。
- panel 面板:可以解决frame关不了的问题
package com.yang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelDemo {
public static void main(String[] args) {
/*
* Panel可以看成是一个空间,是不能单独存在的,存在于窗口内
* 1.先new一个窗口,new一个面板
* 2.设置布局,默认为流式布局
* 3.窗口的大小,位置、颜色
* 4.面板的坐标、颜色
* 5.把面板加入到窗口中并设置可见性
* 6.监听窗口关闭
* */
Frame frame = new Frame("Java");
Panel panel = new Panel();
frame.setLayout(null);
frame.setBounds(100,100,500,500);
frame.setBackground(new Color(40, 128, 6));
panel.setBounds(50,50,400,400);//panel的大小要相对于frame的大小内
panel.setBackground(new Color(128, 73, 7));
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
②布局管理器
容器可设置布局管理器,管理容器中组件的布局
- 流式布局
顾名思义,组件像水流一样,从第一行开始,从左向右依次排列,碰到边界时转到下一行继续。
package com.yang.lesson1;
import java.awt.*;
public class FlowLayLoutDemo {
public static void main(String[] args) {
/*
* 流式布局:组件像水流一样,从第一行开始,从左向右依次排列,碰到边界时转到下一行继续。
* 1.new一个窗口和几个按钮组件
* 2.设置为流式布局
* 3.把按钮添加上去
* */
Frame frame = new Frame("java");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
// 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);
}
}
- 东西南北中布局
将容器划分为EAST、WEST、SOUTH、NORTH、CENTER五个部分,每个部分可放置一个组件。
package com.yang.lesson1;
import java.awt.*;
public class BorderLayoutDemo {
public static void main(String[] args) {
/*1.new 一个面板和几个按钮组件
* 2.将按钮添加到面板中
* 3.设置大小、可见*/
Frame frame = new Frame("java");
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.WEST);
frame.add(west ,BorderLayout.EAST);
frame.add(south ,BorderLayout.SOUTH);
frame.add(north ,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
}
}
- 表格布局
将容器划分为指定行数、列数的网格,每个格子的尺寸都相同,一个格子中放置一个组件,适合组件大小差不多的,比如放置计算器的按钮。
package com.yang.lesson1;
import java.awt.*;
public class GridLayoutDemo {
public static void main(String[] args) {
/*1.new一个窗口和几个按钮组件
* 2.设置成网格布局并将按钮添加进窗口
* 3.设置窗口最佳大小和可见*/
Frame frame = new Frame("java");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
Button button5 = new Button("5");
Button button6 = new Button("6");
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();
frame.setVisible(true);
}
}
总结
- Frame是一个顶级窗口,必须先new一个它才能添加进去面板和组件。相当于一个大容器
- Panel 无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
- 需要设置这些性质:大小,定位,背景颜色,可见性,监听!
③事件监听
package com.yang.lesson1;
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 ActionEventDemo {
public static void main(String[] args) {
/*
按下按钮触发一些事件
* 1.把监听动作放在按钮上,把按钮放在窗口上
* 2.addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
* 3.窗口关闭
* */
Frame frame = new Frame();
Button button = new Button();
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
window(frame);
frame.pack();
frame.setVisible(true);
}
private static void window(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("111");
}
}
④输入框监听
package com.yang.lesson1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextDemo {
public static void main(String[] args) {
/*主函数里只new对象,把类和方法都抽出去
* */
new MyFrame();
}
}
//窗口
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的
MyActionListener1 myActionListener1 = new MyActionListener1();
//按下enter,出发这个输入框中的事件
textField.addActionListener(myActionListener1);
textField.setEchoChar('1');
setVisible(true);
pack();
}
}
//监听事件
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源,返回的一个对象
TextField field = (TextField) e.getSource();
//获得输入框的文本
System.out.println(field.getText());
//null ""
field.setText("");
}
}
⑤练习:简易计算器
package com.yang.lesson1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CaclDemo {
public static void main(String[] args) {
new Caclulator().loadframe();
}
}
//计算器类
class Caclulator 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 MyCaclulatorListener());
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
//监听类
private class MyCaclulatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1. 获得加数和被加数
int N1 = Integer.parseInt(num1.getText());
int N2 = Integer.parseInt(num2.getText());
//2. 将这个值 + 法运算后,放到第三个框
num3.setText(""+(N1+N2));
//3. 清除前两个框
num1.setText("");
num2.setText("");
}
}
}
⑥画笔
package com.yang.lesson1;
import java.awt.*;
public class PaintDemo {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
//画笔
class MyPaint extends Frame{
//先加载窗口
public void loadFrame(){
setBounds(200,200,200,200);
setVisible(true);
}
//加载画笔
public void paint(Graphics g){
//画笔需要颜色
g.setColor(Color.cyan);
//画笔需要画东西 画出来的形状
g.fillRect(100,100,100,100);//矩形
//g.fillOval(100,100,100,100); //实心的园
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
⑦鼠标监听
画笔只能画一次,鼠标监听可以用鼠标实现画画,鼠标每点击一次就画一次,将鼠标每次点击的点存到集合中,读出来,画在画板上。
package com.yang.lesson1;
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 MouseListenerDemo {
public static void main(String[] args) {
new MyFrame1("PAINT");
}
}
//自己的类 画图
class MyFrame1 extends Frame{
//点集合
ArrayList points;
// 画板
public MyFrame1(String title){
super(title);
setBounds(200,200,200,200);
//new一个集合来放点
points = new ArrayList<>();
setVisible(true);
//鼠标监听对着这个窗口
this.addMouseListener(new MyMouseListener());
}
//画画
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 {
//按下、弹起
public void mousePressed(MouseEvent e){
MyFrame1 myFrame1 = (MyFrame1) e.getSource();
myFrame1.addPaint(new Point(e.getX(),e.getY()));
myFrame1.repaint();
}
}
}
⑧窗口监听
package com.yang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WindowDemo {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.cyan);
setBounds(100,100,200,200);
setVisible(true);
//窗口监听正对着这个窗口
this.addWindowListener(new WindowAdapter() {
@Override
//关闭
public void windowClosing(WindowEvent e) {
System.exit(0);
System.out.println("exit");
}
@Override
//激活
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活");
System.out.println("WindowActited");
}
});
}
}
⑨键盘监听
package com.yang.lesson1;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
//设置窗口大小颜色可见性
setBounds(1,2,500,300);
setVisible(true);
//键盘监听
//键盘监听正对着这个键盘
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得当下按键对应的码,不需要去记录这个值,直接使用静态属性
int keyCode = e.getKeyCode();
System.out.println(keyCode);
//根据不同操作,得到不同结果
if (keyCode==KeyEvent.VK_UP){
System.out.println("up");
}
}
});
}
}