Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过多线程的两个示例时钟窗口/_Java - Fatal编程技术网

Java 通过多线程的两个示例时钟窗口/

Java 通过多线程的两个示例时钟窗口/,java,Java,我正在尝试用多线程构建一个简单的GUI时钟。我的目的是创建两个相同的示例时钟窗口 public class JavaApplication9 { public static void main(String[] args) { JFrame clock = new TextClockWindow(); clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); clock.setVisible(true);

我正在尝试用多线程构建一个简单的GUI时钟。我的目的是创建两个相同的示例时钟窗口

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时,出现了一些错误。 方法中的main非静态变量不能在静态上下文中引用。

我的多线程代码:

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

请帮助查找它不起作用的原因。runnable正在正确写入….

是否有理由将
时钟
类声明为内部类

如果要在同一文件中声明类,请将
Clocks
类移到
MyClock
类之外,并删除
public
限定符。它将开始工作


1)在问题中添加“一些错误”,2)我相信您只能有一个GUI线程,但我不确定这是否与此相关。是的。我知道GUI有一个线程。但我正在尝试制作必须同时执行两个windows时钟的应用程序。