Java 如何在框架可见后调用 setUndecorated()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/875132/
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
How to call setUndecorated() after a frame is made visible?
提问by Lucky
In my Swing application, I want the ability to switch between decorated and undecorated without recreating the entire frame. However, the API doesn't let me call setUndecorated()
after the frame is made visible.
在我的 Swing 应用程序中,我希望能够在不重新创建整个框架的情况下在装饰和未装饰之间切换。但是,API 不允许我setUndecorated()
在框架可见后调用。
Even if i call setVisible(false)
, isDisplayable()
still returns true. The API says the only way to make a frame not-displayable is to re-create it. However, I don't want to recreate the frame just to switch off some title bars.
即使我打电话setVisible(false)
,isDisplayable()
仍然返回true。API 说使框架不可显示的唯一方法是重新创建它。但是,我不想仅仅为了关闭一些标题栏而重新创建框架。
I am making a full-screenable application that can be switched between fullscreen and windowed modes; It should be able to switch while maintaining the state, etc.
我正在制作一个可以在全屏和窗口模式之间切换的全屏应用程序;它应该能够在保持状态等的同时进行切换。
How do I do this after a frame is visible?.
框架可见后如何执行此操作?
采纳答案by Steve McLeod
You can't. That's been my experience when I tried to achieve the same.
你不能。这就是我尝试实现相同目标时的经验。
However if you have your entire UI in one panel which is in your frame, you can create a new frame and add that panel to the frame. Not so much work.
但是,如果您将整个 UI 放在框架中的一个面板中,则可以创建一个新框架并将该面板添加到框架中。没有那么多工作。
Something like this:
像这样的东西:
// to start with
JPanel myUI = createUIPanel();
JFrame frame = new JFrame();
frame.add(myUI);
// .. and later ...
JFrame newFrame = new JFrame();
newFrame.setUndecorated();
newFrame.add(myUI);
In Swing a panel (and indeed any instance of a component) can only be in one frame at a time, so when you add it to a new frame, it immediately ceases to be in the old frame.
在 Swing 中,面板(以及组件的任何实例)一次只能位于一帧中,因此当您将其添加到新帧时,它会立即停止在旧帧中。
回答by Tom Hawtin - tackline
Well, you are going to need different frame instance. You may be able to move the contents of your frame over without recreating that. The key here is to make your code not be reliant on a specific frame. This is a basic good practice in any case.
好吧,您将需要不同的框架实例。您可以将框架的内容移过去而无需重新创建。这里的关键是让你的代码不依赖于特定的框架。在任何情况下,这都是一个基本的良好做法。
回答by Yishai
Have you tried calling Frame.dispose()
and then changing it? Haven't tried it myself, but it might work.
你有没有试过打电话Frame.dispose()
然后改变它?自己没试过,不过应该可以。
If not, then what you can do is have the frame an inconsequential part of the class, with only the most minimal hooks to the highest level panel or panels necessarily, and just move those to the new frame. All the children will follow.
如果没有,那么您可以做的是让框架成为班级的一个无关紧要的部分,只需要最少的挂钩到最高级别的面板或面板,然后将它们移动到新框架。所有的孩子都会跟着。
回答by Steve McLeod
Have a look at https://tvbrowser.svn.sourceforge.net/svnroot/tvbrowser/trunk/tvbrowser/src/tvbrowser/ui/mainframe/MainFrame.java
In Method switchFullscreenMode()
:
在方法中switchFullscreenMode()
:
dispose();
...
setFullScreenWindow(...);
setUndecorated(true/false);
setBounds(mXPos, mYPos, mWidth, mHeight);
...
setVisible(true);
Actually there's a lot more stuff going on to hide various sidepanels that reappear if the mouse touches the sides.
实际上,还有更多的东西可以隐藏各种侧面板,如果鼠标触摸侧面,这些侧面板就会重新出现。
Also note that you must explicitly set the bounds. Window.setExtendedState(MAXIMIZED_BOTH)
interferes badly in timely vicinity of dispose(), because they both rely on multiple native events of the operating system, that are lost, should the window no be displayable at that split second.
另请注意,您必须明确设置边界。Window.setExtendedState(MAXIMIZED_BOTH)
在 dispose() 的及时附近严重干扰,因为它们都依赖于操作系统的多个本机事件,如果窗口在那一瞬间无法显示,这些事件就会丢失。
I don't recommend taking the default screen directly:
我不建议直接使用默认屏幕:
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
and instead use the Screen, your JFrame is currently on:
而是使用屏幕,您的 JFrame 当前处于:
setBounds(getGraphicsConfiguration().getBounds());
getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
Though it's currently the same, it might change in the future.
虽然现在是一样的,但将来可能会改变。
回答by Thomas
calling dispose()
releases the native window resources. then you can edit properties like undecorated and so on. then just call setVisible(true)
to recreate the window resources and everything works fine (the position and all compoenents won`t be changed)
调用dispose()
释放本机窗口资源。然后您可以编辑未装饰等属性。然后只需调用setVisible(true)
重新创建窗口资源,一切正常(位置和所有组件都不会改变)
dispose();
setUndecorated(true/false);
setVisible(true);
回答by Eng.Eyad S.
Here is a code in how to make ALT+ Enterenters Full Screen without the bar mode and Minimize with showing the Title bar and the Start bar:
这是如何使ALT+Enter在没有栏模式的情况下进入全屏和显示标题栏和开始栏的最小化的代码:
public class myTest extends JFrame{
//Your codes...
//if "ALT" key on hold and "Enter" key pressed with it
if (evt.isAltDown() && evt.getKeyCode() == evt.VK_ENTER) {
//if the JFrame has Title bar
if (isUndecorated()) {
//this will dispose your JFrame
dispose();
//here to set it with no Title bar
setUndecorated(false);
pack();
setLocationRelativeTo(null);
//as you dispose your JFrame, you have to remake it Visible..
setVisible(true);
} else {
dispose();
setUndecorated(true);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
}
//your codes
}
回答by Deepak Karamchandani
Try:
尝试:
dispose();
setUndecorated(true);
setVisible(true);
Check it Out. Hope it will help !!
一探究竟。希望它会有所帮助!