0
멀티 스레딩으로 간단한 GUI 시계를 만들려고합니다. 내 목적은 두 개의 동일한 exampl 시계 창을 만드는 것입니다.다중 스레드를 통한 두 개의 시계 창/
public class JavaApplication9 {
public static void main(String[] args) {
JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}//end main}
class TextClockWindow extends JFrame {
private JTextField timeField; // set by timer listener
private JButton listener;
public TextClockWindow() {
// GUI
timeField = new JTextField(6);
timeField.setFont(new Font("sansserif", Font.PLAIN, 48));
JButton button1 = new JButton("Action");
add(button1);
button1.addActionListener((ActionListener) listener);
ActionListener listener=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){setBackground(Color.red);
}
};
Container content = this.getContentPane();
content.setLayout(new FlowLayout());
content.add(timeField);
this.setTitle("My_simple_clock"); this.pack();
// Create a 1-second timer and action listener for it.
// Specify package because there are two Timer classes
javax.swing.Timer t = new javax.swing.Timer(1000,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
timeField.setText("" + h + ":" + m + ":" + s);
}
});
t.start();
}
이 Runnable를 일부 에러가 발생한 사용하려고 wheni multithreading.But없이 코드입니다. 메소드에서 Main Non Static 변수는 정적 컨텍스트에서 참조 할 수 없습니다. 멀티 스레딩과
내 코드 :
public class MyClock{
public static void main(String[] args) {
Runnable r=new Clocks();
Thread n=new Thread(r);
n.start();
}
public class Clocks implements Runnable {
public Clocks() {}
public void run() {JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}
제대로 쓰고 work.runnable없는 이유를 찾을 수 있도록 도와주십시오 ....
1) 질문에 "몇 가지 오류"를 추가하고 2) 난 당신이 하나의 GUI 스레드를 가질 수 있다고 생각하지만 여기에 관련이 있는지 확실하지 않습니다. – nablex
예. GUI에 하나의 thread가 있습니다.하지만 두 개의 병렬 창 시계를 실행해야하는 응용 프로그램을 만들려고합니다. – flyhorne99