簡體   English   中英

SwingWorker-如何在ActionEvent中刷新JTextfield

[英]SwingWorker - How to refresh a JTextfield during an ActionEvent

我有這個: 基本應用程序-JButton-JTextField

當我單擊JButton“連接”時,我想在JTextField(名稱為tf1)(0-> 100)中顯示一個計數器。 但是我希望GUI在JButton事件期間顯示計數。

因此,當我單擊JButton時,我會調用:

 public class Swing_Lab_2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestWindows tw = new TestWindows();
    }
}
class TestWindows extends JFrame {

    private final JPanel pnl;
    private final JTextField tf1;
    private final JButton btn;

    TestWindows() {
        super();
        pnl = new JPanel(new GridLayout(2, 2));
        tf1 = new JTextField();
        btn = new JButton("Connect");

        pnl.add(btn);
        pnl.add(new JPanel());
        pnl.add(tf1);

        this.setContentPane(pnl);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                goSequenceTest();
            }

        });

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    private void goSequenceTest() {
        CountWorker_TF cw = new CountWorker_TF();
        cw.execute();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(TestWindows.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        boolean result = cw.getStatus();
        System.out.print(String.format("in the interruption %b", result));
    }

    private class CountWorker_TF extends SwingWorker<Integer, String> {

        boolean finished = false;

        CountWorker_TF() {

        }

        @Override
        protected Integer doInBackground() throws Exception {
            for (int i = 0; i < 101; i++) {
                publish(String.format("%d", i));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestWindows.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            finished = true;
            System.out.print(String.format("in the swingWorker %b", finished));
            return 1;
        }

        public boolean getStatus() {
            return finished;
        }

        @Override
        protected void process(List<String> strings) {
            /* Affichage des publications reçues dans le textarea. */
            for (String s : strings) {
                tf1.setText(s);
            }
        }

    }

要查看秋千工作者是否工作,我在boolean result = cw.getStatus();上設置了一個斷點 ,我觀察到該計數器處於活動狀態,但未在Jtextfield中顯示該計數。

你能告訴我我做錯了什么嗎?

編輯:在這里,測試用例。 我在方法goSequenceTest()中放入了“ Thread.sleep(1000)”來模擬耗時的計算(我不知道這是否是個好主意); 我觀察到該計數器處於活動狀態,但未在Jtextfield中顯示該計數。

非常感謝!

超頻

我了解代碼有什么問題。 GUI事件期間 無法修改GUI! 現在看來很明顯……EDT有一個堆棧,其中充滿了要執行的操作。 當我單擊連接按鈕時,EDT在其堆棧中有一個新操作要執行。 直到該操作未完全處理,EDT才能繼續執行其堆棧上的下一個操作。

我嘗試與Swingworker一起工作的方法不是很好的方法。 在我的方法中添加“ Thread.sleep(1000)”是愚蠢的,因為我將EDT阻止了1000ms。

我通過使用兩個線程解決了我的問題:

CalculationThread處理我的耗時計算:

private class CalculationThread extends Thread {

        public CalculationThread() {
            super();
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestWindows.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println(String.format("I am processing a calculation consuming time %d", i));
            }
        }
    }

SwingWorkerThread處理我的SwingWorker。 (JOptionPane在這里查看處理消耗計算任務的線程是否正常工作):

  private class SwingWorkerThread extends Thread {

        boolean reloadFpga = false;

        public SwingWorkerThread() {
            super();
        }

        public void run() {
            Object[] options = {"Yes", "No"};
            String txt = "Would you like start counting ?";
            int reload = JOptionPane.showOptionDialog(TestWindows.this.getFrame(), txt, "Counting ?", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, null);
            reloadFpga = (reload == 0);

            if (reloadFpga) {
                CountWorker_TF cw = new CountWorker_TF();
                cw.execute();
                boolean result = false;
                while (!result) {
                    result = cw.getStatus();
                    System.out.print(String.format("In the SwingWorker %b \n", result));
                }
            }

        }

    }

goSequenceTest()方法如下所示:

private void goSequenceTest() {
        SwingWorkerThread tt = new SwingWorkerThread();
        tt.start();

        CalculationThread tt2 = new CalculationThread();
        tt2.start();

        System.out.print(String.format("End EDT"));
    }

在類TestWindows中添加以下getFrame()方法以查看項目的工作情況:

private Component getFrame() {
        return this;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM