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();
}
}
}}
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();
}
}
}}
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).
这是错误...
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();
}
}
}
这样会出错吗?
InnerClass outer =new InnerClass();
InnerClass.TalkingClock clock = outer.new TalkingClock(1000,true);
clock.start(); // keep program running until user selects "Ok"
或者不使用内部类 ,把TalkingClock拿到外面,OK
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).
这是错误...就是这个 第一个报的 第二个正确运行