将Java中的图标拉伸以适应按钮

6
我试图调整图标的大小,使其覆盖整个按钮并位于按钮中心。但是当我尝试时,它会拉伸我的按钮并破坏其他所有内容。我该怎么做?目前,我的代码如下:
在我的类构造函数中...
javax.swing.JButton Console = new javax.swing.JButton;
ScaleButtonImage(Console, ConsoleEnabledImage);

Within that class..

private void ScaleButtonImage(javax.swing.JButton Button, java.awt.Image ButtonIcon) {
        double Width  = ButtonIcon.getWidth(Button);
        double Height = ButtonIcon.getHeight(Button);
        double xScale = 28/Width;//Button.getWidth() / Width;
        double yScale = 28/Height;//Button.getHeight() / Height;
        double Scale = Math.min(xScale, yScale);   //ToFit
        //double Scale = Math.max(xScale, yScale); //ToFill
        java.awt.Image scaled = ButtonIcon.getScaledInstance((int)(Scale * Width), (int)(Scale * Height), java.awt.Image.SCALE_SMOOTH);
        Button.setIcon(new javax.swing.ImageIcon(scaled));
    }

布局:

.addGroup(layout.createSequentialGroup()
                        .addComponent(Enable, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Graphics, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Debug, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Console, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

我将所有按钮水平和垂直链接在一起,使它们大小相同。

但实际上,结果看起来像下面这样。另外,如果我更改第一个按钮的图标,由于我的约束,所有按钮的大小都会改变。如何让图标适合按钮?

enter image description here


[JButton]没有实现任何[LayoutManager]。 - mKorbel
已经尝试过了。添加标签图片,然后将其添加到按钮中并不起作用。我还尝试在按钮中实现边框布局。仍然不起作用。 - Brandon
我怀疑你调整图像大小的方式有问题(双倍x比例= 28/宽度; 双倍y比例= 28/高度; ...)。这样做,我认为你的比例将变成1:1,而不是原始比例如4:3或16:9。 - Branislav Lazic
2个回答

10

试试这样做(如果我没有在括号方面犯错):

JButton button = new JButton(new ImageIcon(((new ImageIcon(
            "path-to-image").getImage()
            .getScaledInstance(64, 64,
                    java.awt.Image.SCALE_SMOOTH)))));

按照这种方式,您的图像将被调整大小(在我的情况下为大小64x64),并添加到button中,就像这个示例中所示:

enter image description here

编辑:

这是调整图像大小并保持图像比例的方法:

ImageIcon ii = new ImageIcon("path-to-image");
int scale = 2; // 2 times smaller
int width = ii.getIconWidth();
int newWidth = width / scale;
yourComponent.setIcon(new ImageIcon(ii.getImage().getScaledInstance(newWidth, -1,
            java.awt.Image.SCALE_SMOOTH)));

0
这对我有用:
public class ImageButton
extends JButton {
    private static final long serialVersionUID = 1;

    /** @serial */
    private Image image;

    /** @serial */
    private final Rectangle innerArea = new Rectangle();

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            SwingUtilities.calculateInnerArea(this, innerArea);

            g.drawImage(image,
                innerArea.x, innerArea.y, innerArea.width, innerArea.height,
                this);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接