问题描述
尝试在GUI中实现一个简单的时钟,该时钟将不断更新时间。我决定通过将标签设置为与日历输出等效的字符串,然后将其粘贴在while true循环中来做到这一点。问题是,当我尝试这样做时,应用程序将永远不会打开。没有错误消息或其他任何内容,只是无法打开Picture of the console window. 我将应用程序分为几个单独的Java类:File hierarchy
时间设置代码:
package prototype;
import java.util.Date;
/**
*
* @author PC
*/
public class TimeAndDate {
Date date = new Date();
void timedisplay(javax.swing.JLabel a){
String hours = String.valueOf(date.getHours());
String minutes = String.valueOf(date.getMinutes());
String seconds = String.valueOf(date.getSeconds());
while(true){
a.setText(hours + ":" + minutes + ":" + seconds);
}
}
使用上述方法的主要GUI表单的代码。
package prototype;
import javax.swing.JLabel;
/**
*
* @author PC
*/
public class MainMenu extends javax.swing.JFrame {
/**
* Creates new form MainMenu
*/
public MainMenu() {
TimeAndDate time = new TimeAndDate();
initComponents();
time.timedisplay(dateLbl);
}
如果我退出for循环,代码将正常工作(但当然,时间不会更新)。我唯一的猜测是,无限循环会使程序无法尝试处理太多东西,但是我找不到任何东西(或处理它)。想知道如何才能使此时钟刷新。干杯。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)