英文:
swing full screen shortcut
问题
我想创建一个具有shift + f11
加速键的JMenuItem
。
通过按下shift + f11
或点击JMenuItem
来使其全屏显示。
有人有什么建议吗?
JMenuItem toggle_full_screenFull = new JMenuItem("切换全屏");
toggle_full_screenFull.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11 , InputEvent.SHIFT_DOWN_MASK));
英文:
I want to create a JMenuItem
which has an Accelerator of shift + f11
.
by pressing shift + f11
or clicking on JMenuItem
it must get fullScreen.
does anyone have any advice?
JMenuItem toggle_full_screenFull = new JMenuItem("Toggle Full Screen");
toggle_full_screenFull.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11 , InputEvent.SHIFT_DOWN_MASK));
答案1
得分: 1
以下是翻译好的部分:
以下代码适用于我:
public class FullScreenExample extends JFrame {
public FullScreenExample() {
super("");
JMenuBar menuBar = new JMenuBar();
JMenu homeMenu = new JMenu("主页");
JMenuItem fullScreen = new JMenuItem("全屏模式");
fullScreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11, KeyEvent.SHIFT_MASK));
fullScreen.addActionListener(e -> setExtendedState(JFrame.MAXIMIZED_BOTH));
homeMenu.add(fullScreen);
menuBar.add(homeMenu);
setJMenuBar(menuBar);
setLocationByPlatform(true);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new FullScreenExample().setVisible(true));
}
}
而且它可以使用KeyEvent.SHIFT_DOWN_MASK
或KeyEvent.SHIFT_MASK
。
现在,如果您想要使它像启用/禁用全屏模式一样工作:
fullScreen.addActionListener(e -> {
boolean isNormal = getExtendedState() == JFrame.NORMAL;
setExtendedState(isNormal ? JFrame.MAXIMIZED_BOTH : JFrame.NORMAL);
});
英文:
The following code works for me:
public class FullScreenExample extends JFrame {
public FullScreenExample() {
super("");
JMenuBar menuBar = new JMenuBar();
JMenu homeMenu = new JMenu("home");
JMenuItem fullScreen = new JMenuItem("full screen");
fullScreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11, KeyEvent.SHIFT_MASK));
fullScreen.addActionListener(e->setExtendedState(JFrame.MAXIMIZED_BOTH));
homeMenu.add(fullScreen);
menuBar.add(homeMenu);
setJMenuBar(menuBar);
setLocationByPlatform(true);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new FullScreenExample().setVisible(true));
}
}
And it works either with KeyEvent.SHIFT_DOWN_MASK
or KeyEvent.SHIFT_MASK
.
Now, if you want to make it function like enable/disable full screen mode:
fullScreen.addActionListener(e -> {
boolean isNormal = getExtendedState() == JFrame.NORMAL;
setExtendedState(isNormal ? JFrame.MAXIMIZED_BOTH : JFrame.NORMAL);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论