我想創建自己的窗口類,它擴展了JFrame
。但是,我對fullScreenBtn
的動作偵聽器存在問題。在編寫ActionListener.actionPerformed
函數時,我無法使用this
關鍵字,因爲它指的是new ActionListener
。我如何參考MyWindow
的實例?'this'的範圍問題關鍵字
public class MyWindow extends JFrame {
private static GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
private static GraphicsDevice gDev = gEnv.getDefaultScreenDevice();
private static JPanel toolbar = new JPanel();
private static JButton fullScreenBtn = new JButton("Show Full Screen");
private static boolean isFullScreen = false;
public MyWindow() {
toolbar.setLayout(new FlowLayout());
this.getContentPane().add(toolbar, BorderLayout.PAGE_START);
fullScreenBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Toggle full screen window
this.setUndecorated(!isFullScreen);
this.setResizable(isFullScreen);
gDev.setFullScreenWindow(this);
isFullScreen = !isFullScreen;
if (isFullScreen) {
fullScreenBtn.setText("Show Windowed");
} else {
fullScreenBtn.setText("Show Full Screen");
}
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
this.dispose();
System.exit(0);
}
});
}
}
只是投票關閉這個問題,這是一個明顯的重複。 – 2013-03-23 14:49:52
感謝您的快速回復!我知道必須有一個簡單的方法來做到這一點。你會如何推薦創建這種類而不擴展JFrame? – MTCoster 2013-03-23 14:51:17
@MTCoster:請參閱編輯。 – 2013-03-23 14:52:55