这是我到目前为止,它可能是完全错误的...如何在线程内的JInternalFrame中执行可更新的JProgressBar?
该问题似乎是更新循环内的百分比,然后在SwingUtilities.invokeLater
程序中调用它的外部。
此外JInternalFrame
坐落在程序的其余部分,也许JDialog
会更好地使用。
将在这里进行必要了代码,也愿意,如果你需要更多的信息张贴在github整个项目。(如果被允许)
这是我的代码的第一个项目,因此任何帮助,将不胜感激!
package twoDMapEditor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.BorderFactory;
import javax.swing.JInternalFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class SaveActionListener implements ActionListener {
public void actionPerformed(ActionEvent me) {
final JProgressBar progressBar = new JProgressBar();
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setBorder(BorderFactory.createTitledBorder("Saving..."));
final JInternalFrame loadingDialog = new JInternalFrame();
loadingDialog.add(progressBar);
loadingDialog.pack();
loadingDialog.setVisible(true);
TwoDMapEditor.frame.add(loadingDialog);
TwoDMapEditor.frame.revalidate();
TwoDMapEditor.frame.repaint();
new Thread(new Runnable() {
public void run() {
final int percentage = 0;
int i = 0, j;
while (i < TwoDMapEditor.size[0]) {
j = 0;
while(j < TwoDMapEditor.size[1]){
if (!TwoDMapEditor.mySql.updateMapPiece(i, j)) {
System.out.println("Something went wrong");
}
//percentage = ((i * TwoDMapEditor.size[1]) + j+1) * 100/(TwoDMapEditor.size[0] * TwoDMapEditor.size[1]);
j++;
}
i++;
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
progressBar.setValue(percentage);
}
});
} catch (InvocationTargetException | InterruptedException e1) {
e1.printStackTrace();
}
try {
java.lang.Thread.sleep(100);
}
catch(Exception e) { }
}
}).start();
}
}
编辑:现在得到这个:
package twoDMapEditor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.JInternalFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class SaveActionListener implements ActionListener, PropertyChangeListener {
JProgressBar progressBar;
ProgressBar progressBarClass;
JInternalFrame loadingDialog;
SaveActionListener(){
progressBar = new JProgressBar();
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setBorder(BorderFactory.createTitledBorder("Saving..."));
loadingDialog = new JInternalFrame();
loadingDialog.add(progressBar);
loadingDialog.pack();
loadingDialog.setVisible(true);
TwoDMapEditor.frame.add(loadingDialog);
TwoDMapEditor.frame.revalidate();
TwoDMapEditor.frame.repaint();
}
class ProgressBar extends SwingWorker<Void, Void>{
public int percentage = 1;
protected Void doInBackground(){
int i = 0, j;
while (i < TwoDMapEditor.size[0]) {
j = 0;
while(j < TwoDMapEditor.size[1]){
if (!TwoDMapEditor.mySql.updateMapPiece(i, j)) {
System.out.println("Something went wrong");
}
percentage = ((i * TwoDMapEditor.size[1]) + j+1) * 100/(TwoDMapEditor.size[0] * TwoDMapEditor.size[1]);
progressBar.setValue(percentage);
System.out.println(percentage);
j++;
}
i++;
}
return null;
}
public void done(){
loadingDialog.dispose();
}
}
public void actionPerformed(ActionEvent me) {
progressBarClass = new ProgressBar();
progressBarClass.addPropertyChangeListener(this);
progressBarClass.execute();
}
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(TwoDMapEditor.saveActionListener.progressBarClass.percentage);
progressBar.setValue(TwoDMapEditor.saveActionListener.progressBarClass.percentage);
}
}
我会用'SwingWorker'作为初学者... – MadProgrammer
谢谢你一定会看看!全新的,所以需要学习很多! :) –