JFrame:获取无边框大小?

发布于 2024-10-18 14:37:25 字数 114 浏览 2 评论 0原文

在Java中,是否可以获取没有标题和其他边框的JFrame的宽度和高度?

frame.getWidth() 和frame.getHeight()1 似乎返回包括边框的宽度。

谢谢。

In Java, is it possible to get the Width and Height of the JFrame without the title and other borders?

frame.getWidth() and frame.getHeight()1 seems to return the width including the border.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云雾 2024-10-25 14:37:25

frame.getContentPane().getSize();


frame.getContentPane().getSize();

撩起发的微风 2024-10-25 14:37:25
frame.pack();
System.out.println("frame width : "+getWidth());
System.out.println("frame height: "+getHeight());
System.out.println("content pane width : "+getContentPane().getWidth());
System.out.println("content pane height: "+getContentPane().getHeight());
System.out.println("width  of left + right  borders: "+(getWidth()-getContentPane ().getWidth()));
System.out.println("height of top  + bottom borders: "+(getHeight()-getContentPane().getHeight()));
frame.pack();
System.out.println("frame width : "+getWidth());
System.out.println("frame height: "+getHeight());
System.out.println("content pane width : "+getContentPane().getWidth());
System.out.println("content pane height: "+getContentPane().getHeight());
System.out.println("width  of left + right  borders: "+(getWidth()-getContentPane ().getWidth()));
System.out.println("height of top  + bottom borders: "+(getHeight()-getContentPane().getHeight()));
世界和平 2024-10-25 14:37:25

这工作正常

frame.getContentPane().getSize();

,但如果您还没有添加内容,则不行。就我而言,我想在添加内容之前计算 JFrame 的内部尺寸,以便我可以相应地划分内容。这就是我的想法。

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

pack(); // Need this, otherwise insets() show as 0.  
int scrW = (int)screenSize.getWidth();
int scrH = (int)screenSize.getHeight();
  int innerW = scrW - getInsets().left - getInsets().right;
  int innerH = scrH - getInsets().top - getInsets().bottom;

  // Need to setSize(), otherwise pack() will collapse the empty JFrame
  setSize(scrW, scrH);

This works fine

frame.getContentPane().getSize();

But not if you haven't added content yet. In my case, I wanted to calculate the inner dimensions of the JFrame before adding content, so I could divide up the content accordingly. Here's what I came up with.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

pack(); // Need this, otherwise insets() show as 0.  
int scrW = (int)screenSize.getWidth();
int scrH = (int)screenSize.getHeight();
  int innerW = scrW - getInsets().left - getInsets().right;
  int innerH = scrH - getInsets().top - getInsets().bottom;

  // Need to setSize(), otherwise pack() will collapse the empty JFrame
  setSize(scrW, scrH);
私野 2024-10-25 14:37:25

下面是一个适用于 JFrame 以及 AWT 的 Frame(它恰好是 JFrame 的超类型)的代码片段:

public static Dimension getInnerSize(Frame frame) {
    Dimension size = frame.getSize();
    Insets insets = frame.getInsets();
    if (insets != null) {
        size.height -= insets.top + insets.bottom;
        size.width -= insets.left + insets.right;
    }
    return size;
}

注意:插入仅在以下情况下有效:框架已显示。

这是解决此问题的另一个代码片段:

private static Insets defaultInsets;
public static Insets getInsetsWithDefault(Frame frame) {
    // insets only correct after pack() and setVisible(true) has been
    // called, so we use some fallback strategies
    Insets insets = frame.getInsets();
    if (insets.top == 0) {
        insets = defaultInsets;
        if (insets == null) {
            insets = new Insets(26, 3, 3, 3);
            // usual values for windows as our last resort
            // but only as long as we never saw any real insets
        }
    } else if (defaultInsets == null) {
        defaultInsets = (Insets) insets.clone();
    }
    return insets;
}

此代码需要使用可见框架调用一次。之后,即使对于不可见的帧,它也可以正确预测插入(由于 defaultInsets 的缓存),假设它们始终相同。

当然,这仅在所有窗口都具有相同的窗口装饰时才有效。但我不知道在任何情况下它们可能因窗口而异。

这也可能很有用:

frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowOpened(WindowEvent e) {
        MyUtilClass.getInsetsWithDefault(frame); // init the defaultInsets
    }

});

一旦窗口可见,它将调用 getInsetsWithDefault() 方法并初始化正确的 defaultInsets

Here is a code snippet which works on JFrame as well as AWT's Frame (which happens to be the super-type of JFrame):

public static Dimension getInnerSize(Frame frame) {
    Dimension size = frame.getSize();
    Insets insets = frame.getInsets();
    if (insets != null) {
        size.height -= insets.top + insets.bottom;
        size.width -= insets.left + insets.right;
    }
    return size;
}

Beware: The insets are only valid once the frame has been shown.

Here is another code snippet to work around this problem:

private static Insets defaultInsets;
public static Insets getInsetsWithDefault(Frame frame) {
    // insets only correct after pack() and setVisible(true) has been
    // called, so we use some fallback strategies
    Insets insets = frame.getInsets();
    if (insets.top == 0) {
        insets = defaultInsets;
        if (insets == null) {
            insets = new Insets(26, 3, 3, 3);
            // usual values for windows as our last resort
            // but only as long as we never saw any real insets
        }
    } else if (defaultInsets == null) {
        defaultInsets = (Insets) insets.clone();
    }
    return insets;
}

This code needs to be called once with a visible Frame. After that it can correctly predict the insets even for invisible frames (due to caching of the defaultInsets), assuming they are always the same.

Of course this only works if all windows get the same window-decorations. But i am not aware of any case where they might differ from window to window.

This might be useful, too:

frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowOpened(WindowEvent e) {
        MyUtilClass.getInsetsWithDefault(frame); // init the defaultInsets
    }

});

It will call the getInsetsWithDefault() method once the window is visible and initialize the correct defaultInsets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文