나는 JFrame
과 일하고 있으며 while 회 돌이가 있습니다. 그 루프 내에서 프레임의 배경을 검정색으로 변경 한 후 다시 흰색으로 바꿉니다. 그러나 변경하기 전에 두 번째 또는 두 번째로 일시 중지해야 실제로 볼 수 있습니다. Thread.sleep()
및 Timer
이 작동하지 않는 것 같습니다. 누구든지 도와 줄 수 있습니까?프로그램 일시 중지
0
A
답변
0
당신이 swing
에서 timer
를 사용하려면이 그것을 할 수있는 적절한 방법 :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Animation extends JFrame implements ActionListener {
private Timer t;
private Color myColor;
private int howManyTimesIwantThis;
private int count = 0;
public Animation() {
t = new Timer(1000, this); // actionPerformed will be called every 1 sec
t.start();
this.howManyTimesIwantThis = 10;
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
myColor = Color.blue;
}
public void actionPerformed(ActionEvent e) {
if (count < howManyTimesIwantThis) {
count++;
if (myColor.equals(Color.blue)) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
repaint(); //calls the paint method
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(myColor);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.dispose();
}
}
가 그리고 당신은 Thread.sleep()
을 사용하려는 경우,이 같은 작업을 수행 할 수 있습니다
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Animation extends JFrame{
private Color myColor;
private int howManyTimesIwantThis;
private int count = 0;
public Animation() {
this.howManyTimesIwantThis = 10;
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
myColor = Color.blue;
}
public void paint(Graphics g) {
super.paint(g);
while (count < howManyTimesIwantThis) {
count++;
if (myColor.equals(Color.blue)) {
myColor = Color.red;
} else {
myColor = Color.blue;
}
g.setColor(myColor);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
g.dispose();
}
}
코드에 대한 질문이 있으시면 언제든지 문의하십시오.
+0
프로그래머가 paint() 안에 sleep()을 넣을 때마다 강아지가 죽습니다. : ( –
+0
나는 그것을 알고있다 :). 나는 paint() 내부에서 잠을 자지 않겠지 만,이 경우 다른 빠른 방법을 보지 못합니다. – Andy
관련 문제
- 1. Erlang 프로그램 전체 일시 중지/일시 중지
- 2. 세금 계산기 일시 중지 프로그램
- 3. C++ 외부 프로그램 일시 중지
- 4. C# 일시적인 프로그램 일시 중지
- 5. 알림 일시 중지 일시 중지 [Android]
- 6. AS2 애니메이션 일시 중지 및 일시 중지
- 7. C 프로그램 완료시 화면 일시 중지
- 8. 백그라운드에서 실행중인 ios 응용 프로그램 일시 중지
- 9. 아이폰 메모리 경고 일시 중지 응용 프로그램
- 10. GUI 용 Matlab 프로그램 일시 중지
- 11. 자바 스크립트 프로그램 실행 일시 중지
- 12. 응용 프로그램 일시 중지 Windows Phone 8.1
- 13. angularjs 응용 프로그램 실행 일시 중지
- 14. 자바 스윙 타이머 및 프로그램 일시 중지?
- 15. 콘솔 응용 프로그램 "일시 중지"C#
- 16. AWS EC2에서 콘솔 응용 프로그램 "일시 중지"
- 17. 타이머를 사용하여 프로그램 실행 일시 중지
- 18. TextView setText raise 응용 프로그램 일시 중지
- 19. 일시 중지 버튼을 누를 때 프로그램 실행을 일시 중지하는 방법
- 20. 빠른 앱 중지/일시 중지
- 21. 스레드 일시 중지 및 중지
- 22. 어떻게 시작할 수 있습니까 - 일시 중지 - 계속 - 일시 중지 - 계속 - threading.timer로 일시 중지 하시겠습니까?
- 23. 다른 터미널 창에서 일시 중지 된 레일 서버 일시 중지
- 24. 로그인 일시 중지/일시 중지 만료 된 비밀번호 변경
- 25. libgdx에서 게임을 일시 중지 할 때 타이머에서 작업 일시 중지
- 26. 스크립트를 일시 중지/일시 중지 할 수없는 이유는 무엇입니까?
- 27. 스레드를 일시 중지/일시 중지 한 다음 계속하는 방법은 무엇입니까?
- 28. 스레드 일시 중지 - HistoryRecord의 활동 일시 중지 시간 초과
- 29. Jquery simplyscroll trigger 일시 중지/일시 중지 해제
- 30. Libgdx : 일시 중지 메뉴 및 일시 중지 상태
'javax.swing.timer'. –
어떻게 지내니? 당신이 시도한 것을 게시하십시오. –
'Timer','java.util' 또는'javax.swing'이란 무엇입니까? – Azad