GUI编程

  • 内容
  • 相关

组件

窗口

弹窗

面板

文本框

列表框

按钮

图片

监听事件

鼠标

键盘监听


1.简介

GUI的核心技术:Swing,AWT

2.AWT

2.1、AWT简介

1.包含很多类和接口。GUI

2.元素:窗口,按钮,文本框

3.java.awt包

AWT.png

2.2、组件和容器

Frame:


package com.zzk.demo;

import java.awt.*;
//GUI的第一个界面,
public class TestFrame {
    public static void main(String[] args) {
        //frame,jdk 看源码
        Frame frame = new Frame("这是一个窗口的标题");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置颜色
        frame.setBackground(Color.pink);
        //设置弹出位置
        frame.setLocation(20,20);
        //设置不可调节大小
        frame.setResizable(false);
    }
}
设置多个窗口:


Frame.png

面板Panel


package com.zzk.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 = new Frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //设置坐标
        frame.setBounds(200,200,500,500);
        frame.setBackground(new Color(20));
        //panel设置坐标,设置颜色
        panel.setBounds(20,20,500,500);
        panel.setBackground(Color.CYAN);
        //窗口上画面版
        frame.add(panel);
        frame.setVisible(true);

        //监听时间,监听窗口关闭时间 System.exit
        //适配器模式 new WindowsAdapter
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
        });
    }
}

布局管理器

流式布局


package com.zzk.layout;
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.RIGHT));
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setVisible(true);
        frame.setSize(200,200);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
    }
}



东西南北中


package com.zzk.layout;
import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button  north= new Button("north");
        Button  south= new Button("south");
        Button  west= new Button("west");
        Button  east= new Button("east");
        Button  center= new Button("cantor");
        //四周布局
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(west,BorderLayout.WEST);
        frame.add(east,BorderLayout.EAST);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(200,200);
        frame.setVisible(true);


    }
}
BroderLayout.png



表格式布局


package com.zzk.layout;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button bt1= new Button("bt1");
        Button  bt2= new Button("bt2");
        Button  bt3= new Button("bt3");
        Button  bt4= new Button("bt4");
        Button  bt5= new Button("bt5");
        Button  bt6= new Button("bt6");
        //表格流布局,3行2列
        frame.setLayout(new GridLayout(3,2));
        frame.add(bt1);
        frame.add(bt2);
        frame.add(bt3);
        frame.add(bt4);
        frame.add(bt5);
        frame.add(bt6);

        frame.setVisible(true);
        frame.setSize(200,200);

    }
}
GridLayout.png




事件监听

package com.zzk.action;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发事件
        Frame frame = new Frame();
        Button button = new Button("Button");
        frame.setSize(200,200);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        //需要监听器,ActionLister
        MyActionLister myActionLister = new MyActionLister();
        button.addActionListener(myActionLister);
        
    }
static class MyActionLister implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按下了按钮");
    }
}

}


关闭窗口事件


        windowsClose(frame);//关闭窗口
    }
    //窗口关闭事件
    public static void windowsClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
多个按钮共享一个事件



package com.zzk.action;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent2 {
    public static void main(String[] args) {
           //两个按钮,实现同一个监听
           // 开始停止
        Frame frame = new Frame("开始,停止");
        Button button1 = new Button("Start");
        Button button2 = new Button("Stop");

        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);

        Mymsg mymsg = new Mymsg();

        button1.addActionListener(mymsg);
        button2.addActionListener(mymsg);
        frame.setVisible(true);
        frame.pack();
    }

    static class Mymsg implements ActionListener {
        @Override
        //e.getActionCommand() 获取按键信息
        public void actionPerformed(ActionEvent e) {
            System.out.println("按下了"+e.getActionCommand());
        }
    }
}

输入框TextField监听


package com.zzk.text;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText {
    public static void main(String[] args) {

        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        setVisible(true);
        //创建文本输入框
        TextField textField = new TextField();
        add(textField);

        MyActionListener myActionListener = new MyActionListener();
        //输入框的监听
        textField.addActionListener(myActionListener);

        //设置替换编码,星号,用于密码输入
        textField.setEchoChar('*');

        pack();
    }
}
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获得一些资源,返回这个对象
        System.out.println(field.getText());
        field.setText("");//回车清空
    }
}

简易的计算器

oop原则:组合大于继承。

原来的简易的代码:

