Java GUI notes (crazy God says Java)

Keywords: Java

GUI

Here are the videos of MySQL learning: Point! Here! Inside!
Crazy teacher's learning platform: www.kuangStudy.com
This note does not include the snake project

assembly

  • window
  • Popup
  • panel
  • Text box
  • list box
  • Button
  • picture
  • Monitoring time
  • mouse
  • Keyboard events
  • Plug in
  • Crack tool

1. Introduction

Gui's core technology: Swing AWT

Reasons for non popularity:

  1. Because the interface is not beautiful
  2. jre environment required

Why should we study?

  1. You can write the gadgets you want
  2. When working, you may also need to maintain the swing interface. The probability is very small!
  3. Understand MVC architecture and monitor!

2. AWT introduction

  1. Contains many classes and interfaces! GUI: graphical user interface programming
  2. Elements: window, button, text box
  3. The packages are in java.awt

Components and containers

1. Frame frame

//Gui's first interface
public static void main(String[] args){
    //Frame,JDK, look at the source code! (JDK help document)
    Frame frame = new Frame("My first Java Image interface window");
    //Visibility needs to be set
    frame.setVisible(true);
    //Set window size
    frame.setSize(400,400);
    //To set the background color, you can directly check the source code and set the existing color frame.setBackground(Color.black);
    //You can also directly enter the three primary colors of new color(r.g.b)
    frame.setBackground(new Color(37, 80, 167));
    //Initial position of eject
    frame.setLocation(200,200);
    //There is probably a prototype, but the window closing function is not set. The minimization, maximization and window size exist by default.
    //Set fixed size
    frame.setResizable(false);
}

Problem: if the window cannot be closed, you can only stop the java program.

Review packaging:

import java.awt.*;
public class TestFrame2 {
    public static void main(String[] args) {
        //Multiple windows may be required
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
        MyFrame myFrame2 = new MyFrame(150, 150, 200, 200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(200, 200, 200, 200, Color.red);
        MyFrame myFrame4 = new MyFrame(250, 250, 200, 200, Color.green);
    }
}
class MyFrame extends Frame {
    static int id = 0;      //There may be multiple windows and a counter is required
    public MyFrame(int x,int y,int w,int h,Color color){
        super("Myframe+"+(++id));//inherit
        setBounds(x, y, w, h);//coordinate
        setBackground(color);//colour
        setVisible(true);//visibility 
    }
}

2. Panel panel

import java.awt.*;
//panel can be regarded as a space, but it cannot exist alone
public class TestPanel {
    public static void main(String[] args){
        Frame frame = new Frame();  //new window
        //Concept of layout
        Panel panel = new Panel();  //new panel
        Panel panel1 = new Panel();
        //Set the layout. If not, the panel will be placed on the top
        frame.setLayout(null);
        //Window coordinates and colors
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(140, 208, 212));
        //panel sets the coordinates relative to the frame
        panel.setBounds(50,50,400,100);
        panel.setBackground(new Color(181, 186, 54));
        panel1.setBounds(50,200,400,250);
        panel1.setBackground(new Color(165, 34, 101));
        //Add panel to frame
        frame.add(panel1);
        frame.add(panel);
        frame.setVisible(true);

    }
}
// Solve the problem of window closing
//Listen to the event. The listening window closes. The event System.exit(0) is forced to end
/*frame.addWindowListener(new WindowListener() {
    @Override
    public void windowOpened(WindowEvent e) {

    }
    @Override
    public void windowClosing(WindowEvent e) {

    }
});......
Too many direct new WindowListener() subclasses,
You can use the adapter pattern to new one of the required subclasses*/
    frame.addWindowListener(new WindowAdapter() {
    //What you need when you click to close the window
    @Override
    public void windowClosing(WindowEvent e) {
        //End program
        System.exit(0);
    }

Layout manager

  • Streaming layout
  • East, West, North and South
  • Table layout

frame.setLayout

Streaming layout
frame.setLayout(new FlowLayout(0));

import java.awt.*;
public class TestFlowLayout  {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //Component - button
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        //Sets the location of the streaming layout
        //frame.setLayout(new FlowLayout(0)); 	 0 is left, 1 is middle
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));//Two ways
        frame.setSize(200,200);
        //Add the button
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

East, West, North and South
Frame. Add (key name, BorderLayout.EAST orientation);

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.setSize(400,400);
        //Orientation of different layouts
        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.setVisible(true);
    }
}

Table layout
TestGridLayout

import java.awt.*;
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();//Java function to optimize the size;
        // frame.setSize(400,400);
        frame.setVisible(true);
    }
}

Summary:

  1. Frame is a top-level window
  2. The Panel cannot be displayed separately and must be added to a container.
  3. Layout manager
    1. Flow type
    2. East, West, North and South
    3. form
  4. Size, positioning, background color, visibility, monitoring!

