简体   繁体   English

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

[英]JTextField setText() method does not work in a run() method

I'm having trouble with the setText() method of the JTextField class. 我在JTextField类的setText()方法上遇到麻烦。 In short, it doesn't work in the CounterPanel class below. 简而言之,它在下面的CounterPanel类中不起作用。 It is called in the run() method and it does not update the text field. 它在run()方法中调用,并且不更新文本字段。 The rest of the code is run (it can be printed to console using the println() statements that I've left in. 其余代码将运行(可以使用我留下的println()语句将其打印到控制台。

These panels are added to the MainWindow class that I've also included below. 这些面板被添加到MainWindow类中,我也在下面包括了这些类。 There are 4 CounterPanels in MainWindow and each gets its own thread. MainWindow中有4个CounterPanels,每个都有自己的线程。 As I've said, the rest of the code in the run() method works OK, so can anyone tell me where I'm going wrong? 就像我说过的那样,run()方法中的其余代码工作正常,所以有人可以告诉我我要去哪里了吗?

Many thanks. 非常感谢。

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();
    }

}

The problem is: 问题是:

Swing components should be created and manipulated on Event Dispatch Thread via SwingUtilities.invokeXXX block. 应该通过SwingUtilities.invokeXXX块在Event Dispatch Thread上创建和操作Swing组件。

You dont do this. 你不这样做。 Have a read on: 阅读以下内容:

Your String counterAsString does not get updated every time the int counter gets 每次获取int计数器时,您的String 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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM