Article 1 Swing

Source: Internet
Author: User

1.Swing is a GUI toolkit designed for Java.

You just need to make the UI.

2. Swing Programming process

Step 1: Obtain the main form

JFrame jf = new JFrame ("Demo1 ");

Step 2: Obtain the container of 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 component attributes

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

Step 5: Set the form attributes, close the main form, and exit the program.

Jf. setSize (200,100); // set the size of the main form
Jf. setVisible (true );

Jf. setResizable (false );

Jf. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the form is closed.

In addition:






 

3. My personal learning experience:

It is not difficult to learn Swing and make simple small software. If you want to do something nice, you need to have a deep research on the layout!

A use of Swing: Although Swing is outdated, it can be used as a tool or a helper to entertain itself. I am very interested in the computer graphic interface.

Swing components use some design patterns, which is worth studying and useful for programming!

You are always insisting on learning things. Many toolkit tools are similar. They are profound and others can be passed!

4. A Swing program learned in Youtobe video is included:

Packagecom. ting723.www;

 

Importjava. awt. Container;

Importjava. awt. GridLayout;

Importjava. awt. event. ActionEvent;

Importjava. awt. event. ActionListener;

 

Importjavax. swing. ImageIcon;

Importjavax. swing. JButton;

Importjavax. swing. JFrame;

Importjavax. swing. JPanel;

 

Publicclass Demo10XOGame extends JFrame {

 

JPanel jp = new JPanel ();

 

Public Demo10XOGame (){

Container c = this. getContentPane ();

C. add (jp );

Jp. setLayout (newGridLayout (3, 3 ));

For (int I = 0; I <9; I ++ ){

XOButton jb = newXOButton ();

Jp. add (jb );

}

 

This. setSize (500,500 );

This. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );

This. setLocationRelativeTo (null );

This. setVisible (true );

}

 

Public static void main (String [] args ){

 

New Demo10XOGame ();

}

 

 

}

 

ClassXOButton extends JButton implements ActionListener {

Private ImageIcon X, O;

Byte value = 0;

Public XOButton (){

X = newImageIcon (this. getClass (). getResource ("x.png "));

O = newImageIcon (this. getClass (). getResource ("o.png "));

This. addActionListener (this );

 

}

 

@ Override

Public void actionreceivmed (ActionEvente ){

 

Value ++;

Value % = 3;

Switch (value ){

Case 0:

SetIcon (null );

Break;

Case 1:

SetIcon (X );

Break;

Case 2:

SetIcon (O );

}

}

}

 

 

Whatever you do, stick to it and there will always be some results! Life, just happy, cannot be the result of injury!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.