您好,我这里有一个java程序,我正在尝试制作进度条。 while循环在另一个类中,并且需要一段时间才能运行,因此该GUI应该显示进度。然而,当while循环运行时,直到while循环结束然后条形为100%时,条形才会移动。在while循环中,我有正常的东西,然后我添加了
WindowWin.pb.setValue(getPercent());
然而,这似乎并没有实际发生,直到循环结束或循环的最后一次运行。在这里搜索时,我看到了使用
的建议public static void progressUpdate() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowWin.pb.setValue(getPercent());
}
});
}
我可能没有使用这个权利,或者这可能不适合我的情况。所有帮助赞赏。感谢。
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class WindowWin extends JFrame implements ActionListener {
int max=100;
static int min=0;
public static boolean stop=false;
String out;
JPanel[] row = new JPanel[5];
JButton[] button = new JButton[5];
String[] buttonString = { "Go", "Copy to Clipboard", "Back", "Info", "Stop"};
int[] dimW = { 400, 200, 72, 328};
int[] dimH = { 40, 100, 25};
Dimension keyDim = new Dimension(dimW[0], dimH[0]);
Dimension displayDimension = new Dimension(dimW[0], dimH[1]);
Dimension butDim = new Dimension(dimW[1], dimH[0]);
Dimension infoDim = new Dimension(dimW[2], dimH[0]);
Dimension pbDim = new Dimension(dimW[3], dimH[2]);
Dimension stopDim = new Dimension(dimW[2], dimH[2]);
JEditorPane display = new JEditorPane();
JTextField keyIn = new JTextField(22);
JEditorPane msgIn = new JEditorPane();
static JProgressBar pb = new JProgressBar();
Font font = new Font("Calibri", Font.PLAIN, 14);
JScrollPane scrollerD = new JScrollPane(display,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollerM = new JScrollPane(msgIn,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
WindowWin c = new WindowWin();
}
WindowWin() {
super("Test");
// setDesign();
// setSize(380,250);
// setSize(460,500);
// setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// GridLayout grid = new GridLayout(4,3);
GridBagLayout grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
// FlowLayout South
for (int i = 0; i < 5; i++)
row[i] = new JPanel();
row[0].setLayout(f1);
for (int i = 1; i < 5; i++) {
row[i].setLayout(f2);
}
for (int i = 0; i < 5; i++) {
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
scrollerD.setBorder(new CompoundBorder(new TitledBorder(
new EmptyBorder(0, 0, 0, 0), "Out"), scrollerD
.getBorder()));
scrollerM.setBorder(new CompoundBorder(new TitledBorder(
new EmptyBorder(0, 0, 0, 0), "In"), scrollerM
.getBorder()));
keyIn.setBorder(new CompoundBorder(new TitledBorder(new EmptyBorder(0,
0, 0, 0), "Temp"), keyIn
.getBorder()));
/*
* for(int i = 0; i < 2; i++) { label[i] = new JLabel(labelString[i]);
* row[i].add(label[i]); //label[i].setVerticalAlignment(JLabel.TOP);
* //label[i].setHorizontalAlignment(JLabel.CENTER);
* label[i].setText(labelString[i]); }
*/
display.setFont(font);
display.setEditable(false);
display.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
display.setPreferredSize(displayDimension);
keyIn.setFont(font);
keyIn.setEditable(true);
keyIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
keyIn.setPreferredSize(keyDim);
msgIn.setFont(font);
msgIn.setEditable(true);
msgIn.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
msgIn.setPreferredSize(displayDimension);
pb.setMinimum(min);
pb.setMaximum(max);
pb.setStringPainted(true);
pb.setPreferredSize(pbDim);
for (int i = 0; i < 2; i++)
button[i].setPreferredSize(butDim);
for (int i = 2; i < 4; i++)
button[i].setPreferredSize(infoDim);
button[4].setPreferredSize(stopDim);
row[0].add(scrollerM);
add(row[0], gbc);
row[1].add(button[0]);
row[1].add(button[1]);
add(row[1], gbc);
row[2].add(button[2]);
row[2].add(keyIn);
row[2].add(button[3]);
add(row[2], gbc);
row[3].add(scrollerD);
add(row[3], gbc);
row[4].add(pb);
row[4].add(button[4]);
add(row[4], gbc);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/*
* public final void setDesign() { try { UIManager.setLookAndFeel(
* "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch(Exception e)
* { } }
*/
public static void updateBar(int newValue) {
pb.setValue(newValue);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == button[0]) {
}
if (ae.getSource() == button[1]) {
}
if (ae.getSource() == button[2]) {
}
if (ae.getSource() == button[3]) {
updateBar(50);
}
if (ae.getSource() == button[4]) {
}
}
public void clear() {
try {
display.setText("");
} catch (NullPointerException e) {
}
}
public void outd() {
display.setText("");
}
}