event listeners

Event monitoring: what do you do when something happens?

public class testActionEvent {
    public static void main(String[] args) {
        // Press the button to trigger some events
        Frame frame = new Frame();
        Button button = new Button();

        // Because addActionListener() needs an ActionListener, we need to build an ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);//close window
        frame.setVisible(true);

    }

    // Event that closes the form
    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

// event listeners 
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

Multiple buttons that share an event

public class testActionEvent02 {
    public static void main(String[] args) {
        Frame frame = new Frame("start-stop it");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        // Definitions that can be displayed trigger commands that will be returned. If definitions are not displayed, the default value will be taken!
        // You can write only one listening class for multiple buttons
        button2.setActionCommand("button2-stop");

        MyMonitor myMonitor = new MyMonitor();

        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

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

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



class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // Get button information
        System.out.println("Button clicked: msg"+e.getActionCommand());

    }
}

Input box TextField monitor

public class testTextFieldEvent {
   public static void main(String[] args) {
       // start-up
       new MyFrame();
   }
}

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

       // Listen for text entered in this text box
       MyActionListener2 myActionListener2 = new MyActionListener2();
       // Pressing enter will trigger the event of this input box
       textField.addActionListener(myActionListener2);


       // Set alternate encoding
       textField.setEchoChar('*');

       setVisible(true);
       pack();
   }
}

class MyActionListener2 implements ActionListener{

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

Simple calculator, combination + internal class review!

oop principle: combination is greater than inheritance!

class A extends B{
    
}

class A{
    public 
}

Initial code

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

// Calculator class
class Calculator extends Frame{
    public Calculator(){
        // Three text boxes
        TextField textField1 = new TextField(10);// Number of characters
        TextField textField2 = new TextField(10);// Number of characters
        TextField textField3 = new TextField(10);// Number of characters

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

        button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));
        
        // A label
        Label label = new Label("+");

        //layout
        setLayout(new FlowLayout());

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);


    }
}

// Listener class
class MyCalculatorListener implements ActionListener{

    // Get three variables
    private TextField textField1,textField2,textField3;

    public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Get addends and addends
        int n1 = Integer.parseInt(textField1.getText());
        int n2 = Integer.parseInt(textField2.getText());
        int n3 = n1 + n2;
        //Add this value and put it in the third box
        textField3.setText(""+n3);
        //Clear the first two boxes
        textField1.setText("");
        textField2.setText("");
    }
}

Completely transformed into object-oriented writing

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

// Calculator class
class Calculator extends Frame{

    //attribute
    TextField textField1,textField2,textField3;

    //method
    public void loadFrame() {
        textField1 = new TextField(10);// Number of characters
        textField2 = new TextField(10);// Number of characters
        textField3 = new TextField(20);// Number of characters

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

        button.addActionListener(new MyCalculatorListener(this));

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

        //layout
        setLayout(new FlowLayout());

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);
    }
}

// Listener class
class MyCalculatorListener implements ActionListener{

    // Get the calculator object and combine another class in one class
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Get addends and addends
        int n1 = Integer.parseInt(calculator.textField1.getText());
        int n2 = Integer.parseInt(calculator.textField2.getText());
        int n3 = n1 + n2;
        //Add this value and put it in the third box
        calculator.textField3.setText(""+n3);
        //Clear the first two boxes
        calculator.textField1.setText("");
        calculator.textField2.setText("");
    }
}

Inner class

  • Better packaging
public class testCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

// Calculator class
class Calculator extends Frame{

    //attribute
    TextField textField1,textField2,textField3;

    //method
    public void loadFrame() {
        textField1 = new TextField(10);// Number of characters
        textField2 = new TextField(10);// Number of characters
        textField3 = new TextField(20);// Number of characters

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

        button.addActionListener(new MyCalculatorListener());

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

        //layout
        setLayout(new FlowLayout());

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        pack();
        setVisible(true);
    }

    // Listener class
    // The biggest advantage of internal classes is that they can have unimpeded access to external properties and methods!
    private class MyCalculatorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            //Get addends and addends
            int n1 = Integer.parseInt(textField1.getText());
            int n2 = Integer.parseInt(textField2.getText());
            int n3 = n1 + n2;
            //Add this value and put it in the third box
            textField3.setText(""+n3);
            //Clear the first two boxes
            textField1.setText("");
            textField2.setText("");
        }
    }
}

paint brush

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

class MyPaint extends Frame{

    public void loadFrame(){
        setBounds(200,200,1200,500);
        setVisible(true);
    }

