JTextField setText()方法在run()方法中不起作用

时间:2013-01-11 15:05:21

标签: java multithreading swing

我遇到了JTextField类的setText()方法的问题。简而言之,它在下面的CounterPanel类中不起作用。它在run()方法中调用,它不会更新文本字段。其余代码运行(可以使用我留下的println()语句打印到控制台。

这些面板被添加到MainWindow类中,我也在下面包含这些面板。 MainWindow中有4个CounterPanel,每个都有自己的线程。正如我所说,run()方法中的其余代码工作正常,所以有人能告诉我哪里出错了吗?

非常感谢。

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class CounterPanel extends JPanel implements Runnable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JLabel labelOne = new JLabel("Counter 1");
    private JTextField textFieldOne = new JTextField(3);
    private JLabel labelTwo = new JLabel("Counter 2");
    private JTextField textFieldTwo = new JTextField(3);

    private int counter;
    private String counterAsString = Integer.toString(counter);

    public CounterPanel() {
        this.setLayout(new MigLayout());
        this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        this.add(labelOne);
        this.add(textFieldOne);
        this.add(labelTwo);
        this.add(textFieldTwo);
    }

    @Override
    public void run() {
        while(counter < 100) {
            textFieldOne.setText(counterAsString);
            textFieldTwo.setText(counterAsString);
            System.out.println("Counter 1 = " + counterAsString + ", Counter 2 = " + counterAsString);
            counter++;
        }
        System.out.println("FINISHED");
    }

}




import java.awt.Color;

import javax.swing.*;

import net.miginfocom.swing.MigLayout;

public class MainWindow extends JFrame {

    private CounterPanel panel1 = new CounterPanel();
    private CounterPanel panel2 = new CounterPanel();
    private CounterPanel panel3 = new CounterPanel();
    private CounterPanel panel4 = new CounterPanel();

    private JLabel labelOne = new JLabel("A");
    private JLabel labelTwo = new JLabel("B");
    private JLabel labelThree = new JLabel("C");
    private JLabel labelFour = new JLabel("D");


    public MainWindow() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new MigLayout());
        this.add(labelOne, "gapright 20px");
        this.getContentPane().add(panel1, "wrap");
        this.add(labelTwo);
        this.getContentPane().add(panel2, "wrap");
        this.add(labelThree);
        this.getContentPane().add(panel3, "wrap");
        this.add(labelFour);
        this.getContentPane().add(panel4);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        MainWindow window = new MainWindow();
        window.setLocationRelativeTo(null);
        window.runThreads();
    }

    public void runThreads() {
        Thread panelThread1 = new Thread(panel1);
        Thread panelThread2 = new Thread(panel2);
        Thread panelThread3 = new Thread(panel3);
        Thread panelThread4 = new Thread(panel4);

        panelThread1.start();
        panelThread2.start();
        panelThread3.start();
        panelThread4.start();
    }

}

2 个答案:

答案 0 :(得分:5)

问题是:

应通过SwingUtilities.invokeXXX块在Event Dispatch Thread上创建和操作Swing组件。

你不这样做。请阅读:

答案 1 :(得分:0)

每次int计数器获取

时,您的字符串counterAsString都不会更新
while(counter < 100) {
            // put this here
            >>  counterAsString = Integer.toString(counter);
            textFieldOne.setText(counterAsString);
            textFieldTwo.setText(counterAsString);
            System.out.println("Counter 1 = " + counterAsString + ", Counter 2 = " + counterAsString);
            counter++;
}