当按下 Jbutton 并在 java 中执行其定义的功能时,为什么我不能在我的应用程序中执行任何操作?
why i can't do anything in my application when Jbutton is pressed and performing its defined function in java?
这就是我尝试完成此任务的方式:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
我将 Enter 按钮设置为默认按钮,所以当我一直按下它时,按钮可能会按下 100 次或更多次,但因为我正在使用 Thread.sleep(1000)
它需要一些时间,所以我有时间输入我的JtextField 甚至关闭 window 但什么也做不了。
此外,我尝试将 btnNewButton.addActionListener()
放入线程的 运行 方法中,但没有区别。
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
}
});
thread.start();
// I used try catch block in real code
谁能帮我解决这个问题?
**我正在使用 windowsBuilder 在 Eclipse 中创建此应用程序。
不完全确定你的代码应该做什么,但如果你想按下按钮,然后在 1000 毫秒后它会检查你的字段,你应该这样做:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("before 1000ms");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after 1000ms");
System.out.println("Reading the text field ...");
}
});
thread.start();
System.out.println("after the thread");
输出:
after the thread
before 1000ms
after 1000ms
Reading the text field ...
这是因为称为 EDT 的东西 - 事件调度线程。
这个特殊用途线程负责您 GUI 中的所有事件 - 包括绘图(刷新)和交互(按钮单击)。这意味着,用于绘制应用程序的线程与用于执行 actionPerformed
代码的线程完全相同。由于您实际上是将该线程暂停了一段时间,因此您的应用程序将在该确切时间段内没有响应。
由于不阻塞应用程序,在用户交互中执行的任何操作都应该简短且执行速度快。如果你需要做一些繁重的事情,有一个名为 SwingWorker
的工具可以让你轻松地在后台线程(池)中进行一些处理并在执行期间安排 UI 更新(如更新进度条)
在你的第二个“线程”片段中,你的线程除了向按钮添加 actionListener
之外没有做任何事情,然后它终止(可能在不到 1 毫秒后;)) - 动作回调仍然由美国东部时间。如果您 start
来自 run
方法内部的自定义线程 - 那么它确实是并行的并且 GUI 不会被冻结 - 但同样,SwingWorker
是去这里的路。
这就是我尝试完成此任务的方式:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
我将 Enter 按钮设置为默认按钮,所以当我一直按下它时,按钮可能会按下 100 次或更多次,但因为我正在使用 Thread.sleep(1000)
它需要一些时间,所以我有时间输入我的JtextField 甚至关闭 window 但什么也做不了。
此外,我尝试将 btnNewButton.addActionListener()
放入线程的 运行 方法中,但没有区别。
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
}
});
thread.start();
// I used try catch block in real code
谁能帮我解决这个问题?
**我正在使用 windowsBuilder 在 Eclipse 中创建此应用程序。
不完全确定你的代码应该做什么,但如果你想按下按钮,然后在 1000 毫秒后它会检查你的字段,你应该这样做:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("before 1000ms");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after 1000ms");
System.out.println("Reading the text field ...");
}
});
thread.start();
System.out.println("after the thread");
输出:
after the thread
before 1000ms
after 1000ms
Reading the text field ...
这是因为称为 EDT 的东西 - 事件调度线程。
这个特殊用途线程负责您 GUI 中的所有事件 - 包括绘图(刷新)和交互(按钮单击)。这意味着,用于绘制应用程序的线程与用于执行 actionPerformed
代码的线程完全相同。由于您实际上是将该线程暂停了一段时间,因此您的应用程序将在该确切时间段内没有响应。
由于不阻塞应用程序,在用户交互中执行的任何操作都应该简短且执行速度快。如果你需要做一些繁重的事情,有一个名为 SwingWorker
的工具可以让你轻松地在后台线程(池)中进行一些处理并在执行期间安排 UI 更新(如更新进度条)
在你的第二个“线程”片段中,你的线程除了向按钮添加 actionListener
之外没有做任何事情,然后它终止(可能在不到 1 毫秒后;)) - 动作回调仍然由美国东部时间。如果您 start
来自 run
方法内部的自定义线程 - 那么它确实是并行的并且 GUI 不会被冻结 - 但同样,SwingWorker
是去这里的路。