효과를 실현하십시오 :
충돌 스레드 맨 : (단일 스레드)
기본 스레드가 이미지를 새로 고침하라는 요청을 처리하는 경우 다른 요청을 수락 할 수 없으므로 차단 DEAD 루프 상태로 떨어집니다.
그림을 그립니다
import java.awt.graphics; import java.awt.image; import java.awt.toolkit; import javax.swing.jpanel; public class cartonperson extends jpanel emplements runnable {image img [] = new Image [6]; int index = 0; public afuonperson (int int intonperson) 속도) {this.speed = speed; img [0] = thoolkit.getDefaultToolKit (). getImage ( "1.png"); img [1] = thoolkit.getDefaultToolKit (). getImage ( "2.png"); img [2] = thoolkit.getDefaultToolKit (). GetImage (). "3.png"); img [3] = thoolkit.getDefaultToolKit (). getImage ( "4.png"); img [4] = thoolkit.getDefaultToolKit (). getImage ( "5.png"); img [5] = thoolkit.getDefaultToolKit (). getImage (6.png "); run () {while (true) {try {repaint (); thread.sleep (100);} catch (InterpruptedExcept e) {e.printstacktrace ();}}}@atredridepublic void paintcomponent (graphics g) {// auto-auto-a-generated method stubsper.paintcomponent (g.) getWidth (), getheight (), this); // system.out.println (index); if (index == 5) {index = 0;} else {index ++;}}}}단일 스레드 형태 레이아웃
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; public class singlethreadcarton jframe {cartonperson p1; jbutton bstart = new jbutton ( "시작"); JBUTTON ( "대기"); JBUTTON BREW = NEW JBUTTON ( "계속"); SINGETHREDREDCARTON () {init (); this.SetTitle ( "Breaking Threadman"); this.SetSize (600, 500); this.setResizable (true); this.setLocationRelativeTo (null); this.setDefaultCloseOperation (exit_on_close); this.set -void (true);} void init () {this.setlayout (null); p1 = new Cartonperson (0); p1.setbounds (260, 100, 80, 80,, 160); BSTART.SETBOUNDS (260, 280, 80, 30); BPAUSE.SETBOUNDS (260, 320, 80, 30); Bresume.SetBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 30); 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); bresume.addactionListener (260, 360, 80, 30); bresume.addactionListener (BC);} 클래스 버튼 클릭을 구현하는 ActionListener {actureVent e) {// a a a a a a a a a a a a a a a a a a actionevent e). stubif (e.getSource () == bstart) {p1.run ();} else if (e.getSource () == bpause) {} else if (e.getSource () == bresume) {}}} public static void main (string [] args) {New Singlethreadcarton ();}}실행 결과 :
"시작"버튼을 클릭 한 후 프로그램이 충돌합니다.
다중 스레드 형태 레이아웃
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; public class multithreadcarton jframe {cartonperson p1; thread t1; jbutton bstart = new Jbutton ( "시작"); JBUTTON ( "대기"); JBUTTON BREW = NEW JBUTTON ( "계속"); multithreadCarton () {init (); this.setTitle ( "Running Threadman"); this.SetSize (600, 500); this.setResizable (true); this.setLocationRelativeTo (null); this.setDefaultCloseOperation (exit_on_close); this.set -void (true);} void init () {this.setlayout (null); p1 = new Cartonperson (0); p1.setbounds (260, 100, 80, 80,, 160); BSTART.SETBOUNDS (260, 280, 80, 30); BPAUSE.SETBOUNDS (260, 320, 80, 30); Bresume.SetBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.setBounds (260, 360, 30); 360, 80, 30); Bresume.setBounds (260, 360, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionlistner (260, 360, 80); 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.AddActionListener (260, 360, 80, 30); AaddactionListener (260, 360, 80, 80, 80, 80, 80, 30); Bresume.addactionListener (260, 360, 80, 30); Bresume.addactionListener (260, 360, ButtonClick 구현 ActionListener {@overridePublic void ActionPerformed (ActionEvent e) {// auto-generated 메소드 stubif (e.getSource () == bstart) {// p1.run (); t1.start ();} else if (e.getSource () == bpause) {t1.suspend ();} else if (e.getSource () == bresume) {t1.resume ()}}}}}}}}}}}} multithreadcarton ();}}실행 결과 : 상단 그림과 같이.
위는 이번에는 Java에서 스윙에서 실행할 수있는 스레드 영웅의 예에 대한 설명입니다. wulin.com을 지원 해주셔서 감사합니다.
이 기사는 https://www.idaobin.com/archives/841.html에서 재현됩니다