2013-01-11 45 views
2

我遇到了JTextField類的setText()方法問題。簡而言之,它在下面的CounterPanel類中不起作用。它在run()方法中被調用,並且不會更新文本字段。其餘代碼運行(它可以使用我留下的println()語句打印到控制檯。JTextField setText()方法在run()方法中不起作用

這些面板被添加到MainWindow類中,我也在下面包含這些面板。 CounterPanels在MainWindow中並且每個都有自己的線程,正如我所說的,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(); 
    } 

} 

回答

0

您的字符串counterAsString沒有得到每次更新INT計數器得到

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++; 
} 
+0

確實沒有!感謝Kostronor。 – user1061799