这里是代码 -奇怪的摇摆编译时错误无障碍
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public final class SetLabelForDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JLabeledButton("foo:")); // new JLabeledButton("foo:") is the problem
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private final class JLabeledButton extends JButton{
public JLabeledButton(final String s){
super();
JLabel label = new JLabel(s);
label.setLabelFor(this);
}
}
}
,这里是错误消息 -
型SetLabelForDemo没有外围实例访问。必须 使用封闭实例类型 SetLabelForDemo(例如x.new A(),其中x是 SetLabelForDemo的实例)来限定分配。
我完全不理解这个错误。对我而言,一切似乎完全有效。我错过了什么吗?
这是一个糟糕的问题。 :/ – mre
有时发生,如果你离开训练大脑在这个论坛上:-)不可置疑的问题+1 – mKorbel
可能重复的[Java - 没有封闭实例类型Foo是可访问的](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian