[英]jLabel not changing text and jProgressbar not updating
I have a JFrame
.我有一个
JFrame
。 in the JFrame
is a JDesktopPane
, JComboBox
, several JLabel
s, and a JProgressBar
. JFrame
有一个JDesktopPane
、 JComboBox
、几个JLabel
和一个JProgressBar
。 I am facing two challenges:我面临两个挑战:
I want to update/change the text in one of the JLabel
s in the JFrame
by clicking a JButton
that is on a JInternalFrame
(the button does some calculations).我想通过单击
JInternalFrame
上的JButton
来更新/更改JFrame
JLabel
之一中的文本(该按钮进行一些计算)。
Upon clicking a JButton
that is on another JInternalFrame
(the button performs a small task), I want to use the JProgressBar
(progressbar is in JFrame
) to show the progress of work done.单击另一个
JInternalFrame
上的JButton
(该按钮执行一个小任务)后,我想使用JProgressBar
(进度条在JFrame
)来显示已完成工作的进度。
I use SwingUtilities.invokelater()
to perform the tasks done by the buttons.我使用
SwingUtilities.invokelater()
来执行按钮完成的任务。 am using NetBeans as my IDE.我使用 NetBeans 作为我的 IDE。
Hard to know what is happening without code, but probably the Event Dispatch Thread (EDT) is being blocked (or terminated?), eg by code being called by invokeLater
.没有代码很难知道发生了什么,但事件调度线程 (EDT) 可能被阻塞(或终止?),例如被
invokeLater
调用的代码。 The EDT is used to update the GUI and should not be used for (slow) non-GUI related calculations. EDT 用于更新 GUI,不应用于(慢)非 GUI 相关计算。 See tutorial The Event Dispatch Thread and subsequent for more details.
有关更多详细信息,请参阅教程事件调度线程和后续教程。
Example (without blocking):示例(无阻塞):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.LocalTime;
import javax.swing.*;
public class LabelProgress {
public static void main(String[] args) {
LabelProgress main = new LabelProgress();
SwingUtilities.invokeLater(() -> main.showInternal1());
SwingUtilities.invokeLater(() -> main.showInternal2());
}
private JFrame frame;
private JLabel label;
private JDesktopPane desktop;
private JProgressBar bar;
private int progress = 0;
private LabelProgress() {
label = new JLabel("Label: ");
desktop = new JDesktopPane();
bar = new JProgressBar(0, 100);
bar.setStringPainted(true);
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(label, BorderLayout.BEFORE_FIRST_LINE);
frame.add(desktop, BorderLayout.CENTER);
frame.add(bar, BorderLayout.AFTER_LAST_LINE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(600, 400);
frame.validate();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void showInternal1() {
JButton change = new JButton("Change");
change.addActionListener(this::doChange);
JInternalFrame internal = new JInternalFrame("Change Label");
internal.setLayout(new FlowLayout());
internal.add(change);
internal.setBounds(20, 20, 200, 150);
internal.setVisible(true);
desktop.add(internal);
}
private void showInternal2() {
JButton task = new JButton("Task");
task.addActionListener(this::doTask);
JInternalFrame internal = new JInternalFrame("Small Task");
internal.setLayout(new FlowLayout());
internal.add(task);
internal.setBounds(150, 100, 200, 150);
internal.setVisible(true);
desktop.add(internal);
}
private void doChange(ActionEvent ev) {
// using a SwingWorker:
// for demonstration I used an anonymous class, maybe a own class is better
SwingWorker<LocalTime , Void> worker = new SwingWorker<LocalTime , Void>() {
@Override
protected LocalTime doInBackground() throws Exception {
// not executed on the EDT - just get the current time
LocalTime someCalculation = LocalTime.now();
return someCalculation;
}
@Override
protected void done() {
// executed on EDT
try {
LocalTime resultOfSomeCalculation = get();
label.setText("Label: " + resultOfSomeCalculation.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
worker.execute();
}
private void doTask(ActionEvent ev) {
// no need to use SwingWorker
Thread thread = new Thread(this::slowTask);
thread.start();
}
private void slowTask() {
// not really that slow, just for demonstration
progress += 10;
if (progress > 100) progress = 100;
// and now switching to the EDT
SwingUtilities.invokeLater(() -> bar.setValue(progress));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.