In java, Swing will run in the thread Xia, javaswing thread Xia

Source: Internet
Author: User

In java, Swing will run in the thread Xia, javaswing thread Xia

Effect:

Crash thread Xia: (single thread)

When the main thread is processing the request to refresh the image, it cannot accept other requests and is in a blocked endless loop.

Draw Images

import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JPanel;public class CartonPerson extends JPanel implements Runnable{Image img[]=new Image[6];int index=0;int speed;public CartonPerson(int speed){this.speed=speed;img[0]=Toolkit.getDefaultToolkit().getImage("1.png");img[1]=Toolkit.getDefaultToolkit().getImage("2.png");img[2]=Toolkit.getDefaultToolkit().getImage("3.png");img[3]=Toolkit.getDefaultToolkit().getImage("4.png");img[4]=Toolkit.getDefaultToolkit().getImage("5.png");img[5]=Toolkit.getDefaultToolkit().getImage("6.png");}public void run(){while(true){try{repaint();Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}}}@Overridepublic void paintComponent(Graphics g) {// TODO Auto-generated method stubsuper.paintComponent(g);g.drawImage(img[index], 0, 0, getWidth(), getHeight(), this);//System.out.println(index);if(index==5){index=0;}else{index++;}}}

Single-threaded form layout

Import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JButton; import javax. swing. JFrame; public class SingleThreadCarton extends JFrame {CartonPerson p1; JButton bstart = new JButton ("START"); JButton bpause = new JButton ("wait "); JButton bresume = new JButton ("continue"); SingleThreadCarton () {init (); this. setTitle ("failed thread Xia"); this. setSize (600,500); this. setResizable (true); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (null); p1 = new CartonPerson (0); p1.setBounds (260,100, 80,160); bstart. setBounds (260,280, 80, 30); bpause. setBounds (260,320, 80, 30); bresume. set bounds (260,360, 80, 30); this. add (p1); this. add (bstart); this. add (bpause); this. add (bresume); ButtonClick bc = new ButtonClick (); bstart. addActionListener (bc); bpause. addActionListener (bc); bresume. addActionListener (bc);} class ButtonClick implements ActionListener {@ Overridepublic void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stubif (e. getSource () = bstart) {p1.run ();} else if (e. getSource () = bpause) {} else if (e. getSource () = bresume) {}} public static void main (String [] args) {new SingleThreadCarton ();}}

Running result:

Click the start button to crash the program.

Multi-threaded form layout

Import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. JButton; import javax. swing. JFrame; public class MultiThreadCarton extends JFrame {CartonPerson p1; Thread t1; JButton bstart = new JButton ("START"); JButton bpause = new JButton ("wait "); JButton bresume = new JButton ("continue"); MultiThreadCarton () {init (); this. setTitle ("Running thread Xia"); this. setSize (600,500); this. setResizable (true); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (null); p1 = new CartonPerson (0); p1.setBounds (260,100, 80,160); bstart. setBounds (260,280, 80, 30); bpause. setBounds (260,320, 80, 30); bresume. set bounds (260,360, 80, 30); this. add (p1); this. add (bstart); this. add (bpause); this. add (bresume); ButtonClick bc = new ButtonClick (); bstart. addActionListener (bc); bpause. addActionListener (bc); bresume. addActionListener (bc); t1 = new Thread (p1);} class ButtonClick implements ActionListener {@ Overridepublic void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stubif (e. getSource () = bstart) {// p1.run (); t1.start ();} else if (e. getSource () = bpause) {t1.suspend ();} else if (e. getSource () = bresume) {t1.resume () ;}} public static void main (String [] args) {new MultiThreadCarton ();}}

Running result: As shown in the top figure.

The above is an example of the thread Xia that will run in Swing in java. Thank you for your support.

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.