全屏快捷键

huangapple go评论70阅读模式
英文:

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_MASKKeyEvent.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);
});

huangapple
  • 本文由 发表于 2020年5月3日 22:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61576224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定