第一个代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cos {
public static int a;
private static JLabel labeler;
// public static Runnable r1;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
a = 0;
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
cos window = new cos();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public cos() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 205, 194);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblTime = new JLabel("Time:");
frame.getContentPane().add(lblTime, BorderLayout.WEST);
final JLabel labeler = new JLabel("");
frame.getContentPane().add(labeler, BorderLayout.CENTER);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
Runnable r1 = new Runnable() {
public void run() {
while (a <= 10) {
a = a + 1;
labeler.setText(Integer.toString(a));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
public void actionPerformed(ActionEvent arg0) {
Thread threder = new Thread(r1);
threder.start();
// liczniczek bla = new liczniczek();
}
});
frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
}
public void licznik() {
while (a < 60) {
a = a + 1;
labeler.setText(Integer.toString(a));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}现在我的问题来了。我想使用这样的代码:
Runnable r1 = new Runnable(){
public void run(){
licznik();
}
};但这并不管用。我必须做什么来分离这段代码?对不起,我的英语不好
西尔兰。
发布于 2011-11-07 06:51:46
切勿在EDT期间使用Thread#sleep(int),确保只有此线程才能正常工作(使用blockng )
Runnable r1 = new Runnable(){
public void run(){
licznik();
}
};比你调用普通的licznik();是错误的,你必须这样包装它
Runnable r1 = new Runnable(){
public void run(){
labeler.setText(Integer.toString(a));
}
};但同样,如果没有Thread#sleep(int),您有三个选择
1)将Thread修改为javax.swing.Timer
2)将Thread更改为Runnable#Thread,可以使用Thread#sleep(int)进行延迟,但输出到图形用户界面必须是
Runnable r1 = new Runnable(){
public void run(){
labeler.setText(Integer.toString(a));
}
};3)使用SwingWorker,这里的输出在EDT中,您也可以使用Thread#sleep(int)
示例Thread#sleep(int) during EDT
编辑
在编程语言中,cos)
实现了我在这里发布的所有三个选项
发布于 2011-11-07 06:26:47
你说“它不工作”是什么意思?这对我很管用。你是如何尝试使用这段代码的,当你运行它时,你会遇到什么错误或问题?而我自己则使用SwingWorker,并通过SwingWorker的publish/process方法对设置JLabel的文本。要了解有关如何使用它的更多信息,请参阅本教程:Concurrency in Swing
编辑
实际上,实现你想要的更简单的方法是根本不直接使用线程或Runnables,而是使用Swing计时器,因为它们就是为这种情况而构建的。有关这方面的更多信息,请查看Swing Timer Tutorial
发布于 2011-11-07 06:27:06
我猜您希望函数licznik()在单独的线程中运行。您创建了一个Runnable,但您必须执行更多操作才能执行它的run()方法。有几种方法可以做到这一点:
Runnable r1 = new Runnable(){
public void run(){
licznik();
}
};
new Thread(r1).start();或者你可以直接子类化Thread:
Thread r1 = new Thread(){
public void run(){
licznik();
}
};
r1.start();https://stackoverflow.com/questions/8030832
复制相似问题