Java 的 setFocusableWindowState 和 setAccelerator 不能很好地发挥作用
Java's setFocusableWindowState and setAccelerator don't play nicely
在 Windows 7 下,我有一个 Java 程序,它以 window 状态 JFrame.ICONIFIED
启动,我要求它不会窃取加载时聚焦。
我通过在调用 setVisible
之前将 setFocusableWindowState
设置为 false
并随后将其恢复为 true
来完成此操作。这工作正常,我的程序在后台有效加载。
但是,我注意到 none 的键盘加速器不再起作用,这是使用 setFocusableWindowState
的直接结果。我什至尝试在 window 可见后设置我的键盘加速器,但没有成功。下面的 SSCCE 演示了问题 - 如果我调用 setFocusableWindowState
.
,用户无法按 CTRL+T
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCCE extends JFrame implements ActionListener {
private JMenuBar mBar;
private JMenu mFile;
private JMenuItem miTest;
public SSCCE() {
setSize(300, 200);
mBar = new JMenuBar();
mFile = new JMenu("File");
miTest = new JMenuItem("Test");
miTest.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
miTest.addActionListener(this);
mFile.add(miTest);
mBar.add(mFile);
setJMenuBar(mBar);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.ICONIFIED);
setFocusableWindowState(false);
setVisible(true);
setFocusableWindowState(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(miTest)) {
System.out.println("Testing...");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SSCCE();
}
});
}
}
我在 Java 7 和 Java 8 的最新补丁版本中看到了相同的行为。这看起来像错误,但是否愿意接受其他建议?
I have even tried setting my keyboard accelerators after the window is visible, but with no luck.
我在最后添加了菜单栏,它对我来说没问题:
//setJMenuBar(mBar);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.ICONIFIED);
setFocusableWindowState(false);
setVisible(true);
setFocusableWindowState(true);
setJMenuBar(mBar);
在 Windows 7 下,我有一个 Java 程序,它以 window 状态 JFrame.ICONIFIED
启动,我要求它不会窃取加载时聚焦。
我通过在调用 setVisible
之前将 setFocusableWindowState
设置为 false
并随后将其恢复为 true
来完成此操作。这工作正常,我的程序在后台有效加载。
但是,我注意到 none 的键盘加速器不再起作用,这是使用 setFocusableWindowState
的直接结果。我什至尝试在 window 可见后设置我的键盘加速器,但没有成功。下面的 SSCCE 演示了问题 - 如果我调用 setFocusableWindowState
.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCCE extends JFrame implements ActionListener {
private JMenuBar mBar;
private JMenu mFile;
private JMenuItem miTest;
public SSCCE() {
setSize(300, 200);
mBar = new JMenuBar();
mFile = new JMenu("File");
miTest = new JMenuItem("Test");
miTest.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
miTest.addActionListener(this);
mFile.add(miTest);
mBar.add(mFile);
setJMenuBar(mBar);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.ICONIFIED);
setFocusableWindowState(false);
setVisible(true);
setFocusableWindowState(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(miTest)) {
System.out.println("Testing...");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SSCCE();
}
});
}
}
我在 Java 7 和 Java 8 的最新补丁版本中看到了相同的行为。这看起来像错误,但是否愿意接受其他建议?
I have even tried setting my keyboard accelerators after the window is visible, but with no luck.
我在最后添加了菜单栏,它对我来说没问题:
//setJMenuBar(mBar);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.ICONIFIED);
setFocusableWindowState(false);
setVisible(true);
setFocusableWindowState(true);
setJMenuBar(mBar);