這裏是代碼 -奇怪的搖擺編譯時錯誤無障礙
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