我浏览了ApplicationListener,他们没有在那里。在Mac上,当应用程序具有相同的焦点时;它的菜单位于顶部菜单栏中。
另外,如果您知道这一点,您能否告诉我我的申请如何要求取消对焦?
答案 0 :(得分:5)
WindowListener
或WindowAdapter
中windowActivated()
和windowDeactivated()
的实施将告诉您何时激活或停用某个窗口。您不需要ApplicationListener
。
附录:虽然在这种情况下不需要,但可以在ApplicationListener
中找到example中指定的附加功能的透明实现。
附录:另见How to Write Window Listeners。
附录:我想我明白你的意思了。在使用-Dapple.laf.useScreenMenuBar=true
的示例OSXAdapter
中,当最后一个窗口(默认情况下为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);
}
}
答案 1 :(得分:1)
你可以告诉我我的申请是怎么回事 可以要求解散自己吗?
您可以尝试:
frame.toBack();
如果这不起作用,那么您可以图标化您的应用程序,在这种情况下焦点应该转到之前的应用程序。
frame.setExtendedState(...);
答案 2 :(得分:0)
Java编程语言与平台无关。您应该使用官方Java API Reference Documentation,而不是阅读Apple的参考文档。在那里,您可以找到JFrame,WindowListener和WindowAdapter的文档。您可以使用addWindowListener函数在JFrame上注册WindowListener。窗口监听器可用于拦截和处理各种与窗口相关的事件,包括激活/去激活(哪个窗口在顶部)或获得焦点/丢失焦点(哪个窗口将接收键盘事件)。如果您提供自己的WindowListener并且不想实现每个函数,则WindowAdapter可用于此目的,因为它实现了WindowListener,但为每个函数提供了空定义。至于散焦(在你的意思上),可以使用toBack,而toFront则相反。
修改的
大部分信息已在之前的帖子中提供过;但是,我补充说这是为了强调: