我試圖使一個框架與由兩個按鈕組成的面板組成,這兩個按鈕位於框架的底部。Java:不能從靜態上下文引用
public class ControlledBall extends JPanel {
public static void main(String[] args) {
JFrame Frame = new Viewer();
Frame.setSize(1000, 500);
Frame.setTitle("Bouncing Ball");
Frame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
Frame.setVisible(true);
}
public class Viewer extends JFrame {
JButton buttonGo = new JButton("GO");
JButton buttonStop = new JButton("STOP");
JPanel aPanel = new JPanel();
public Viewer() {
aPanel.add(buttonGo);
aPanel.add(buttonStop);
this.add(aPanel, BorderLayout.SOUTH);
}
}
}
這裏的問題是這樣的:
JFrame Frame = new Viewer();
它告訴我
ControlledBall.this不能從靜態上下文中引用
怎麼辦我修復它?
你真的不應該用大寫字母來啓動變量名,它會讓讀者感到困惑,並且也會讓編譯器感到困惑。 –
@HarryBlargle我很好,它是令人困惑的閱讀,但我希望有沒有Java編譯器那裏是與那個混淆^^ – xav
@xav它可以認爲你在靜態環境中使用java.awt.Frame –