11

我正在用 java 小程序设计一个心理学实验。我必须让我的 java applet 全屏显示。这样做的最佳方法是什么,我该怎么做。

由于我已经 3 年没有使用 java 小程序了(我最后一次使用它是为了完成课程作业 :))我已经忘记了大部分概念。我用谷歌搜索并找到了该链接: Dani web

但是在上面链接中描述的方法中,您必须在小程序中放置一个 JFrame,我不知道该怎么做。

无论我需要一个快速而肮脏的方法,因为我没有太多时间,这就是我在这里问它的原因。

提前感谢

4

4 回答 4

6

显而易见的答案是不要使用小程序。编写一个使用 JFrame 或 JWindow 作为其顶级容器的应用程序。将小程序转换为应用程序的工作量并不大。Applet 旨在嵌入到其他东西中,通常是网页。

如果您已经有一个小程序并想要使其全屏显示,那么有两个快速而肮脏的技巧:

1)。如果您知道屏幕分辨率,只需在 HTML 中将小程序参数设置为该大小,然后以全屏模式运行浏览器。

2)。在 appletviewer 中运行 applet,而不是在网页中运行,并最大化 appletviewer 窗口。

于 2009-01-10T14:59:25.623 回答
5

为什么不直接从小程序中打开一个新框架(通过“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);

不要忘记:应该从 EDT 创建和打开 JFrame。不能保证在该线程上调用 Applet start(),因此请使用 SwingUtilities.invokeLater()。当然,如果您选择按钮路由,则会在 EDT 上调用按钮侦听器,因此您应该是安全的。

于 2009-01-10T17:35:23.453 回答
4

我想你想使用 WebStart。您可以从浏览器进行部署,但它是一个完整的应用程序。有一些与浏览器相关的安全限制,但是,由于您目前使用的是 Applet,我想我可以假设它们不是问题。

于 2009-01-10T15:19:38.267 回答
1

我找到了一个可以正常工作的解决方案。在 Linux 64 位(Chrome 和 Firefox)和 Windows 7 64 位(Chrome 和 Explorer)中测试

唯一的问题是我的小程序使用了浏览器中的所有空间,当用户关闭全屏模式时,小程序没有缩放到浏览器大小。解决方案是在进入全屏模式之前保持小程序的先前大小,然后在小程序返回正常模式时设置此大小:

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();
    }
于 2013-08-19T08:28:01.293 回答