我正在尝试创建一个按钮,当按下时,将在JTextArea
中显示文本,每次循环中的一个步骤在等待期后运行时不断更新。我现在拥有的代码打印了它在循环第一步时的样子,然后停止。我所指的动作在//Actions
下面的底部。
public class MainWindow {
private JFrame frmMcakennaAntivirus;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frmMcakennaAntivirus.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMcakennaAntivirus = new JFrame();
frmMcakennaAntivirus.setResizable(false);
frmMcakennaAntivirus.setTitle("McAkenna Anti-Virus");
frmMcakennaAntivirus.setAlwaysOnTop(true);
frmMcakennaAntivirus.setBounds(100, 100, 303, 197);
frmMcakennaAntivirus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 149, 135, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 14, 0, 24, 45, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frmMcakennaAntivirus.getContentPane().setLayout(gridBagLayout);
JSeparator separator = new JSeparator();
GridBagConstraints gbc_separator = new GridBagConstraints();
gbc_separator.insets = new Insets(0, 0, 5, 5);
gbc_separator.gridx = 1;
gbc_separator.gridy = 0;
frmMcakennaAntivirus.getContentPane().add(separator, gbc_separator);
JSeparator separator_1 = new JSeparator();
GridBagConstraints gbc_separator_1 = new GridBagConstraints();
gbc_separator_1.insets = new Insets(0, 0, 5, 5);
gbc_separator_1.gridx = 0;
gbc_separator_1.gridy = 1;
frmMcakennaAntivirus.getContentPane().add(separator_1, gbc_separator_1);
JButton btnBeginScan = new JButton("Begin Scan");
btnBeginScan.setFont(new Font("Tahoma", Font.BOLD, 11));
btnBeginScan.setForeground(new Color(0, 0, 0));
btnBeginScan.setBackground(new Color(124, 252, 0));
GridBagConstraints gbc_btnBeginScan = new GridBagConstraints();
gbc_btnBeginScan.insets = new Insets(0, 0, 5, 5);
gbc_btnBeginScan.gridx = 1;
gbc_btnBeginScan.gridy = 1;
frmMcakennaAntivirus.getContentPane().add(btnBeginScan, gbc_btnBeginScan);
JButton btnFixProblems = new JButton("Fix Problems");
btnFixProblems.setBackground(new Color(127, 255, 0));
GridBagConstraints gbc_btnFixProblems = new GridBagConstraints();
gbc_btnFixProblems.insets = new Insets(0, 0, 5, 5);
gbc_btnFixProblems.gridx = 2;
gbc_btnFixProblems.gridy = 1;
frmMcakennaAntivirus.getContentPane().add(btnFixProblems, gbc_btnFixProblems);
JSeparator separator_2 = new JSeparator();
GridBagConstraints gbc_separator_2 = new GridBagConstraints();
gbc_separator_2.insets = new Insets(0, 0, 5, 0);
gbc_separator_2.gridx = 3;
gbc_separator_2.gridy = 1;
frmMcakennaAntivirus.getContentPane().add(separator_2, gbc_separator_2);
Choice choice = new Choice();
choice.setBackground(SystemColor.scrollbar);
GridBagConstraints gbc_choice = new GridBagConstraints();
gbc_choice.insets = new Insets(0, 0, 5, 5);
gbc_choice.fill = GridBagConstraints.HORIZONTAL;
gbc_choice.gridx = 1;
gbc_choice.gridy = 2;
frmMcakennaAntivirus.getContentPane().add(choice, gbc_choice);
choice.add("Full System Scan");
choice.add("Quick Scan");
JProgressBar progressBar = new JProgressBar();
GridBagConstraints gbc_progressBar = new GridBagConstraints();
gbc_progressBar.fill = GridBagConstraints.BOTH;
gbc_progressBar.insets = new Insets(0, 0, 5, 5);
gbc_progressBar.gridx = 1;
gbc_progressBar.gridy = 3;
frmMcakennaAntivirus.getContentPane().add(progressBar, gbc_progressBar);
final JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setFont(new Font("Source Sans Pro Light", Font.PLAIN, 12));
textArea.setEditable(false);
textArea.setBackground(SystemColor.inactiveCaption);
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.insets = new Insets(0, 0, 5, 5);
gbc_textArea.fill = GridBagConstraints.BOTH;
gbc_textArea.gridx = 1;
gbc_textArea.gridy = 4;
frmMcakennaAntivirus.getContentPane().add(textArea, gbc_textArea);
JButton btnCancelScan = new JButton("Cancel Scan");
btnCancelScan.setBackground(new Color(255, 0, 0));
GridBagConstraints gbc_btnCancelScan = new GridBagConstraints();
gbc_btnCancelScan.insets = new Insets(0, 0, 0, 5);
gbc_btnCancelScan.gridx = 1;
gbc_btnCancelScan.gridy = 5;
frmMcakennaAntivirus.getContentPane().add(btnCancelScan, gbc_btnCancelScan);
//Actions
btnBeginScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int x= 1; x < 6735; x++){
textArea.setText("Files scanned: " + x + "nViruses found: " + x/350);
try {
Thread.sleep(randInt(90, 200));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
public static int randInt(int min, int max) {
Random rand = null;
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
您正在阻塞事件调度线程,该线程负责处理重绘请求。
Swing是一个单线程框架,您永远不应该在EDT的上下文中运行长时间运行或阻塞操作。
参见Swing中的并发性了解更多细节
使用SwingWorker
或Swing Timer
代替。
参见Worker Threads和SwingWorker以及如何使用Swing Timers了解更多细节
作为旁注,您还将遇到NullPointerException
…
Random rand = null;
int randomNum = rand.nextInt((max - min) + 1) + min;
当使用Random
时,您应该创建一个实例并在需要时重用它。这将确保您从中获得的值是适当的(尽可能好)随机化的,否则您可能会在很短的时间内重复值