我正在尝试在JFrame中显示当前时间。如何刷新JLabel中的文本,而不在每次必须更新文本时打开单独的框架?
这是我到目前为止的所有代码。
Test
public class Test{
static String timeDisplay = "";
public static class time extends Thread{
static int timeHours = 7;
static int timeMins = 30;
static int timeSecs = 0;
@Override
public void run(){
while(true){
try{
time.sleep(1000);
timeSecs++;
if(timeSecs == 60){
timeMins++;
timeSecs = 0;
}
if(timeMins == 60){
timeHours++;
timeMins = 0;
}
if(timeHours < 10){
if(timeMins < 10){
if(timeSecs < 10){
timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = "0" + timeHours + ":" + "0" + timeMins + ":" + timeSecs;
}
}
else{
if(timeSecs < 10){
timeDisplay = "0" + timeHours + ":" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = "0" + timeHours + ":" + timeMins + ":" + timeSecs;
}
}
}
else{
if(timeMins < 10){
if(timeSecs < 10){
timeDisplay = timeHours + ":" + "0" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = timeHours + ":" + "0" + timeMins + ":" + timeSecs;
}
}
else{
if(timeSecs < 10){
timeDisplay = timeHours + ":" + timeMins + ":" + "0" + timeSecs;
}
else{
timeDisplay = timeHours + ":" + timeMins + ":" + timeSecs;
}
}
}
System.out.println(timeDisplay);
//CountDown time = new CountDown(timeDisplay);
}
catch(Exception e){
System.out.println("Something went wrong :(");
}
}
}
}
public static void main(String[] args){
time time = new time();
time.start();
try {
TimeUnit.SECONDS.sleep(1);
CountDown window = new CountDown(timeDisplay);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 500);
window.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
}CountDown
public class CountDown extends JFrame{
private static final long serialVersionUID = 1L;
static JLabel label = new JLabel();
public CountDown(String time){
super("Title");
setLayout(new FlowLayout());
add(label);
label.setText("Current Time: " + time);
Handler eventHandler = new Handler();
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==""){
string = String.format("label 1: %s", event.getActionCommand());
}
}
}
}我对这个程序的意图是制作一个显示当前时间的帧。它使用的是程序中的本地时间,而不是实际时间。提前感谢,如果我应该修改代码中的任何内容以使其更好,请随时告诉我。
发布于 2017-02-22 11:00:05
API文档将成为您的朋友。请看一下JLabel的文档,特别是setText()方法。可以在事件处理程序中再次使用此方法来更改标签的文本。
但另一个问题是,您既没有触发也没有注册事件,因此您编写的actionPerformed函数永远不会被调用。如果秒精度足够好,您可以使用javax.swing.Timer极大地简化代码。不需要在不必要的地方更改代码,这应该会让你走上正确的道路:
public class CountDown extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel label = new JLabel(); // note: understand static keyword before using it.
private long startTime = System.currentTimeMillis(); // gets the current time in milliseconds, when your class is initialized.
public CountDown(String time){
super("Title");
setLayout(new FlowLayout());
add(label);
// label.setText("Current Time: " + time); "time" was never in scope here.
Handler eventHandler = new Handler();
new Timer(1000, eventHandler).start(); // will execute ~1/sec
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
// NOTE: here, you could put all of your logic that was previously
// in your Thread to determine the time, then use the result
// with label.setText();
long currentTime = System.currentTimeMillis();
long upTime = currentTime - startTime; // this is how many milliseconds your JFrame has been up and running.
// TODO: formate upTime however you desire.
label.setText( <whatever_you_calculate_directly_above> );
}
}
}
}你还需要重写main方法来初始化你的框架。这应该就行了。
public static void main( String[] args){
JFrame frame = new CountDown();
frame.setVisible(true);
}如果main方法位于单独的文件中,则需要在该文件开头附近导入CountDown。
发布于 2017-02-22 11:11:59
您应该首先创建一个CountDown实例,然后调用它的一个方法来设置标签的文本。
在CountDown中,您必须调用JLabel.setText。方法来设置标签的文本:
public void displayTime(String time){
label.setText("Current Time: " + time);
}用...and代替System.out.println(time)调用
time.displayTime(timeDisplay);与其使用简单的线程,不如使用更适合gui对象的java.util.Timer,甚至更好的javax.swing.Timer。
另一个备注:类名以大写字母开头,而方法或变量以小写方法开头。
最后,你的处理程序在这里要做什么。你没有注册它,它似乎是无用的。
https://stackoverflow.com/questions/42381633
复制相似问题