GUI之AWT
AWT介绍
- 包含了很多个类和接口
- 元素:窗口,按钮,文本框
- java.awt
组件和容器
弹窗 Frame
- 直接使用Frame
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个图形界面");
//设置窗口可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(500,500);
//设置窗口位置
frame.setLocation(300,300);
//设置窗口颜色
frame.setBackground(new Color(3, 206, 246));
//设置窗口大小固定
frame.setResizable(false);
}
}
- 调用Frame的包,对其封装
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
MyFrame m1=new MyFrame(200,200,200,200,Color.red);
MyFrame m2=new MyFrame(400,200,200,200,Color.black);
MyFrame m3=new MyFrame(200,400,200,200,Color.cyan);
MyFrame m4=new MyFrame(400,400,200,200,Color.green);
}
}
class MyFrame extends Frame{
static int id=0;
public MyFrame(int x, int y, int w, int h, Color color){
super(""+(++id));
//设置窗口可见性
setVisible(true);
//设置窗口位置
setLocation(x,y);
//设置窗口大小
setSize(w,h);
//设置窗口颜色
setBackground(color);
}
}
面板 Panel
- 面板在窗口之中
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TextPanel {
public static void main(String[] args) {
Frame frame = new Frame("大");
Panel panel = new Panel();
//设置窗口布局
frame.setLayout(null);
//设置窗口可见性
frame.setVisible(true);
//设置窗口位置
frame.setLocation(500,50);
//设置窗口大小
frame.setSize(800,800);
//设置窗口颜色
frame.setBackground(new Color(116, 226, 50));
//设置窗口大小固定
frame.setResizable(false);
//设置面板位置
panel.setBounds(100,100,600,600);//相对于窗口的位置
//设置面板颜色
panel.setBackground(new Color(7, 234, 239));
//窗口里面加入面板
frame.add(panel);
//监听事件,监听窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
布局管理
- 流式布局 Flowlayout
import java.awt.*;
public class TestFlowlayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(500,500);
frame.setVisible(true);
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 FlowLayout());//默认在中上
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
}
}
- 东西南北中 BorderLayout
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(500,500);
frame.setVisible(true);
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
Button canter = new Button("canter");
//添加按钮
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(canter,BorderLayout.CENTER);
}
}
- 表格布局
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(500,500);
frame.setVisible(true);
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();
}
}
事件监听
- 事件监听:当某个事件发生的时候,要干什么
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();
Button button = new Button("button");
//因为addActionListener()需要一个ActionListener,所以我们构造一个ActionListener
MyActionListener myActionListener =new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
frame.setVisible(true);
widowClose(frame);
}
//关闭窗口的方法
private static void widowClose(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("你好!");
}
}
- 多个按钮共享一个事件
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");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.setVisible(true);
frame.setResizable(false);
frame.pack();
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="start"){
System.out.println("傻逼");
}else{
System.out.println("信球");
}
}
}
输入框TextFied
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Text {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
//按下enter就会触发这个输入的事件
textField.addActionListener(myActionListener);
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源,返回一个对象
TextField field =(TextField) e.getSource();
//获得输出文本框
System.out.println(field.getText());
field.setText("");
}
}
加法器
- 基础加法器
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc1 {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//三个文本框
TextField textField1 = new TextField(5);
TextField textField2 = new TextField(5);
TextField textField3 = new TextField(10);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
class MyCalculatorListener implements ActionListener{
//获得三个变量
private TextField textField1,textField2,textField3;
public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){
this.textField1=textField1;
this.textField2=textField2;
this.textField3=textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int num1=Integer.parseInt(textField1.getText());
int num2=Integer.parseInt(textField2.getText());
//将加法运算的结果放在第三个框
textField3.setText(""+(num1+num2));
//将前两个框清零
textField1.setText("");
textField2.setText("");
}
}
- 面向对象优化
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc2 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//三个文本框
textField1 = new TextField(5);
textField2 = new TextField(5);
textField3 = new TextField(10);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
class MyCalculatorListener implements ActionListener {
//获得加法器对象
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed (ActionEvent e){
//获得加数和被加数
//将加法运算的结果放在第三个框
//将前两个框清零
int num1 = Integer.parseInt(calculator.textField1.getText());
int num2 = Integer.parseInt(calculator.textField2.getText());
calculator.textField3.setText("" + (num1 + num2));
calculator.textField1.setText("");
calculator.textField2.setText("");
}
}
- 内部类优化
- 内部类的最大优势,可以畅通无阻的访问外部类
package top.dty.awt2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc3 {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
TextField textField1,textField2,textField3;
public void loadFrame(){
//三个文本框
textField1 = new TextField(5);
textField2 = new TextField(5);
textField3 = new TextField(10);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed (ActionEvent e){
//获得加数和被加数
//将加法运算的结果放在第三个框
//将前两个框清零
int num1 = Integer.parseInt(textField1.getText());
int num2 = Integer.parseInt(textField2.getText());
textField3.setText("" + (num1 + num2));
textField1.setText("");
textField2.setText("");
}
}
}
画笔 paint
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,100,800,600);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawOval(100,100,200,200);
g.setColor(Color.cyan);
g.fillOval(300,300,150,150);
g.setColor(Color.blue);
g.fillRect(500,100,150,150);
//用完把画笔还原为原来的颜色
g.setColor(Color.black);
}
}
鼠标监听
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseLister {
public static void main(String[] args) {
IFrame iFrame = new IFrame("画板");
iFrame.widowClose(iFrame);
}
}
class IFrame extends Frame{
//画画需要笔,需要监听鼠标当前位置,需要集合来存储这个点
ArrayList points;
public IFrame(String title){
setBounds(200,200,800,600);
//存鼠标点击的点
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) {
IFrame frame = (IFrame) e.getSource();
//这个我们点击的时候,就会产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每点击鼠标都啊哟重新画一遍
frame.repaint();
}
}
//关闭窗口的方法
public static void widowClose(Frame frame){
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 TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(200,200,800,600);
setBackground(Color.cyan);
setVisible(true);
//匿名内部类
this.addWindowListener(
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
键盘监听
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,1,100,100);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得按下的是键盘的哪一个,获得键盘的码
int keyCode=e.getKeyCode();
System.out.println("这个键的keyCode值为"+keyCode);
//不需要记keyCode,直接通过静态属性VK_...获取键位
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按了上键");
}
}
});
}
}