gpt4 book ai didi

Java Swing ProgressBar 在计算时更新

转载 作者:行者123 更新时间:2023-12-01 22:09:34 25 4
gpt4 key购买 nike

类似问题有一些例子( Progress Bar Java ) 和 ( Java uploading percentage progressbar thread ) 但不明白如何让它适用于我的代码。

我的主面板有 2 个面板(其中一个是带有 2 个选项卡的 tabbedPane)

public class MainIRH {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
MainWindow window = new MainWindow();
JPanel mainPanel = new JPanel(new BorderLayout(0, 20));
mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

ControlPanel controlPanel = new ControlPanel();

tabbedPane.setBorder(new EmptyBorder(10, 10, 10, 10));
controlPanel.setBorder(new EmptyBorder(0, 20, 20, 20));

ECPanel ecPanel = new ECPanel();
SMRPanel smrPanel = new SMRPanel();

tabbedPane.addTab("Environnement Canada", null, ecPanel, "Convertir des données des stations d\'Environnement Canada");
tabbedPane.addTab("Station Météo Routière", null, smrPanel, "Convertir des données des stations météos routières");

mainPanel.add(tabbedPane, BorderLayout.NORTH);
mainPanel.add(controlPanel, BorderLayout.CENTER);
window.add(mainPanel);

new ControllerIRH(ecPanel, smrPanel, controlPanel);

window.setVisible(true);
}
});
}
}

重要的是这个对象:SMRPanel smrPanel = new SMRPanel();

在这个对象中,我有一个 JButton convertArchiveButton,我从 Controller 类添加了一个 ActionListener

public void addConvertArchiveButton(ActionListener ConvertArchiveButton) {
convertArchiveButton.addActionListener(ConvertArchiveButton);
}

这里是Controller中Listener的实现

private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
converter.convert();
}
}

converter 是执行冗长操作的类

File[] listOfFiles = folder.listFiles();

for (int file = 0; file < listOfFiles.length; file++) {

int progress = Math.round(file * 100 / listOfFiles.length);

//tried a lot of things here...

//...computing

}

我的示例尝试使用 EventQueueSwingUtilities 添加一些新的 Runnable,但没有成功。 SwingWorker 类会有用吗?不太明白用途。

基本上,int Progress 将被传递到 SMRPanel 中的 JProgressBar

使用 ControlAltDel 编辑答案:

仍然不起作用,converter.readFile(file); 没有被调用。

private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
File[] listOfFiles = folder.listFiles();

System.out.println(listOfFiles.length);

for (int i = 0; i < listOfFiles.length; i++) {
final File file = listOfFiles[i];
final int idx = (i * 100) / listOfFiles.length;
Thread t = new Thread(new Runnable() {
public void run() {
converter.readFile(file);

System.out.println(idx);

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
smrPanel.setProgressBarValue(idx);
}
});
}
});

try {
t.start();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
converter.convert(year);

}
}

最佳答案

您的问题是您正在 EDT 线程中运行循环。通过在另一个线程中运行此修复,然后在完成处理文件时在 EDT 中设置更新的进度值

private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
new Thread(new Runnable() {
public void run() {
for (int file = 0; file < listOfFiles.length; file++) {
final File f = listOfFiles[i];
final idx = file;
Thread t = new Thread(new Runnable() {
converter.convert(f);
swingUtilities.invokeLater(new Runnable() {
myProgressBar.setValue(idx);
});
t.start();
t.join();
});
}
}).start();
}
}

关于Java Swing ProgressBar 在计算时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32079993/

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