4

を拡張する独自のウィンドウ クラスを作成しようとしていますJFrame。ただし、 のアクション リスナーに問題がありfullScreenBtnます。関数を書くとき、キーワードが参照しているActionListener.actionPerformedため使用できません。代わりにのインスタンスを参照するにはどうすればよいですか?thisnew ActionListenerMyWindow

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);
            }
        });
    }
}
4

4 に答える 4

7

内側のクラスthisでは、外側のクラスへの参照を取得する必要がある場合、外側のクラスのクラス名を使用する必要があります。たとえば、次のように使用します。

MyWindow.this.setUndecorated(...)` 
//  etc...

余談ですが、ここでは、ほとんどの場合、JFrame を拡張したくありません。

また、JButton を保持する祖先 Window は、 via などの他の方法で取得できますSwingUtilities.getWindowAncestor(theButton)。つまり、

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof JButton) {
              JButton button = (button) source;
              Window ancestorWin = SwingUtilities.getAncestorWindow(button);
              ancestorWin.setUndecorated(!isFullScreen);
              ancestorWin.setResizable(isFullScreen);
              // etc...

または、祖先ウィンドウが JFrame であることを最も明確に知っている場合:

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof JButton) {
              JButton button = (button) source;

              JFrame ancestorWin = (JFrame) SwingUtilities.getAncestorWindow(button);
              ancestorWin.setUndecorated(!isFullScreen);
              ancestorWin.setResizable(isFullScreen);
              // etc...
于 2013-03-23T14:48:11.433 に答える
3

これは、内部クラスまたは匿名クラスから囲んでいるクラスのインスタンスにアクセスする構文です。

OuterClass.this.foo();
于 2013-03-23T14:50:24.117 に答える
1

を使用しているためanonymous classthisはそのクラスを参照します。この場合はActionListener. ActionListenerなどのメソッドがないためsetUndecorated、コンパイル エラーが発生します。

あなたがしたいのは、を使用しMyWindow.this、その後に任意のメソッドを続けることですMyWindow

于 2013-03-23T14:50:45.977 に答える
1

外部クラスも指定してアクセスする必要がありthisます。あなたの場合は、次のようにする必要があります。

MyWindow.this
于 2013-03-23T14:51:35.973 に答える