6

我正在尝试调整图标的大小,使其覆盖整个按钮并位于按钮的中心。当我尝试时,它会拉伸我的按钮并弄乱其他一切。我该怎么做?目前,我的代码是:

在我的类的构造函数中..

javax.swing.JButton Console = new javax.swing.JButton;
ScaleButtonImage(Console, ConsoleEnabledImage);

那个班里面..

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)

然后我将它们水平和垂直连接起来,使它们都具有相同的大小。

相反,它最终看起来像下面这样。此外,如果我更改第一个按钮的图标,所有按钮都会因为我的限制而改变大小。如何使图标适合按钮?

在此处输入图像描述

4

2 回答 2

10

尝试这样的事情(如果我没有在括号中犯任何错误):

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

这样,您的图像将被调整大小(在我的情况下为 64x64 大小)并添加到button,就像在这个例子中一样:

在此处输入图像描述

编辑:

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

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)));
于 2012-12-10T23:11:15.257 回答
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);
        }
    }
}
于 2012-12-10T23:32:05.200 回答