如何在java中实现Swing的线程
我想要一个java程序,它显示程序运行时经过的时间,一个计时器。 首先,我创建了一个线程和变量countSec、countMin和boolean stop。 最终,将创建一个包含上述值的字符串变量,然后创建一个JFrame和一个JLabel,用于在JFrame上显示字符串计时器。线程在变大1值之前休眠1000毫秒,每60秒countSec设置为零,countMin增加1 输入:如何在java中实现Swing的线程,java,multithreading,swing,Java,Multithreading,Swing,我想要一个java程序,它显示程序运行时经过的时间,一个计时器。 首先,我创建了一个线程和变量countSec、countMin和boolean stop。 最终,将创建一个包含上述值的字符串变量,然后创建一个JFrame和一个JLabel,用于在JFrame上显示字符串计时器。线程在变大1值之前休眠1000毫秒,每60秒countSec设置为零,countMin增加1 输入: class Timer implements Runnable { Thread thrd; priv
class Timer implements Runnable {
Thread thrd;
private static int countSec = 0;
private static int countMin = 0;
private static boolean stop = false; // set to false to stop the program.
String timeTimers = "sec: " + countSec + " min: " + countMin;
JFrame jfrm;
JLabel jlab;
Timer() {
jfrm = new JFrame("Timer In Java");
jfrm.setSize(770, 440);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLayout(new FlowLayout());
jlab = new JLabel();
jlab.setText(timeTimers);
jfrm.add(jlab);
jfrm.setVisible(true);
}
public void run() {
stop = false;
try {
do {
countSec++;
Thread.sleep(1000);
if(countSec >= 60) {
countSec = -1;
countMin++;
}
} while(!stop);
} catch(InterruptedException e) {}
}
}
class TimerDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Timer();
}
});
Timer t = new Timer();
Thread a = new Thread(t);
a.start();
}
}
期望输出:
在Swing程序中实现的一种工作计时器,每秒改变秒数,每分钟改变分钟数
收到的输出:
一种计时器,其标签显示秒:0分:0,但在所有秒数内都不会改变
考虑事项:
可能是因为Swing方法看不到线程实现,所以计时器仍然处于停止状态。注意我使用了
java.swing.Timer
,并且我更新了一个swing组件,而不仅仅是一个变量
这是一个非常快速且肮脏的代码,请注意。只是一些我基本上是在飞行中做的事情
timing = true;
timer = new javax.swing.Timer( 1000,
new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
long minuteTime = ( System.nanoTime() -
nanoTime ) /
( 60_000_000_000L );
view.setTimerLabel( minuteTime + " minutes" );
if( minuteTime > sitTime )
if( darkBg ) {
view.setBackground( Color.PINK );
darkBg = false;
} else {
view.setBackground( Color.PINK.
darker() );
darkBg = true;
}
}
} );
timer.setRepeats( true );
timer.start();
}
}
这是一个使用java.util.Timer的简单示例:
package test;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Test extends JFrame {
Timer t;
private int sec = 0;
private boolean stop = true; // set to false to stop the program.
JLabel lab;
JButton startBtn;
JButton stopBtn;
Test() {
super("Timer In Java");
this.setSize(770, 440);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
lab = new JLabel();
startBtn = new JButton("start");
startBtn.addActionListener((e)->{
stop = false;
});
stopBtn = new JButton("stop");
stopBtn.addActionListener((e)->{
stop = true;
});
lab.setText("sec : 0 , min : 0");
this.add(lab);
this.add(startBtn);
this.add(stopBtn);
}
@Override
public void setVisible(boolean b) {
super.setVisible(b);
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
if(!stop){
sec++;
SwingUtilities.invokeLater(()->{
lab.setText("sec : "+(sec%60)+" , min : "+(sec/60));
});
}
}
}
, 0, 1000);
}
public static void main(String[] args) {
Test t = new Test();
t.setVisible(true);
}
}
你的问题是什么?你似乎已经实现了一切。有什么问题吗?在run()中,countMin是递增的,但您没有修改jlab中显示的分钟数。另外,在主gui线程上使用Thread.sleep会导致整个gui暂停。我相信如果你用谷歌搜索“java swing timer”,你会得到更好的结果,但我会先创建一个线程(就像你所做的那样)。我会在swing之外创建这个。当您需要更改标签时,我会使用SwingUtilities.invokeLater()来更改标签。@Abra我希望能通过简单的web搜索找到像这样常见的东西。问题中的一句简单的话“我搜索了这些术语,没有发现任何我能理解的东西”就足够了。然而,这是一个常见的问题;几乎每天都有一个。