java - 在 ActionListener 中持续设置 JTextArea 文本

标签 java swing

我正在尝试创建一个按钮,按下该按钮后,将在 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 上下文中运行长时间运行或阻塞操作。

参见Concurrency in Swing了解更多详情

改用 SwingWorker 或 Swing Timer

参见Worker Threads and SwingWorkerHow to use Swing Timers了解更多详情

顺便说一句,您还会遇到 NullPointerException...

Random rand = null;
int randomNum = rand.nextInt((max - min) + 1) + min;

使用Random时,您应该创建一个实例并在需要时重复使用。这将确保您从中获得的值被正确(尽可能)随机化,否则您可能会在很短的时间内得到重复的值

关于java - 在 ActionListener 中持续设置 JTextArea 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323154/

相关文章:

java - 异常 : "Missing class indicator field from database row [UnmarshalRecordImpl()]." when unmarshalling XML using EclipseLink JAXB (MOXy)

java - 无法使用 Apache POI 读取 xls 文件的单元格颜色

java - 将 GUI 分成几类

java - 对多个 out.setText(); 的任何建议

java - 将图像保存在特定文件夹中并将路径存储在 mysql 中以便稍后显示

c# - 同时发送http请求的最佳方式

java - 如何使用下一个按钮在firebase中一一调用子节点

java - 当我的框架显示时,如何以编程方式打开 DateChooserPanel

Java 玻璃面板

java - 如何将对象中的数据插入到 Eclipse 生成的 JTable 中? (请不要数据库)