import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;import javax.swing.JOptionPane;
import javax.swing.Timer;public class innerclass { /**
 * @param args
 */
public static void main(String[] args) {
TalkingClock clock = new TalkingClock(1000, true);
clock.start(); // keep program running until user selects "Ok"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
} class TalkingClock {
public TalkingClock(int timeouts, boolean beep) {
this.timeouts = timeouts;
this.beep = beep;
} public void start() {
ActionListener listener = new TimeListen();
Timer t = new Timer(timeouts, listener);
t.start();
} private int timeouts;
private boolean beep; private class TimeListen implements ActionListener {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep)
Toolkit.getDefaultToolkit().beep();
}
}
}}

解决方案 »

  1.   

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type innerclassTest is accessible. Must qualify the allocation with an enclosing instance of type innerclassTest (e.g. x.new A() where x is an instance of innerclassTest).
    这是错误...
      

  2.   

    创建内部类的对象时需要把外部类对象的this传进去,但是这里没有外部类的对象,可以把内部类声明为static可以避免这个问题。
      

  3.   


    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.Timer;public class InnerClassTest
    {  
       public static void main(String[] args)
       {  
          TalkingClock clock = new TalkingClock(1000, true);
          clock.start();
          JOptionPane.showMessageDialog(null, "Quit program?");
          System.exit(0);
       }
    }
    class TalkingClock
    {  
      
       public TalkingClock(int timeouts, boolean beep)
       {  
          this.timeouts= timeouts;
          this.beep = beep;
       }   
       public void start()
       {
          ActionListener listener = new TimeListen ();
          Timer t = new Timer(timeouts, listener);
          t.start();
       }   private int timeouts;
       private boolean beep;   private class TimeListen implements ActionListener
       {  
          public void actionPerformed(ActionEvent event)
          {  
             Date now = new Date();
             System.out.println("At the tone, the time is " + now);
             if (beep) Toolkit.getDefaultToolkit().beep();
          }
       }
    }
    这样会出错吗?
      

  4.   

    或者这样改 :
    InnerClass outer =new InnerClass();
            InnerClass.TalkingClock clock = outer.new TalkingClock(1000,true);
            clock.start(); // keep program running until user selects "Ok"  
      

  5.   

    如果使用内部类,可以按我提供的两种方法进行修改。
    或者不使用内部类 ,把TalkingClock拿到外面,OK
     
      

  6.   

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    No enclosing instance of type innerclassTest is accessible. Must qualify the allocation with an enclosing instance of type innerclassTest (e.g. x.new A() where x is an instance of innerclassTest).
    这是错误...就是这个 第一个报的  第二个正确运行