本文介绍了Mac上的Java:如何检测应用程序何时收到焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了,而且它们没有。在Mac上,这个应用程序的重点是相当的;它的菜单位于顶部菜单栏中。

I looked through ApplicationListener, and they don't have it in there. On a Mac, it's when that application has the equivalent of focus; its menu is in the top menu bar.

另外,如果你知道这一点,你能告诉我我的应用程序可以如何请求去重点自己?

Also, if you know this, could you tell me how my application can request to de-focus itself?

推荐答案

执行 windowActivated() windowDeactivated() / code>在或将告诉您窗口何时被激活或停用。你不需要。

Implementations of windowActivated() and windowDeactivated() in WindowListener or WindowAdapter will tell you when a window is activated or deactivated. You don't need ApplicationListener for that.

附录:虽然在这种情况下不是必需的,在可以在这个。

Addendum: Although not required in this case, a transparent implementation of the additional functionality specified in ApplicationListener may be found in this example.

附录:另请参见。

Addendum: See also How to Write Window Listeners.

附录:我想我看到你的意思。在示例中,,它使用 -Dapple.laf.useScreenMenuBar = true ,菜单在最后一个窗口消失( HIDE_ON_CLOSE 默认)关闭。它不是最佳的,但是关于... 首选项菜单保留在应用程序菜单中;选择还原屏幕菜单。另一种可能性是修改 com.apple.eawt.Application 中的dock菜单。

Addendum: I think I see what you mean. In the example OSXAdapter, which uses -Dapple.laf.useScreenMenuBar=true, the menus disappear when the last window (HIDE_ON_CLOSE by default) closes. It's less than optimal, but the About… and Preferences menus remain in the application menu; choosing either restores the screen menu. Another possibility is to modify the dock menu in 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);
    }

}

这篇关于Mac上的Java:如何检测应用程序何时收到焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:10