    //paint brush
    @Override
    public void paint(Graphics g) {
        //Brushes need color to draw
        g.setColor(Color.BLUE);
        g.drawOval(100,100,100,100);//Hollow circle
        g.fillOval(200,200,100,100);//disc 

        g.setColor(Color.green);
        g.fillRect(300,300,100,100);

        //Form a habit, use up the brush and restore it to its original color (black)

    }
}

Mouse monitor

Purpose: to achieve mouse painting!

//Mouse listening events
public class testMouseListener {
    public static void main(String[] args) {
        new MyFrame("Drawing");
    }
}

class MyFrame extends Frame{

    ArrayList points;
    // You need a brush to draw, you need to monitor the current position of the mouse, and you need a collection to store this point
    public MyFrame(String title) {
        super(title);
        setBounds(200,200,400,300);
        // Save mouse clicks
        points = new ArrayList<>();
        //Mouse listener for this window
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
           Point points =(Point) iterator.next();
           g.setColor(Color.blue);
           g.fillOval(points.x, points.y,10,10);
        }
    }


    //Add a point to the interface
    public void addPaint(Point point){
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //When we click this, a point will be generated on the interface! painting
            //This point is the point of the mouse
            addPaint(new Point(e.getX(),e.getY()));


            // You need to draw it every time you click the mouse
            repaint();
        }
    }
}

Window listening

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

class WindowFrame extends Frame {
    public WindowFrame() {
        setVisible(true);
        setBackground(Color.BLUE);
        setBounds(100,100,200,200);
        addWindowListener(new MyWindowListener());
//        this.addWindowListener(
//                //Anonymous inner class
//                new WindowAdapter() {
//                    @Override
//                    public void windowClosing(WindowEvent e) {
//                        System.exit(0);
//                    }
//                }
//        );

    }

    class MyWindowListener extends WindowAdapter{
        @Override
        // close window
        public void windowClosing(WindowEvent e) {
            setVisible(false);// Hide the window, not exit
            System.exit(0);// Normal exit
        }

        @Override
        //Activate window
        public void windowActivated(WindowEvent e) {
            WindowFrame source  =(WindowFrame) e.getSource();
            source.setTitle("Activated");
            System.out.println("windowactivated");
        }
    }
}

Keyboard monitor

public class testKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(100,100,400,300);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //Get which key is under the keyboard and the current code
                int keyCode = e.getKeyCode();//You do not need to remember the value, but use the dynamic attribute VK directly_ XX
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("You pressed the up button");
                }
                // Different results are generated according to different operations pressed;
            }
        });
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

3,Swing

Windows, panels

public class JFrameDemo {
    // init(); initialization
    public void init(){
        JFrame jframe = new JFrame("this is a JFrame window!");
        jframe.setVisible(true);
        jframe.setBounds(100,100,300,400);

        //Set text JLabel
        JLabel jLabel = new JLabel("You opened this window!");
        jframe.add(jLabel);
        //Close event
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        //Create a new window
        new JFrameDemo().init();
    }
}

Label centered

package GUI.lesson04;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {
    // init(); initialization
    public void init(){
        JFrame jframe = new JFrame("this is a JFrame window!");
        jframe.setVisible(true);
        jframe.setBounds(100,100,300,400);

        //Set text JLabel
        JLabel jLabel = new JLabel("You opened this window!");
        jframe.add(jLabel);
        //Close event
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Container instantiation
        Container container = jframe.getContentPane();
        container.setBackground(Color.blue);

        //Set label horizontal center
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

    }
    public static void main(String[] args) {
        //Create a new window
        new JFrameDemo().init();
    }
}

Popup

JDialog is used to pop up. By default, there is a close event!

//main window
public class DiologDemo extends JFrame {

    public DiologDemo(){
        this.setVisible(true);
        this.setBounds(300,300,700,500);
       // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame put things, containers
        Container container = this.getContentPane();
        // Absolute positioning
        container.setLayout(null);

        //Button
        JButton jbutton = new JButton("Point me");
        jbutton.setBounds(30,30,200,50);

        //When you click this button, a pop-up window pops up
        jbutton.addActionListener(new ActionListener() { //monitor
            @Override
            public void actionPerformed(ActionEvent e) {
                //Popup
                new MyDialog();
            }
        });
        container.add(jbutton);
    }

    public static void main(String[] args) {
            new DiologDemo();
    }
}
//Pop up window
class MyDialog extends JDialog{
    public MyDialog() {
        this.setBounds(100,100,500,500);
        this.setVisible(true);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        Container container = this.getContentPane();
        container.add(new Label("La La La"));
        container.setLayout(null);

    }
}

label

label

new JLabel("xxx");

Icon icon

public class IconDemo extends JFrame implements Icon {

    private int width;
    private int height;

    public IconDemo(){}//Nonparametric structure

