简体   繁体   English

Java,GridBagLayout

[英]Java, GridBagLayout

Hi i am trying to resize the image icon which i tried to use the getScaled method but it's not working, i have attached a copy of the code below. 嗨,我正在尝试调整图像图标的大小,我试图使用getScaled方法,但是它不起作用,我附上了下面代码的副本。

public Example (){
        JLabel l = new JLabel(new ImageIcon(getClass().getResource("gui/CheckLib.png")));
        Image resize = l.getScaled(200, 180, 18);
        l.setIcon(new ImageIcon(resize));

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.SOUTHEAST;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(l);

Some research would have shown you that JLabel does not have a method called getScaled 一些研究表明, JLabel没有名为getScaled的方法。

Some more research would have lead you to Image#getScaledInstance 更多研究将使您进入Image#getScaledInstance

Some more research might have lead you to The Perils of Image.getScaledInstance() 一些更多的研究可能会导致你Image.getScaledInstance的危险()

Which might have lead you to Java: maintaining aspect ratio of JPanel background image 可能导致您使用Java:维护JPanel背景图像的宽高比

Now remember, ImageIcon wraps a java.awt.Image in an Icon interface 现在记住, ImageIconIcon接口中包装了一个java.awt.Image

You can try Darryl's Stretch Icon . 您可以尝试Darryl的Stretch Icon It will resize the image based on the space available to the icon. 它将根据图标的可用空间调整图像大小。

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

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