Swing:设置JFrame内容区域的大小 [英] Swing: set JFrame content area size

查看:95
本文介绍了Swing:设置JFrame内容区域的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个具有500x500的可用内容区域的JFrame.如果我这样做...

I'm trying to make a JFrame with a usable content area of exactly 500x500. If I do this...

public MyFrame() {
    super("Hello, world!");
    setSize(500,500);
}

...我得到一个全尺寸为500x500的窗口,包括标题栏等.在这里,我确实需要一个尺寸约为504x520的窗口,以说明该窗口的边框和标题栏.我该如何实现?

... I get a window whose full size is 500x500, including the title bar, etc., where I really need a window whose size is something like 504x520 to account for the window border and titlebar. How can I achieve this?

推荐答案

您可以尝试以下几种方法: 1-骇客:

you may try couple of things: 1 - a hack:

public MyFrame(){
 JFrame temp = new JFrame;
 temp.pack();
 Insets insets = temp.getInsets();
 temp = null;
 this.setSize(new Dimension(insets.left + insets.right + 500,
             insets.top + insets.bottom + 500));
 this.setVisible(true);
 this.setResizable(false);
}

2-或 将JPanel添加到框架的内容窗格,然后 只需设置首选/最小尺寸 JPanel的大小为500X500,请调用pack()

2- or Add a JPanel to the frame's content pane and Just set the preferred/minimum size of the JPanel to 500X500, call pack()

  • 2-更便携

这篇关于Swing:设置JFrame内容区域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