Java swing programming primer code writing of Java programming primer

  • 2020-04-01 02:39:05
  • OfStack

The basic flow of Swing programming

Step 1: get the main form


JFrame jf = new JFrame("Demo1");

Step 2: get the container for the main form


Container c = jf.getContentPane();

Step 3: set the container layout


c.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));

Step 4: add components and set their properties


JLabel label1 = new JLabel("Hello World!");
JLabel label2 = new JLabel("Bye World!");
label1.setBackground(Color.BLUE);
label1.setOpaque(true);

Step 5: set the properties of the form, close the main form, and exit the program


jf.setSize(200, 100); //Sets the size of the main form
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the program when the form is set to close
 The other: 
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); You can also use the following code instead 
jf.addWindowListener(new WindowAdapter() {
@Override
     public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
});


3. Personal learning experience:

* learn Swing, make simple small software, is not difficult, want to do good, you need to have a deeper study of the layout!

* one use for using Swing: although it's a bit outdated, Swing can be used for gadgets, support work, and entertainment. I'm interested in computer graphics.

*Swing components use some design patterns, is quite worth studying, quite useful for programming!

* learning things is important to adhere to, many toolkits are similar, to one kind of profound, others can also analogy!

4. Attached is a Swing program you learned about in the youtube video:


package com.ting723.www;
 
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class Demo10XOGame extends JFrame{
 
JPanel jp = new JPanel();
 
public Demo10XOGame() {
 Container c = this.getContentPane();
 c.add(jp);
 jp.setLayout(new GridLayout(3, 3));
 for (int i = 0; i < 9; i++) {
 XOButton jb = new XOButton();
 jp.add(jb);
 }
 
 this.setSize(500, 500);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setLocationRelativeTo(null);
 this.setVisible(true);
}
 
public static void main(String[] args) {
 
 new Demo10XOGame();
}
 
 
}



class XOButton extends JButton implements ActionListener {
private ImageIcon X, O;
byte value = 0;
public XOButton() {
 X = new ImageIcon(this.getClass().getResource("x.png"));
 O = new ImageIcon(this.getClass().getResource("o.png"));
 this.addActionListener(this);
 
}
 
@Override
public void actionPerformed(ActionEvent e) {
 
 value++;
 value %= 3;
 switch (value) {
 case 0:
 setIcon(null);
 break;
 case 1:
 setIcon(X);
 break;
 case 2:
 setIcon(O);
 }
}
}


Related articles: