简体   繁体   English

如何在GroupLayout中调整图像大小?

[英]How to resize image in GroupLayout?

I have an image which is displayed with a label in grouplayout.I want the image to be resized as the size of the window gets changed but label's getSize() always returns zero width and height which causes and exception to be thrown. 我有一个图像,在grouplayout中显示一个标签。我希望在窗口大小改变时调整图像大小,但标签的getSize()总是返回零宽度和高度,这会导致抛出异常。 here is relevant part of my code : 这是我的代码的相关部分:

GroupLayout gl_panel_3 = new GroupLayout(panel_3);
    gl_panel_3.setHorizontalGroup(gl_panel_3.createParallelGroup(
            Alignment.LEADING).addGroup(
            gl_panel_3
                    .createSequentialGroup()
                    .addContainerGap()
                    .addComponent(logoPanel,GroupLayout.PREFERRED_SIZE,300,Short.MAX_VALUE)
                    .addComponent(topButtonPanel,
                            GroupLayout.PREFERRED_SIZE, 519,
                            GroupLayout.PREFERRED_SIZE).addContainerGap()));
    gl_panel_3.setVerticalGroup(gl_panel_3.createParallelGroup(
            Alignment.LEADING)
            .addComponent(logoPanel,GroupLayout.PREFERRED_SIZE,100,Short.MAX_VALUE)
            .addComponent(topButtonPanel,GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE));

JLabel label22 = new JLabel();


GroupLayout gl_logoPanel = new GroupLayout(logoPanel);
    gl_logoPanel.setHorizontalGroup(
        gl_logoPanel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_logoPanel.createSequentialGroup()
                .addComponent(logoLabel, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(313, Short.MAX_VALUE))
    );
    gl_logoPanel.setVerticalGroup(
        gl_logoPanel.createParallelGroup(Alignment.LEADING)
            .addComponent(logoLabel, GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
    );

ImageIcon icon = new ImageIcon(getClass().getResource("/icons/noa_en/logo.png"));
    ImageIcon scaledinstance = new ImageIcon(icon.getImage().getScaledInstance(label22.getWidth(), label22.getWidth(), Image.SCALE_SMOOTH));
    label22.setIcon(scaledinstance);

I'm wondering how to do it in grouplayout,or if I have to change my layout manager? 我想知道如何在grouplayout中做到这一点,或者我是否需要更改我的布局管理器?

but label's getSize() always returns zero width and height which causes and exception to be thrown. 但是标签的getSize()总是返回零宽度和高度,这会导致抛出异常。

Components only have a size once the GUI is visible. 一旦GUI可见,组件只有一个大小。

I'm wondering how to do it in grouplayout,or if I have to change my layout manager? 我想知道如何在grouplayout中做到这一点,或者我是否需要更改我的布局管理器?

GroupLayout is generally only used with an IDE because it is complicated to use. GroupLayout通常仅用于IDE,因为它使用起来很复杂。 Most developers will code the GUI themselves and use various layout managers to achieve the desired layout. 大多数开发人员将自己编写GUI并使用各种布局管理器来实现所需的布局。

In any case an Icon on a JLabel is always displayed at its actual size, so it doesn't matter what layout you use, the image size will not change. 在任何情况下,JLabel上的Icon总是以其实际大小显示,因此无论您使用什么布局都无关紧要,图像大小不会改变。

However you can use Darryl's Stretch Icon which is designed to stretch/shrink depending on the space available for the label. 但是,您可以使用Darryl的Stretch Icon ,它可以根据标签的可用空间进行拉伸/缩小。 I have no idea if it will work with a GroupLayout, but it does work with other layout managers that support dynamic sizing. 我不知道它是否适用于GroupLayout,但它确实可以与支持动态大小调整的其他布局管理器一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM