在 Java/Swing 的全屏程序中停止使用 Tab/Alt-F4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4462454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stopping the use of Tab/Alt-F4 in a full-screen program in Java/Swing
提问by James Rattray
I need a way to stop people using other programs while my Java program is running. i.e stopping people switch tabs and pressing Alt-F4.
我需要一种方法来阻止人们在我的 Java 程序运行时使用其他程序。即停止人们切换标签并按 Alt-F4。
回答by Harry Martland
To make the program full screen use;
使程序全屏使用;
window.setExtendedState(Frame.MAXIMIZED_BOTH); //maximise window
window.setUndecorated(true); //remove decorations e.g. x in top right
And to make the window always on top use(To stop people using other running programs);
并使窗口始终处于最佳使用状态(阻止人们使用其他正在运行的程序);
window.setAlwaysOnTop(true);
回答by Curtis
You're not going to be able to do this at the Java level - you'll need to put the operating system into a "Kiosk Mode" of some kind.
您将无法在 Java 级别执行此操作 - 您需要将操作系统置于某种“信息亭模式”。
Unsolicited commentary - Do you need this because you (or your customer) hate your users, and want them to curse you forever? Are you planning to add features like "shut down the computer" to your program?
不请自来的评论 - 您是否需要这个,因为您(或您的客户)讨厌您的用户,并希望他们永远诅咒您?您是否打算在您的程序中添加诸如“关闭计算机”之类的功能?
回答by user538442
If your looking for full screen support, this is the code I use. Should be enough to get you going. You just need a global boolean variable to say if the application is full screen or not. You can tinker with it to get it to display a you like.
如果您正在寻找全屏支持,这是我使用的代码。应该足以让你前进。您只需要一个全局布尔变量来说明应用程序是否全屏。您可以修改它以显示您喜欢的内容。
/**
* Method allows changing whether this window is displayed in fullscreen or
* windowed mode.
* @param fullscreen true = change to fullscreen,
* false = change to windowed
*/
public void setFullscreen( boolean fullscreen )
{
//get a reference to the device.
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode dispMode = device.getDisplayMode();
//save the old display mode before changing it.
dispModeOld = device.getDisplayMode();
if( this.fullscreen != fullscreen )
{ //are we actually changing modes.
//change modes.
this.fullscreen = fullscreen;
// toggle fullscreen mode
if( !fullscreen )
{
//change to windowed mode.
//set the display mode back to the what it was when
//the program was launched.
device.setDisplayMode(dispModeOld);
//hide the frame so we can change it.
setVisible(false);
//remove the frame from being displayable.
dispose();
//put the borders back on the frame.
setUndecorated(false);
//needed to unset this window as the fullscreen window.
device.setFullScreenWindow(null);
//recenter window
setLocationRelativeTo(null);
setResizable(true);
//reset the display mode to what it was before
//we changed it.
setVisible(true);
}
else
{ //change to fullscreen.
//hide everything
setVisible(false);
//remove the frame from being displayable.
dispose();
//remove borders around the frame
setUndecorated(true);
//make the window fullscreen.
device.setFullScreenWindow(this);
//attempt to change the screen resolution.
device.setDisplayMode(dispMode);
setResizable(false);
setAlwaysOnTop(false);
//show the frame
setVisible(true);
}
//make sure that the screen is refreshed.
repaint();
}
}