No enclosing instance of type E is accessible.

时间:2024-01-07 14:31:56

No enclosing instance of type E  is accessible.

静态方法(main)中调用内部类,会出现这样的问题;

学习了:https://www.cnblogs.com/runnigwolf/p/5570810.html

http://blog.csdn.net/sunny2038/article/details/6926079

public class SwingObserverExample {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
JButton jButton = new JButton("Should I do it?");
jButton.addActionListener(new SwingObserverExample().new AngelListener());
jButton.addActionListener(new DevilListener());
jFrame.getContentPane().add(jButton);
jFrame.setVisible(true);
}
class AngelListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
System.out.println("Don't do it!");
} }
static class DevilListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
System.out.println("Just do it!");
} } }