当点击 JButton 触发的进程仍在处理时,如何更新 JLabel?
我正在编写一个简单的java程序。它需要三个参数。包含图像的文件夹、所需的输出文件夹和图像水印文件。该程序将输入文件夹中每个图像的水印图像复制到输出文件夹中。
我正在写一个简单的 GUI。用户输入三个参数并按下按钮。该程序有效,但我想向用户提供反馈。我希望能够在每次处理图像时更新 JLabel 字段。
在我的 WatermarkFolders 类中,我使用此代码片段向用户显示一些反馈:
Gui.FEEDBACK.setText(text);
问题是 JLabel 不会在每次处理图像时更新。处理完成后,它只会更新 100% 完整的输出。 有没有一种简单的方法可以异步更新。感谢您的回答。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Gui extends JPanel {
JButton button;
JFileChooser chooser;
JLabel inputLabel = new JLabel("Directorio donde estan las imagenes");
JTextField inputField = new JTextField(15);
JLabel outputLabel = new JLabel("Directorio donde guardar las copias con marca de agua");
JTextField outputField = new JTextField(15);
JLabel watermarkLabel = new JLabel("Archivo marca de agua (gif transparente)");
JTextField watermarkField = new JTextField(15);
public static JLabel FEEDBACK = new JLabel("Número de Imágenes: ");
JProgressBar pb;
public Gui() {}
public void go() {
JFrame frame = new JFrame("Añadir Marcas de Agua a todas las imágenes de un directorio");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("Añadir Marca de Agua a las imágenes");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String input = inputField.getText();
String output = outputField.getText();
String watermark = watermarkField.getText();
if(!input.equals("") && !output.equals("") && !watermark.equals("")) {
WatermarkFolders wm = new WatermarkFolders(input, output, watermark);
wm.run();
}
}
});
panel.add(inputLabel);
panel.add(inputField);
panel.add(outputLabel);
panel.add(outputField);
panel.add(watermarkLabel);
panel.add(watermarkField);
panel.add(button);
panel.add(FEEDBACK);
frame.getContentPane().add(BorderLayout.WEST,panel);
frame.setVisible(true);
frame.pack();
}
public static void main(String s[]) {
Gui gui = new Gui();
gui.go();
}
}
I am coding a simple java program. It takes three parameteres. A folder containing images, a desired output folder and an image watermark file. The programs copies a watermarked image of every image in the input folder into the output folder.
I am writing a simple GUI. The user introduces the three parameters and presses a button. The programm works but I want to provide feedback to the user. I want to be able to update a JLabel field everytime an image is processed.
In my WatermarkFolders class i use this code snippet to show the user some feedback:
Gui.FEEDBACK.setText(text);
The issue is that the JLabel does not update each time an image is processed. It just updates with the 100% complete output when the processing is done.
Is there an easy way to update this asynchrously. Thanks for the answers.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Gui extends JPanel {
JButton button;
JFileChooser chooser;
JLabel inputLabel = new JLabel("Directorio donde estan las imagenes");
JTextField inputField = new JTextField(15);
JLabel outputLabel = new JLabel("Directorio donde guardar las copias con marca de agua");
JTextField outputField = new JTextField(15);
JLabel watermarkLabel = new JLabel("Archivo marca de agua (gif transparente)");
JTextField watermarkField = new JTextField(15);
public static JLabel FEEDBACK = new JLabel("Número de Imágenes: ");
JProgressBar pb;
public Gui() {}
public void go() {
JFrame frame = new JFrame("Añadir Marcas de Agua a todas las imágenes de un directorio");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("Añadir Marca de Agua a las imágenes");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String input = inputField.getText();
String output = outputField.getText();
String watermark = watermarkField.getText();
if(!input.equals("") && !output.equals("") && !watermark.equals("")) {
WatermarkFolders wm = new WatermarkFolders(input, output, watermark);
wm.run();
}
}
});
panel.add(inputLabel);
panel.add(inputField);
panel.add(outputLabel);
panel.add(outputField);
panel.add(watermarkLabel);
panel.add(watermarkField);
panel.add(button);
panel.add(FEEDBACK);
frame.getContentPane().add(BorderLayout.WEST,panel);
frame.setVisible(true);
frame.pack();
}
public static void main(String s[]) {
Gui gui = new Gui();
gui.go();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,需要时间的进程不应该出现在 EDT 上。相反,您可以在更新 GUI 的同时使用 SwingWorker 类来运行长时间运行的任务。
您可以使用 SwingUtilities.invokeLater() 方法从另一个线程更新 GUI,或者您可以让 Swing 计时器检查任务的状态并在 GUI 仍在运行时更新 GUI。
Yes, processes that take time should not be on the EDT. Instead use a SwingWorker class to run the long running task while you make updates to the GUI at the same time.
You can use the SwingUtilities.invokeLater() method to update the GUI from another thread, or you could have a Swing Timer check the status of a task and update the GUI while it still runs.