我浏览了ApplicationListener,但那里没有。在Mac上,该应用程序具有同等的关注度。它的菜单在顶部菜单栏中。

另外,如果您知道这一点,您能否告诉我我的应用程序如何请求散焦?

最佳答案

windowActivated() windowDeactivated() 中的WindowListenerWindowAdapter的实现将告诉您何时激活或停用窗口。您不需要 ApplicationListener

附录:尽管在这种情况下不是必需的,但可以在此 ApplicationListener 中找到example中指定的附加功能的透明实现。

附录:另请参见How to Write Window Listeners

附录:我想我明白你的意思。在使用OSXAdapter的示例 -Dapple.laf.useScreenMenuBar=true 中,菜单在最后一个窗口(默认为HIDE_ON_CLOSE)关闭时消失。它不是最佳选择,但是About…Preferences菜单保留在应用程序菜单中;选择其中一个将恢复屏幕菜单。另一种可能性是修改com.apple.eawt.Application中的停靠菜单。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class WindowTest extends JFrame implements ActionListener,
    WindowListener, WindowFocusListener, WindowStateListener {

    public static final void main(String args[]) throws Exception {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WindowTest("One");
                new WindowTest("Two");
            }
        });
    }

    public WindowTest(String name) {
        super(name);
        this.setName(name);
        this.setLayout(new GridLayout(0, 1));
        createButton("Back");
        createButton("Front");
        createButton("Hide");
        this.addWindowListener(this);
        this.addWindowFocusListener(this);
        this.addWindowStateListener(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }

    private void createButton(String name) {
        JButton b = new JButton(name);
        this.add(b);
        b.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if ("Back".equals(s)) {
            this.toBack();
        } else if ("Front".equals(s)) {
            this.toFront();
        } else {
            this.setExtendedState(JFrame.ICONIFIED);
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowIconified(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowActivated(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowGainedFocus(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowLostFocus(WindowEvent e) {
        System.out.println(e);
    }

    @Override
    public void windowStateChanged(WindowEvent e) {
        System.out.println(e);
    }

}

09-04 19:15