【问题标题】:setting size of JFrame using Dimension.setSize使用 Dimension.setSize 设置 JFrame 的大小
【发布时间】:2011-05-27 17:46:25
【问题描述】:

我有一个 gui 应用程序,我需要将主 JFrame 的大小设置为屏幕的 80% 宽度和屏幕高度的 90%。我试过这个

public class JFrameDemo extends JFrame{
    public JFrameDemo(String title) throws HeadlessException {
            super(title);
            setLayout(new GridBagLayout());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            int height = screenSize.height;
            int width = screenSize.width;
            screenSize.setSize(width*(0.80), height*(0.90));
            int newheight = screenSize.height;
            int newwidth = screenSize.width;

             //Then I put some print statements in the code
            System.out.println("height="+height);
            System.out.println("width="+width);
            System.out.println("0.80*height="+(height*0.80));
            System.out.println("0.90*width="+(width*0.90));
            System.out.println("newheight="+newheight);
            System.out.println("newwidth="+newwidth);

            this.setSize(newwidth, newheight);
            this.addWidgets();
            this.setVisible(true);
        }

        ...
}

这是正确的方法吗?我对 gui 应用程序设计不是很熟悉..我想 我可以这样使用 Dimension.setSize(double width, double height)。

打印语句导致了这个输出..

height=768
width=1366
0.80*height=614.4
0.90*width=1229.4
newheight=692
newwidth=1093

嗯,newheight 和 newwidth 并不是宽度、高度值的 80% 或 90%。

欢迎提出任何改进建议

谢谢

标记

【问题讨论】:

标签: size jframe


【解决方案1】:

我花了一些时间,但它只是打错了。

实际上,您计算的是 90% 的宽度和 80% 的高度,而不是 80% 的宽度和 90% 的高度。

交换两个百分比,一切都会好起来的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2013-01-17
    • 2021-01-16
    相关资源
    最近更新 更多