gpt4 book ai didi

java - 多线程程序无法运行

转载 作者:行者123 更新时间:2023-11-30 04:25:04 24 4
gpt4 key购买 nike

我正在努力完成这项工作。我创建一个窗口,其中包含一个文本字段和按钮,然后运行 ​​run() 方法,该方法应该刷新文本字段中的文本,当我单击按钮时,它应该将数字迭代 1。我想使这项工作同时进行,但我被困住了。它只是迭代数字,但不刷新文本字段中的值。您能以某种方式帮助我吗?我认为了解线程很容易,但是...不:-D 这是代码。

窗口类

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Okno extends JFrame implements ActionListener,Runnable {

private JFrame o = new JFrame();
private static JTextField t = new JTextField();
private JTextField t2 = new JTextField();
private static int x = 0;
protected JButton b = new JButton("KLIK");


Okno() {

o.setVisible(true);
o.setBounds(0, 0, 300, 200);
o.setLayout(null);
o.setDefaultCloseOperation(EXIT_ON_CLOSE);

t.setBounds(10, 10, 60, 20);
t2.setBounds(80, 10, 60, 20);
b.setBounds(50, 80, 60, 30);
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
while (true) {
Okno.work();
System.out.println("Klik");
}

}
});
o.add(t);
o.add(b);
o.add(t2);
}
public static int iter(){

x++;
return x;
}

public static void work(){
try {
iter();
System.out.println(x);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {

}
@Override
public void run() {
while(true){
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.setText(Integer.toString(x));
System.out.println("RUN");
}
}
}

主类

public class ThreadDemo {
public static void main(String args[]) {
Okno o = new Okno();

while(true){
o.run();
}
}
}

最佳答案

Swing 是单线程的。调用 Thread.sleep 会阻止 UI 更新。使用Swing Timer相反。

摘自 GETah 对 java stopwatch that updates gui every second 的回答:

Something along these lines should do it:

import java.awt.EventQueue;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;

/** @see https://stackoverflow.com/a/11058263/230513 */
public class Clock {

private Timer timer = new Timer();
private JLabel timeLabel = new JLabel(" ", JLabel.CENTER);

public Clock() {
JFrame f = new JFrame("Seconds");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(timeLabel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.schedule(new UpdateUITask(), 0, 1000);
}

private class UpdateUITask extends TimerTask {

int nSeconds = 0;

@Override
public void run() {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
timeLabel.setText(String.valueOf(nSeconds++));
}
});
}
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
final Clock clock = new Clock();
}
});
}
}

The timeLabel will always display the number of seconds the timer has been running.

  1. You will need to correctly format it to display "hh:mm:ss"; one approach is shown here.

  2. Create a container and add the label to it so that you can display it as part of the GUI.

  3. Compare the result to this alternate using javax.swing.Timer.

关于java - 多线程程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16132216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com