[英]Continually set JTextArea text within ActionListener
我試圖創建一個按鈕,當按下該按鈕時,它將在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
。
有關更多詳細信息,請參見工作線程和SwingWorker以及如何使用Swing計時器 。
附帶說明,您還將遇到NullPointerException
...
Random rand = null;
int randomNum = rand.nextInt((max - min) + 1) + min;
使用Random
,您應該創建一個實例並在需要時重復使用。 這將確保您從中獲得的值正確(盡其所能)是隨機的,否則最終可能會在很短的時間內重復出現這些值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.