java - 小程序独立问题

标签 java applet

我试图通过添加以下内容来制作一个独立的小程序程序:

            public static void main(String[] args) {
    JFrame frame = new JFrame("StartingPoint");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    StartingPoint sp = new StartingPoint();
    frame.getContentPane().add(sp);
    sp.init();
    frame.pack();
    frame.setVisible(true);
    sp.start();
    }

就在我的公开课之后。当仅作为小程序运行时,它不会执行任何操作,但是当将其作为应用程序运行时,它会作为标题之外的一个非常小的、几乎平坦的框运行,并且当手动调整大小时,屏幕除了背景颜色之外是空白的。 知道是什么原因造成的吗?

我还注意到,每次我调整框架大小时,它上面的内容都会卡住,就像应该发生的事情的屏幕截图一样,当屏幕大小调整到几乎全屏时,我可以在顶部看到筛选出一小部分应该移动的东西。

最佳答案

我只想说,将小程序放入框架中是一个非常糟糕的主意。您最好将应用程序内容写入单独的容器(例如 JPanel)并将其添加到您的小程序或框架中 - 恕我直言。

来自 Java 文档...

Window#pack

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

这表明如果您希望使用pack,您的小程序需要提供preferredSize

关于java - 小程序独立问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369950/

相关文章:

java - Stackdriver Debug 不捕获快照

Java 实现异常处理

java - Android NumberPicker 设置要包裹项目的项目数

java - 访问控制异常: access denied( "java.io.FilePermission" "[image]" "read")

java - 有没有简单的小程序 url 生成器?

java - 如果我使用 applet,如何使用 Eclipse 创建 .jar 文件?

java - Jetty SSL 模块中的 needClientAuth 与 wantClientAuth 有什么区别?

java - 带数字的antlr4语法字符串

java - 如何创建 B+ 树数据结构

javascript - 如何检测IE是否被JAVA屏蔽(安全)