找到“真实的” Swing / AWT对象的高度/宽度 [英] Find "real" height/width of Swing/AWT object

查看:126
本文介绍了找到“真实的” Swing / AWT对象的高度/宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为Canvas3D无法使用父框架动态调整大小,所以我希望能够跟踪用户何时调整窗口大小,然后自己手动调整大小。 (如果这最终导致崩溃Canvas3D,正如一些文档建议的那样,当用户调整窗口大小时,我将简单地销毁并重新创建它)。这个过程的一部分涉及能够准确地告诉容器面板开始有多大。

Because Canvas3D doesn't have the ability to resize dynamically with the parent frame, I would like to be able to track when a user resizes a window and then resize it manually myself. (If this ends up crashing Canvas3D, as some docs suggest, I will simply destroy and recreate it when the user resizes their window). Part of this procedure involves being able to accurately tell how big the container panel is to begin with.

我尝试过的两种方法:

panel.getHeight();
panel.getPreferredSize().height;

似乎没有准确报告事物: getHeight()总是为零, getPreferredSize()返回实际上与面板实际大小无关的数字。

Don't seem to accurately report things: getHeight() is invariably zero, and getPreferredSize() returns numbers that don't actually have anything to do with the actual size of the panel.

任何想法?

编辑:所以,我把调试器带到面板对象并手动检查非对象属性,我没有看到任何类似宽度/高度的东西。当然,有一些我没看过的子对象。此外,当我查询高度/对象时,可能窗口必须是可见的(当我正在连接对象时,它不是吗?)

Edit: So, I took a debugger to the panel object and manually inspected the non-object properties and I didn't see anything that resembled width/height. Granted, there are sub-objects that I didn't look at. Also, maybe the window has to be visible (it isn't, at the point I'm interfacing the object) when I query for height/object?

编辑2 :所以,Swing类是AWT类的子类,所以我想如果你能够找到那些的高度/宽度,那么这种方法将会概括。我已相应修改了标题。

Edit 2: So, Swing classes are subclasses of AWT classes, so I imagine if you're able to find the height/width of those, the approach would generalize. I've amended the title accordingly.

推荐答案

要确定组件的大小,您需要:

To determine the size of a component you have to either:


  • 在某些时候手动设置

  • 运行负责布局组件的布局管理器

通常,您可以通过getSize()方法获得组件的确切大小,该方法返回包含宽度和高度的Dimension对象,但getWidth / Height()也应该工作。但是,如果满足两个先决条件之一,这只能起作用。如果窗口从未显示过,没有布局管理器或组件(您想知道其大小)已在窗口/容器可见后添加,则大小通常为零。

Generally, you get the exact size of a component via the getSize() method, which returns a Dimension object containing width and height, but getWidth/Height() should work too. But this can only work, if one of the two preconditions are met. If a window has never been made visible, has no layout manager or the component (you want to know the size of) has been added after the window/container has been made visible, the size usually is zero.

因此要获得正确的大小,您必须使容器/框架可见(在添加组件之后)或在容器上调用validate()或doLayout()以重新计算布局,如果在完成最后一个布局后添加了组件。要记住的另一件事是设置并可能在容器上配置布局管理器。如果没有设置布局管理器(null),即使调用容器可见oder调用validate()也不会在其子项上设置大小。

So to get the correct size, you have to make the container/frame visible (after you have added the component) or call validate() or doLayout() on the container to recalculate the layout, if you added the component after the last layout was done. Another thing to keep in mind is setting and probably configuring a layout manager on the container. If no layout manager ist set (null), even making a container visible oder calling validate() does not set a size on its children.

minimumSize / preferredSize / maximumSize属性是布局管理器的提示,组件应如何调整大小,但它不必遵守它们(大多数布局管理器不遵守)。

The minimumSize/preferredSize/maximumSize properties are hints to the layout manager, how the component should be sized, but it does not have to obey them (most layout managers don't).

编辑2:在我阅读您的其他问题之后关于同一主题,我认为你应该阅读使用Java教程中的布局管理器

Edit 2: After I read your other question about the same subject, I think you should read Using Layout Managers from The Java Tutorials

编辑:我不知道你是否已经想出来,但要做出反应为了调整窗口的大小,您可以执行以下操作:

I don't know if you already figured that out, but to react to the resizing of the window, you can do something like this:

public class WindowResizeTest extends JFrame {

    public static void main(String[] args) {
        new WindowResizeTest();
    }

    public WindowResizeTest() {
        this.setSize(640, 480);

        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        this.add(panel);

        this.addComponentListener(new ComponentListener() {

            public void componentResized(ComponentEvent e) {
                System.out.println(e.getComponent().getSize());
            }

            public void componentHidden(ComponentEvent e) {}

            public void componentMoved(ComponentEvent e) {}

            public void componentShown(ComponentEvent e) {}
        });

        this.setVisible(true);
    }

}

这篇关于找到“真实的” Swing / AWT对象的高度/宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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