如何制作全屏java小程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/431044/
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 make full screen java applets?
提问by systemsfault
I am designing a psychology experiment with java applets. I have to make my java applets full screen. What is the best way of doing this and how can I do this.
我正在用 java 小程序设计一个心理学实验。我必须让我的 java 小程序全屏显示。这样做的最佳方法是什么,我该怎么做。
Since I haven't been using java applets for 3 years(The last time I've used it was for a course homework :) ) I have forgotten most of the concepts. I googled and found that link: Dani web
因为我已经 3 年没有使用 Java 小程序了(我最后一次使用它是为了课程作业:))我已经忘记了大部分概念。我用谷歌搜索并找到了该链接: Dani web
But in the method described in above link you have to put a JFrame inside the applet which I have no idea how to do it.
但是在上面链接中描述的方法中,您必须在小程序中放置一个 JFrame,我不知道该怎么做。
Whatever I need a quick and dirty method b'cause I don't have much time and this is the reason why I asked it here.
无论我需要一个快速而肮脏的方法,因为我没有太多时间,这就是我在这里问它的原因。
Thanx in advance
提前谢谢
采纳答案by sblundy
I think you want to use WebStart. You can deploy from a browser but it is otherwise a full blown application. There are a few browserish security restrictions, but, as you're using an Applet currently, I think I can assume they're not a problem.
我认为您想使用 WebStart。您可以从浏览器进行部署,但它是一个完整的应用程序。有一些浏览器安全限制,但是,由于您目前使用的是 Applet,我想我可以假设它们不是问题。
回答by Dan Dyer
The obvious answer is don't use applets. Write an application that uses a JFrame or JWindow as its top-level container. It's not a huge amount of work to convert an applet into an application. Applets are designed to be embedded in something else, usually a web page.
显而易见的答案是不要使用小程序。编写一个使用 JFrame 或 JWindow 作为其顶级容器的应用程序。将小程序转换为应用程序的工作量并不大。小程序被设计成嵌入在其他东西中,通常是网页。
If you already have an applet and want to make it full screen, there's two quick and dirty hacks:
如果您已经有一个小程序并希望使其全屏显示,则有两个快速而肮脏的技巧:
1). If you know the screen resolution, just set the applet parameters to be that size in the HTML and then run the browser in full screen mode.
1)。如果您知道屏幕分辨率,只需在 HTML 中将小程序参数设置为该大小,然后以全屏模式运行浏览器。
2). Run the applet in appletviewer, rather than a web page, and maximise the appletviewer window.
2)。在小程序查看器中运行小程序,而不是在网页中运行小程序,并最大化小程序查看器窗口。
回答by Ran Biron
Why not just open a new Frame from the applet (either from the "start()" method or, preferably, after the user presses an "open" button) and set it to be maximized?
为什么不直接从小程序中打开一个新框架(通过“start()”方法,或者最好在用户按下“打开”按钮后)并将其设置为最大化?
JFrame frame = new JFrame();
//more initialization code here
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width, dim.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Don't forget: The JFrame should be created and opened from the EDT. Applet start() is not guaranteed to be called on that thread, so use SwingUtilities.invokeLater(). Of course, if you opt for the button route, button listener is called on the EDT, so you should be safe.
不要忘记:应该从 EDT 创建和打开 JFrame。不能保证在该线程上调用 Applet start(),因此请使用 SwingUtilities.invokeLater()。当然,如果你选择按钮路由,按钮监听器会在 EDT 上调用,所以你应该是安全的。
回答by Jorge Piera Llodrá
I've found a solutionfor this problem that works fine. Tested in Linux 64 bits (Chrome and Firefox) and in Windows 7 64 bits (Chrome and Explorer)
我已经为这个问题找到了一个很好的解决方案。在 Linux 64 位(Chrome 和 Firefox)和 Windows 7 64 位(Chrome 和资源管理器)中测试
The only problem has been that my applet uses all the space in the browser and when the user switch off the full screen mode, the applet is not scaled to the browser size. The solution has been to keep the previous size of the applet before to enter in a fullscreen mode and then, set this size when the applet returns to the normal mode:
唯一的问题是我的小程序使用了浏览器中的所有空间,当用户关闭全屏模式时,小程序不会缩放到浏览器大小。解决方案是保持小程序之前的大小进入全屏模式,然后在小程序返回正常模式时设置此大小:
public void setFullScreen() {
if (!this.fullscreen) {
size = this.getSize();
if (this.parent == null) {
this.parent = getParent();
}
this.frame = new Frame();
this.frame.setUndecorated(true);
this.frame.add(this);
this.frame.setVisible(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = ge.getScreenDevices();
devices[0].setFullScreenWindow(this.frame);
this.fullscreen = true;
} else {
if (this.parent != null) {
this.parent.add(this);
}
if (this.frame != null) {
this.frame.dispose();
}
this.fullscreen = false;
this.setSize(size);
this.revalidate();
}
this.requestFocus();
}