GUI入门(1)

最近因为要做图形学作业,迫不得已学了一下这个要被淘汰的技术…
以下内容来自于b站“遇见狂神说”GUI教程

文章目录

GUI

GUI:图形用户界面

GUI 核心技术:Swing、AWT

2.AWT

元素:窗口、按钮、文本框

GUI入门(1)

2.1 Frame

package com.cupk.lesson01;

import java.awt.*;

/**
 * @author LY
 * @version 1.0
 */
public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("第一个Java图像界面窗口");

        //设置可见性
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);

        //设置背景颜色
        frame.setBackground(new Color(85,150,68));

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

        //设置大小固定
        frame.setResizable(false);//不可拉伸

    }
}

2.2 Panel

package com.cupk.lesson01;

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

/**
 * @author LY
 * @version 1.0
 */
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(42, 101, 65));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(193,15,60));

        frame.add(panel);

        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

3.布局管理器

  • 流式布局
package com.cupk.lesson01;

import java.awt.*;

/**
 * @author LY
 * @version 1.0
 */
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);

    }
}
  • 东西南北中
package com.cupk.lesson01;

import java.awt.*;

/**
 * @author LY
 * @version 1.0
 */
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);
    }
}
  • 表格布局
package com.cupk.lesson01;

import java.awt.*;

/**
 * @author LY
 * @version 1.0
 */
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");

        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();//自动填充
        frame.setVisible(true);
    }
}

小结

  • frame 是一个*窗口

  • Panel 无法单独显示,必须添加到某个容器中

  • 布局管理器

    1. 流式 FlowLayout
    2. 东西南北中 BorderLayout
    3. 表格 GridLayout
  • 大小,定位,背景颜色,可见性,监听


4.事件监听

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

/**
 * @author LY
 * @version 1.0
 */
public class TestText01 {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame() {
        TextField textField = new TextField();
        add(textField);

        //监听文本框输入的文字
        MyActionListener myActionListener = new MyActionListener();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener);
        
        //设置替换编码
        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }
}

class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获得一些资源,返回一个对象
        System.out.println(field.getText());//获得输入框的文本
    }
}

实现一个简单的计算器

内部类方式

package com.cupk.lesson02;

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


/**
 * @author LY
 * @version 1.0
 */
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(20);
        //一个按钮
        Button button = new Button("=");

        button.addActionListener(new MyCalculatorListener());
        //一个标签
        Label label = new Label("+");

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

    //监听器类
    private class MyCalculatorListener 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("");
        }
    }
}

5.画笔

package com.cupk.lesson03;

import java.awt.*;

/**
 * @author LY
 * @version 1.0
 */
public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame {
    public void loadFrame() {
        setBounds(200,200,600,400);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(100,100,100,100);
        g.fillOval(200,300,50,50);
    }
}
GUI入门(1)
上一篇:三种布局管理器


下一篇:el-button点击了按钮之后,将鼠标移出按钮,会出现保留点击的状态