    public IconDemo(int width , int height){
        this.width = width;
        this.height = height;
    }

    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        //Icons can be placed on labels or buttons!
        JLabel jLabel = new JLabel("Icontest", iconDemo, SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }



    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }

    public static void main(String[] args) {
        new IconDemo().init();
    }
}

Picture Icon

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        JLabel jLabel = new JLabel("ImageIcon");
        //Get the address of the picture
        URL url = ImageIconDemo.class.getResource("pic.jpg");

        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(jLabel);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,200);
    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

panel

JPanel

public class JPanelDemo extends JFrame {
    public JPanelDemo(){
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));//The latter two parameters represent the spacing

        JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
        JPanel jPanel2 = new JPanel(new GridLayout(3, 1));
        JPanel jPanel3 = new JPanel(new GridLayout(1, 2));
        JPanel jPanel4 = new JPanel(new GridLayout(2, 1));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("2"));
        jPanel3.add(new JButton("3"));
        jPanel3.add(new JButton("3"));
        jPanel4.add(new JButton("4"));
        jPanel4.add(new JButton("4"));

        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);
        container.add(jPanel4);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }
    public static void main(String[] args) {
        new JPanelDemo();
    }

}

JSrocllPanel

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = getContentPane();

        //Text field
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("Lala Lala Lala");

        //Scroll panel
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

Button

Picture button

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01(){
        //Turn a picture into an icon
        Container container = this.getContentPane();
        URL resource = JButtonDemo01.class.getResource("pic.jpg");
        Icon icon = new ImageIcon(resource);

        //Put this icon on the button
        JButton jButton = new JButton();  
        jButton.setIcon(icon);
        jButton.setToolTipText("Picture button");

        //add
        container.add(jButton);

        this.setVisible(true);
        this.setBounds(100,100,100,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

radio button

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01(){
        //Turn a picture into an icon
        Container container = this.getContentPane();
        URL resource = JButtonDemo01.class.getResource("pic.jpg");
        Icon icon = new ImageIcon(resource);

        //Radio 
        JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton03 = new JRadioButton("JRadioButton03");

        //Since only one radio box can be selected for grouping, only one group can be selected
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton01);
        group.add(radioButton02);
        group.add(radioButton03);

        container.add(radioButton01,BorderLayout.NORTH);
        container.add(radioButton02,BorderLayout.CENTER);
        container.add(radioButton03,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,100,100,100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

Multi selection button

public class JButtonDemo03 extends JFrame {

    public JButtonDemo03(){
        //Turn a picture into an icon
        Container container = this.getContentPane();
        URL resource = JButtonDemo03.class.getResource("pic.jpg");
        Icon icon = new ImageIcon(resource);

        //Checkbox 
        JCheckBox checkBox01 = new JCheckBox("JCheckBox01");
        JCheckBox checkBox02 = new JCheckBox("JCheckBox02");
        JCheckBox checkBox03 = new JCheckBox("JCheckBox03");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.CENTER);
        container.add(checkBox03,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}

list

Drop down box

public class testComboboxDemo01 extends JFrame {
    public testComboboxDemo01() {
        Container container = this.getContentPane();
        JComboBox comboBox = new JComboBox();

        comboBox.addItem("It's showing");
        comboBox.addItem("Removed");
        comboBox.addItem("Coming soon");

        container.add(comboBox);
        
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }

    public static void main(String[] args) {
        new testComboboxDemo01();
    }
}

list box

public class testComboboxDemo02 extends JFrame {
    public testComboboxDemo02() {
        Container container = this.getContentPane();

        //Generate the contents of the list
//        String[] contents = {"1","2","3"};
        Vector vector = new Vector();
        //What to put in the list
        JList jList = new JList(vector);

        vector.add("zhangsan");
        vector.add("lisi");
        vector.add("wangwu");

        container.add(jList);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(200,200,500,500);
    }

    public static void main(String[] args) {
        new testComboboxDemo02();
    }
}

Application scenario

  • Select a region, or some individual options
  • List, display information, usually dynamic capacity expansion!

Text box

Text box

public class testTextDemo01 extends JFrame {
    public testTextDemo01() {
        Container container = this.getContentPane();

        JTextField textField = new JTextField("Hello");
        JTextField textField2 = new JTextField("World",20);

        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new testTextDemo01();
    }
}

Password box

public class testTextDemo02 extends JFrame {
    public testTextDemo02() {
        Container container = this.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);


        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new testTextDemo02();
    }
}

Text field

//Text field
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("Lala Lala Lala");

supplement

timer

Timer timer = new Timer(delay,listener);
//delay is the number of times to execute. The unit is milliseconds. listener is the listening object

Posted by totof06 on Sun, 10 Oct 2021 20:32:38 -0700