문자열을 변경하지만 jlable에 그대로 남아 있습니다. 해당 문자열을 특정 시간대에서 업데이트하려고합니다.jlabel의 문자열을 특정 간격으로 upadte하는 방법
1
A
답변
1
SwingUtilities.invokeLater 또는 invokeAndWait를 사용해보십시오.
다음 코드와 유사합니다.
희망이 있습니다.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LabelUpdater {
public static void main(String[] args) {
LabelUpdater me = new LabelUpdater();
me.process();
}
private JLabel label;
private void process() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setContentPane(new JPanel(new BorderLayout()));
label = new JLabel(createLabelString(5));
frame.getContentPane().add(label);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
snooze();
for (int i = 5; i >= 1; i--) {
final int time = i - 1;
snooze();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
label.setText(createLabelString(time));
}
});
}
}
private void snooze() {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private String createLabelString(int nbSeconds) {
return "Still " + nbSeconds + " seconds to wait";
}
}
1
사용하십시오 javax.swing.Timer (tutorial). 이렇게하면 이벤트 발송 스레드에서 실행하여 스레드 안전을 보장합니다.
public class TimerDemo {
public static void main(String[] args) {
final int oneSecondDelay = 1000;
final JLabel label = new JLabel(Long.toString(System.currentTimeMillis()));
ActionListener task = new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
label.setText(Long.toString(System.currentTimeMillis()));
}
};
new javax.swing.Timer(oneSecondDelay, task).start();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
관련 문제
- 1. JLabel의 색상을 변경하는 방법
- 2. django celery : 특정 간격으로 프로그래밍 방식으로 실행되도록 태스크를 설정하는 방법
- 3. 사용자 정의 JLabel의 아이콘
- 4. 특정 시간 간격으로 PHP 파일 실행하기
- 5. 특정 시간 간격으로 행에 대한 MySQL 쿼리
- 6. 다양한 시간 간격으로 특정 링크 자동로드
- 7. 설정 JLabel의 정렬 오른쪽에서 왼쪽
- 8. 간격으로 이미지 세트를 팬하는 방법
- 9. JLabel의 배경색은 어떻게 설정합니까?
- 10. JLabel의 크기를 아이콘보다 작습니까?
- 11. 회전 JLabel의 자바 스윙
- 12. JLabel의 HTML이 보이지 않습니다.
- 13. 특정 문자를 무시하고 문자열을 읽는 방법
- 14. C# : 특정 문화권에서 리소스 문자열을 얻는 방법
- 15. 특정 문자열을 포함하는 전체 줄을 필터링하는 방법
- 16. Regex이 특정 문자열을 확인합니다
- 17. 간격으로 변수의 값을 비교하십시오.
- 18. JLabel의 텍스트를 동적으로 크기 조정
- 19. Java - JLabel의 너비/높이 설정
- 20. html JLabel의 줄 수 제한
- 21. 고정 된 간격으로 NServiceBus Publish()하는 방법
- 22. jQuery 및 탭을 사용하여 특정 간격으로 div에 내용을 다시로드하십시오.
- 23. 특정 시간 간격으로 Ruby의 랙 응용 프로그램 데이터 가져 오기
- 24. C#에서 특정 시간 간격으로 텍스트를 서식있는 텍스트 상자에 추가합니다.
- 25. C++에서 특정 시간 간격으로 메서드를 실행하는 Windows 서비스
- 26. 특정 간격으로 사용자에게 알림 메시지 표시 (Ruby on Rails)
- 27. 특정 시간 간격으로 비디오 (mpg, wmv 등)에서 프레임 캡처
- 28. 특정 시간 간격으로 gwt 서블릿을 자동으로 실행하는 방법은 무엇입니까?
- 29. 내 응용 프로그램은 GUI를 원하지 않고 특정 간격으로 서비스를 시작합니다.
- 30. 특정 시간 간격으로 실행되는 응용 프로그램의 수를 추적하는 방법은 무엇입니까?
감사합니다. :) :) – nicky