package com.zzk.text;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易的计算器
public class Calculator {
    public static void main(String[] args) {
        new MyFrame2();
    }
}
//窗口类
class MyFrame2 extends Frame{
    public MyFrame2(){
         setVisible(true);
         setBounds(200,200,500,100);
         //三个文本框
        TextField field1 = new TextField(10);//字符数
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        //按钮设置监听,传参。
        button.addActionListener(new MyActionListener2(field1,field2,field3));
        //一个标签
        Label label = new Label("+");
        //设置流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();

    }
}
//监听器
class MyActionListener2 implements ActionListener{
    //获取三个变量
    private TextField num1,num2,num3;
    public MyActionListener2(TextField num1,TextField num2,TextField num3) {
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
       //将值进行加法运算
        num3.setText(""+(n1+n2));
       //消除前两个框
        num1.setText("");
        num2.setText("");
    }
}
代码优化,改造为面向对象,组合:




ackage com.zzk.text;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易的计算器
public class Calculator {
    public static void main(String[] args) {
        MyFrame2 myFrame2 = new MyFrame2();
        myFrame2.newFrame();
    }
}
//窗口类
class MyFrame2 extends Frame{
    TextField field1;
    TextField field2;
    TextField field3;
    //方法
    public void newFrame(){
        setVisible(true);
        setBounds(200,200,500,100);
        //三个文本框
        field1 = new TextField(10);//字符数
        field2 = new TextField(10);
        field3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        //按钮设置监听,传参。
        button.addActionListener(new MyActionListener2(this));
        //一个标签
        Label label = new Label("+");
        //设置流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
    }
}
//监听器
class MyActionListener2 implements ActionListener{
    //获取计算器这个对象,在一个类中组合另一个类
    MyFrame2 myFrame2=null;
    public MyActionListener2( MyFrame2 myFrame2) {
        this.myFrame2=myFrame2;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得加数和被加数
        int n1 = Integer.parseInt(myFrame2.field1.getText());
        int n2 = Integer.parseInt(myFrame2.field2.getText());
       //将值进行加法运算
        myFrame2.field3.setText(""+(n1+n2));
       //消除前两个框
        myFrame2.field2.setText("");
        myFrame2.field2.setText("");
    }
}
内部类,更好的包装



package com.zzk.text;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易的计算器
public class Calculator {
    public static void main(String[] args) {
        MyFrame2 myFrame2 = new MyFrame2();
        myFrame2.newFrame();
    }
}
//窗口类
class MyFrame2 extends Frame{
    TextField field1;
    TextField field2;
    TextField field3;
    //方法
    public void newFrame(){
        setVisible(true);
        setBounds(200,200,500,100);
        //三个文本框
        field1 = new TextField(10);//字符数
        field2 = new TextField(10);
        field3 = new TextField(20);
        //一个按钮
        Button button = new Button("=");
        //按钮设置监听,传参。
        button.addActionListener(new MyActionListener2());
        //一个标签
        Label label = new Label("+");
        //设置流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
    }
    //监听器类
    //内部类,最大的好处就是畅通无阻地访问外部的属性和方法
   private class MyActionListener2 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //获得加数和被加数
            int n1 = Integer.parseInt(field1.getText());
            int n2 = Integer.parseInt(field2.getText());
            //将值进行加法运算
            field3.setText(""+(n1+n2));
            //消除前两个框
            field2.setText("");
            field2.setText("");
        }
    }
}

画笔



class MyPaint extends Frame{
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.RED);
        g.fill3DRect(50, 50, 50, 50,true);
    }
}

按钮控制窗口背景颜色

package com.zzk.mydemo;

import java.awt.*;
import java.awt.event.ActionEvent;

public class Testdemo {
    public static void main(String[] args) {
        new NewFrame();
    }
}

class NewFrame extends Frame {
    public NewFrame(){
        Button button1 = new Button("red");
        Button button2 = new Button("green");
        setLayout(new FlowLayout(FlowLayout.LEFT));
        setBounds(200,200,550,550);
        setVisible(true);
        add(button1);
        add(button2);
        pack();
        button1.addActionListener(this::actionPerformed);
        button2.addActionListener(this::actionPerformed);
    }
        Color color;
        public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand().equals("red") ){
                color=Color.red;
                setBackground(color);
            }else if (e.getActionCommand().equals ("green")){
                color=Color.green;
                setBackground(color);
            }

        }

}
chuangkoukongzhi.png



停更,群友成功劝退,用到再学

quantui1.png

quantui.png



本文标签:

版权声明:若无特殊注明,本文皆为《z》原创,转载请保留文章出处。

本文链接:GUI编程 - https://www.52zzk.cn/post-116.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注