JTextField 클래스의 setText() 메서드에 문제가 있습니다. 즉, 아래의 CounterPanel 클래스에서는 작동하지 않습니다. run() 메서드에서 호출되며 텍스트 필드를 업데이트하지 않습니다. 나머지 코드가 실행되었습니다. (왼쪽에있는 println() 문을 사용하여 콘솔에 인쇄 할 수 있습니다.JTextField setText() 메서드가 run() 메서드에서 작동하지 않습니다.
이 패널은 아래에 포함 된 MainWindow 클래스에 추가됩니다. MainWindow의 CounterPanels와 각각의 스레드는 자체 스레드를 얻습니다. 앞에서 말했듯이 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();
}
}
+1을 얻는다! – TechSpellBound
David에게 감사드립니다. GUI 및 스레딩을 시작하기 만하면됩니다. – user1061799