GUI programming of P3:Java

Keywords: Java Eclipse

GUI

Objects provided by Java for GUI exist java.awt And javax.swing In two bags

The product of Java graphical interface: eclipse is written in pure Java language. So this software does not need to be installed, but at least
jre(Java language program running environment)

1. AWT and Swing

  • java.awt Abstract window toolkit (Abstract window toolkit) needs to call local system methods to realize functions. It belongs to a heavy-duty control (closely coupled (embedded) with the system)

  • javax.swing Based on AWT, a set of GUI system is established, which provides more components and is completely implemented by Java. It enhances portability and is a lightweight (shallow embedded) control

Component: component container: container checkbox: check box

TextField: text box: single line text box. TextArea: text area: multi line text box

Panel: panel. Generally, panel cannot exist independently. It is based on window

Frame: small window

Dialog: (pop up a) dialog. FileDialog: file dialog

Container: a container. It is a special component. Other components can be added in the component through the add method

2. Event monitoring mechanism

Event source (component) (the source that hosts the event)

Event (what happened)

Listener (monitor some actions)

Event handling (post event handling)

paint brush

Implement simple paint

  • Function:
  1. Implement basic form functions
  2. Define drawing in window
package com.gui;
//paint brush
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Demo04 {
    public static void main(String[] args) {
            new Mypaint().MyFame();  //instantiation 
    }
}
 class Mypaint extends Frame {
        public void MyFame(){
            setBounds(100,100,400,400); //Set start coordinates and size
            setVisible(true);           //visualization
            addWindowListener(new WindowAdapter() {   //Close window event
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
     @Override
     public void paint(Graphics g) {
         //super.paintAll(g);
         g.setColor(Color.red);   //Set brush color
        g.drawOval(100,100,100,100);  //Draw a hollow circle and set its coordinates and size

     }
 }

Operation result:

Mouse listening events

The event source of mouse event is often related to the container. When the mouse enters the container, leaves the container, or clicks or drags the mouse in the container, the mouse event will occur. The java language provides two interfaces for processing mouse events: MouseListener, MouseMotionListener and MouseListener

MouseListener interface can handle 5 kinds of mouse events:
Press the mouse, release the mouse, click mouse, mouse in, mouse out.

The corresponding methods are:
(1) getX(): X coordinate of the mouse
(2) getY(): Y coordinate of the mouse
(3) getModifiers(): gets the left or right mouse button.
(4) getClickCount(): the number of mouse clicks.
(5) getSource(): gets the event source where the mouse occurred.
(6) AddMouseListener: add and play the monitor.
(7) Removemouselistener: remove the monitor.

The methods to implement the MouseListener interface are:
(1) mousePressed(MouseEvent e);
(2) mouseReleased(MouseEvent e);
(3) mouseEntered(MouseEvent e);
(4) mouseExited(MouseEvent e);
(5) mouseClicked(MouseEvent e);

[code case]
Set a Sketchpad in the window. When the mouse clicks a certain position of the sketchpad, a point will be drawn on the current Sketchpad position.

package com.gui;
//Mouse event monitoring
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class Demo05 {
    public static void main(String[] args) {
        MyFrame01 frame = new MyFrame01("Drawing");
    }
}
//Mouse class
 class MyFrame01 extends Frame{
        ArrayList points;
        public MyFrame01(String title){
            super(title);
            setBounds(200,200,400,300);
            //Create a collection to store the points of the drawing
            points = new ArrayList<>();
            setVisible(true);               //visualization
            this.addMouseListener(new MyMouselistener());
            //Close window event
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
     @Override
     public void paint(Graphics g) {
         //Draw! Events listening to mouse
         Iterator iterator = points.iterator();
         while (iterator.hasNext()){
             Point point = (Point) iterator.next();
              g.setColor(Color.red);  //Point color
              g.fillOval(point.x,point.y,10,10);//Set the properties of the point displayed by mouse click
         }
     }
     //Add a point to the palette
     public void addPoint(Point point){
            points.add(point);
     }
     //Adapter mode
     private  class MyMouselistener extends MouseAdapter{

         @Override
         public void mousePressed(MouseEvent e) {
             MyFrame01 frame = (MyFrame01) e.getSource();
             frame.addPoint(new Point(e.getX(),e.getY())); //Click the point drawn with the mouse
             //You need to redraw every time you click the mouse
             frame.repaint();  //REFRESH! Frame rate
         }
     }
 }

Operation result:

Window listening event

[code case]
Create a window and set the listening events for window activation and window closing.

package com.gui;
//Window listening event
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Demo06{
    public static void main(String[] args) {
            new WindowFrame();
    }
}
class WindowFrame extends Frame{

    public WindowFrame() {
        setBackground(Color.cyan);
        setBounds(100,100,400,400);
        setVisible(true);

        this.addWindowListener(
                //Anonymous Inner Class 
                new WindowAdapter() {
                    //close window
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("close window");
                        System.exit(0);
                    }
                    //Activate window
                    @Override
                    public void windowActivated(WindowEvent e) {
                       WindowFrame source = (WindowFrame) e.getSource();
                       source.setTitle("Activated");
                        System.out.println("Window focus! Activated");
                    }
                }
        );
    }
}

Operation result:

Keyboard listening events

Interface KeyListener

Listener interface for receiving keyboard events (keystrokes). Classes designed to handle keyboard events either implement this interface (and all methods it contains) or extend the abstract KeyAdapter class (overriding only useful methods).

The addKeyListener method of the component is then used to register the listener object created by the class with the component. Generates a keyboard event when a key is pressed, released, or typed. It then calls the associated method in the listener object and passes the KeyEvent to it.

Method summary
void This method is called when a key is pressed by keyPressed(KeyEvent e).
void Called when keyReleased(KeyEvent e) releases a key
void This method is called when keyTyped(KeyEvent e) types a key.

[code case] create a window to listen to the keyboard keys, and return to the listening value when the key is pressed.

package com.gui;
//Keyboard listening events
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Demo07 {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
 class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,300,300);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //Keyboard press
            @Override
            public void keyPressed(KeyEvent e) {
                //Get a key pressed on the current keyboard and print the code
               int KeyCode = e.getKeyCode();
                System.out.println(KeyCode);
               if (KeyCode == KeyEvent.VK_UP);{
                    System.out.println("You press the up key");
                }
            }
        });
    }
 }

Operation result:

Posted by Katanius on Mon, 08 Jun 2020 23:38:11 -0700