directory

  • Learning materials
  • Input box and listener
  • Simple adding calculator
  • The brush
  • Mouse listener, analog drawing tools
  • Window listening event
  • Keyboard listening event

Learning materials

B station crazy god said: www.bilibili.com/video/BV1DJ…

Input box and listener

package com.zy7y.gui;

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

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui
 * @Author: zy7y
 * @Date: 2020/8/15 8:26 PM *@Description: * /
public class TestText01 {
    public static void main(String[] args) {
       newMyFrame1(); }}class MyFrame1 extends Frame{
    public MyFrame1(a){
        TextField textField = new TextField();
        add(textField);
        // Listen for text input in the text box
        MyActionListener myActionListener = new MyActionListener();
        // Press Enter to trigger the event
        textField.addActionListener(myActionListener);

        // Set the replacement code; Encryption operation, input content will display *
        textField.setEchoChar(The '*');
        setVisible(true); pack(); }}class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
// e.getSource(); // Get some resources and return an object
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText()); // Get the text of the input box
        // Set the text to empty
        textField.setText(""); }}Copy the code

Simple adding calculator

package com.zy7y.gui.awt;

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

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui.awt
 * @Author: zy7y
 * @Date: 2020/8/15 8:51 PM *@Description: * /
public class Calc {
    public static void main(String[] args) {
        newCalcFrame().loadFrame(); }}/ / layout
class CalcFrame extends Frame{
    / / property
    TextField textField,textField1,textField2;

    public void loadFrame(a){
        setTitle("A calculator that only adds.");
        textField = new TextField(10); // The length of the 10-bit text box
        textField1 = new TextField(10); // The length of the 10-bit text box
        textField2 = new TextField(20); // The length of the 10-bit text box

        / / button
        Button result = new Button("=");

        / / label
        Label label = new Label("+");

        // Stream layout
        setLayout(new GridBagLayout());
        add(textField);
        add(label);
        add(textField1);
        add(result);
        add(textField2);

        // Handle the = event
        ResultListens resultListens = new ResultListens();
        result.addActionListener(resultListens);


        closeWindows();
        // Set visible
        setVisible(true);


    }

    // Close the window method
    public void closeWindows(a){
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0); }}); }// Event handling: Use the inner class to get the properties of the outer class directly
    private class ResultListens implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // Get text content
            int n1 = Integer.parseInt(textField.getText());
            int n2 = Integer.parseInt(textField1.getText());
            // string.valueof can be converted to a String
            textField2.setText(String.valueOf(n1 + n2));

            // Clear the previous box
            textField.setText("");
            textField1.setText(""); }}}Copy the code

The brush

package com.zy7y.gui.awt;

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

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui.awt
 * @Author: zy7y
 * @Date: 2020/8/15 9:25 PM *@Description: * /
public class TestPaint {

    public static void main(String[] args) {
        newMyPaint().loadFrame(); }}class MyPaint extends Frame{

    public void loadFrame(a){
        setTitle("Paintbrush practice");
        setBounds(200.200.600.500);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0); }}); }// Override the paint method and load the corresponding operation directly
    @Override
    public void paint(Graphics g){
        g.setColor(Color.red);
        // A solid circle
        g.fillOval(100.100.100.100); }}Copy the code

Mouse listener, analog drawing tools

package com.zy7y.gui.awt;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui.awt
 * @Author: zy7y
 * @Date: 2020/8/16 11:55 am *@Description: mouse monitor */
public class MouseListener {
    public static void main(String[] args) {
        new MyFrameMouse("Drawing tool"); }}class MyFrameMouse extends Frame{
    // Set of storage points
    ArrayList points;
    public MyFrameMouse(String title) {
        super(title);
        setBounds(300.300.300.300);
        // Access the mouse point
        points = new ArrayList<>();

        // Mouse monitor
        addMouseListener(new MyMouse());
        setVisible(true);
    }

    // Rewrite the brush
    @Override
    public void paint(Graphics g){
        / / the iterator
        Iterator iterator = points.iterator();
        // iterator.hasNext can iterate no
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.green);
            g.fillOval(point.x,point.y,10.10); }}/ / add a point
    public void addPaint(Point point){
        points.add(point);
        [java.awt.Point[x=162,y=157]]
        //[java.awt.Point[x=162,y=157], java.awt.Point[x=158,y=212]]
        //[java.awt.Point[x=162,y=157], java.awt.Point[x=158,y=212], java.awt.Point[x=229,y=143]]
        System.out.println(points);
    }

    // Adapter mode
    private class MyMouse extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrameMouse myFrameMouse = (MyFrameMouse)e.getSource();
            myFrameMouse.addPaint(new Point(e.getX(),e.getY()));

            / / drawingmyFrameMouse.repaint(); }}}Copy the code

Effect:

Window listening event

package com.zy7y.gui.awt;

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

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui.awt
 * @Author: zy7y
 * @Date: 2020/8/16 1:05 PM *@Description: Window event */
public class TestWindow {
    public static void main(String[] args) {
        newMyWindowEvent(); }}//
class MyWindowEvent extends Frame {
    public MyWindowEvent(a) {
        super("Window event");
        setBounds(300.300.300.300);
        setVisible(true);
        addWindowListener(
                // Anonymous inner class
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("Window closed");
                    }

                    // Window activation
                    @Override
                    public void windowActivated(WindowEvent e) {
                        super.windowActivated(e);
                        System.out.println("Window has focus"); }}); }}Copy the code

Keyboard listening event

package com.zy7y.gui.awt;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.gui.awt
 * @Author: zy7y
 * @Date: 2020/8/16 1:17 PM *@Description: keyboard listening */
public class TestKeyListener {
    public static void main(String[] args) {
        newMyKeyFrame(); }}class MyKeyFrame extends Frame{
    public MyKeyFrame(a){
        super("Keyboard monitor");
        setBounds(200.200.200.200);
        setVisible(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
            }

            // Keyboard press down
            @Override
            public void keyPressed(KeyEvent e) {
                // Get the character value of the input key, but up, down, left, right arrow, space seems not to work
                System.out.println(e.getKeyChar());
                // Get the code for the input key
                System.out.println(e.getKeyCode());
                //
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    System.out.println("You pressed the spacebar!"); }}}); }}Copy the code