栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaGUI编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaGUI编程

GUI
  1. AWT

    1. 包含了很多类和接口!GUI!
    2. 元素,窗口,按钮,文本框
    3. java.awt;
  2. Swing

frame
package lession01;

import java.awt.*;

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.BLACK);

        // 弹出初始位置
        frame.setLocation(200,200);

        // 设置固定大小
        frame.setResizable(false);
    }
}

封装到一个类中

package lession01;

import java.awt.*;

public class Testframe2 {
    public static void main(String[] args) {
        Myframe myframe1 = new Myframe(200,200,400,400);
        Myframe myframe2 = new Myframe(700,200,400,400);
        Myframe myframe3 = new Myframe(200,700,400,400);
        Myframe myframe4 = new Myframe(700,700,400,400);
    }
}

class Myframe extends frame{
    static int num = 0;
    Myframe(int x, int y, int w, int h){
        super("我的第"+(++num)+"个窗口");
        setSize(w,h);
        setBackground(Color.blue);
        setLocation(x,y);
        setResizable(false);
        setVisible(true);
    }
}
面板Panel

解决了关闭事件

package lession01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

// panel 可以看成一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        frame frame = new frame("我的frame");
        Panel panel = new Panel();

        // 设置布局
        frame.setLayout(null);

        // 坐标
        // 一次性设置所有参数,更加快捷
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(149, 25, 158));

        // panel设置
        panel.setBounds(20,40,300,300);
        panel.setBackground(new Color(38, 72, 187));

        // frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

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

            }
        });
    }
}
布局管理器
  • 流式布局
package lession01;

import javax.xml.stream.FactoryConfigurationError;
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.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        

        frame.setSize(200,200);

        // 把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}
  • 东南西北中
package lession01;

import java.awt.*;

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(200,200);
        frame.setVisible(true);
    }
}
  • 表格布局 Grid
package lession01;

import java.awt.*;

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

        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();// java 函数 自动布局
        frame.setVisible(true);
    }
}

总结:
1. frame是一个顶级窗口
2. Panel无法单独显示,必须添加到某一个容器中
3. 布局管理器

	1.流式
	2.东南西北中
	3.表格
  1. 大小,定位,背景颜色,可见性,监听!
事件监听

当某个事件发生的时候,该干什么?

package lession01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.Buffer;

public class TestActionEvent {
    public static void main(String[] args) {
        // 按下按钮,触发一些事件
        frame frame = new frame();
        Button button = new Button("监听器");

        // 因为addActionListener需要ActionListener,所以我们构造一个
        MyActionListener myActionListener = new MyActionListener();

        button.addActionListener(myActionListener);
        // 文本框

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);//关闭窗口
        frame.setVisible(true);


    }
    // 关闭窗体的方法
    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("aaa");
    }
}

多个按钮公用一个监听器

package lession01;

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("end");

//        可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
        // 可以多个按钮公用一个监听
        button2.setActionCommand("开始-停止");

        MyAction myAction = new MyAction();

        button1.addActionListener(myAction);
        button2.addActionListener(myAction);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);

    }

}

class MyAction implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        // e.getActionCommand() 获取按钮上的信息
        System.out.println("按钮被点击了"+e.getActionCommand());
    }
}
输入框 TextField 监听
package lession01;

import javax.xml.soap.Text;
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 TestText01 {
    public static void main(String[] args) {
        // 启动!
        Myframe1 Myframe1= new Myframe1();
        windowClose(Myframe1);
    }
    // 关闭窗体的方法
    private static void windowClose(frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class Myframe1 extends frame{
    public Myframe1(){
        TextField textField = new TextField();
        add(textField);

        // 监听这个文本框输入的文字
        MyActionLinsenter myActionLinsenter = new MyActionLinsenter();
        // 按下回车就会获取输入框中的内容
        textField.addActionListener(myActionLinsenter);
        // 设置成替换编码
        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }
}

class MyActionLinsenter implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();// 获得一些资源,返回了一个对象
        String s = field.getText();// 获得输入框中的文本
        System.out.println(s);
        // 设置点击回车后就会消掉
        field.setText("");
    }
}

目前代码-自制计算器

package lession01;

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}

// 计算器类
class Calculator extends frame{
    public Calculator(){
        // 三个文本框
        TextField num1 = new TextField(10);// 字符数
        TextField num2 = new TextField(10);// 字符数
        TextField num3 = new TextField(10);// 字符数

        // 一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyActionListener2(num1,num2,num3));
        // 一个标签
        Label label = new Label("+");

        // 布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }

}
// 监听器类
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());
        num1.getText();
        // 结果放到第三个框
        num3.setText(String.valueOf(n1+n2));
        // 清除前两个框
        num1.setText("");
        num2.setText("");

    }
}

利用组合的方法

package lession01;

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadframe();
    }
}

// 计算器类
class Calculator extends frame{
    // 属性
    TextField num1,num2,num3;
//    方法
    public void loadframe(){
        this.num1 = new TextField(10);// 字符数
        this.num2 = new TextField(10);// 字符数
        this.num3 = new TextField(10);// 字符数
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyActionListener2(this));


        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    public Calculator(){

    }

}
// 监听器类
class MyActionListener2 implements ActionListener{
    // 获取计算器对象。在一个类中组合另外一个类
    private Calculator calculator;
    public MyActionListener2(Calculator calculator){
        this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // 获取两个加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());

        calculator.num3.setText(""+(n1+n2));
        calculator.num1.setText("");
        calculator.num2.setText("");


    }
}
  • 内部类
  • 为了更好地包装
package lession01;

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

public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadframe();
    }
}

// 计算器类
class Calculator 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 MyActionListener2());
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    private class MyActionListener2 implements ActionListener{
        @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("");
        }
    }
}
画笔

类似于Python中的海龟库

package lesson03;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadframe();
    }
}

class MyPaint extends frame{

    public void loadframe(){
        setBounds(200,200,600,500);

        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g){
//        super.paint(g);
//        画笔,颜色,画画
        g.setColor(Color.red);
        //画圆圈
//        g.drawOval(100,100,100,100);
        //实心的圆圈
        g.fillOval(100,100,100,100);
        // 矩形
//        g.fillRect(300,300,100,100);
        
    }
}
鼠标监听 键盘监听 窗口监听
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/276194.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号