8

所以我有这个我添加图标的JButtons。图标最初太大了,所以我事先调整了它们的大小,它工作正常。除了当我调整窗口大小时,JButton 会改变大小,但图标不会改变,这是有问题的。

有没有办法让一个图标只填充它所连接的 JButton?一些代码使其更清晰:

public JewelClass(){

    setBackground (new Color (30,30,30)); 
    addActionListener(this);
    setLayout(new GridLayout());

    ImageIcon icon = new ImageIcon(src/carre.jpg);
    setIcon (resizeIcon(icon,60,60));

}

resizeIcon 是一个个人函数,它接受一个图标、一个宽度参数和一个高度参数,并返回一个调整大小的图标(显然)。我尝试更改布局,但它没有改变任何东西。我尝试获取 JButton 的宽度/高度,但由于添加图标时它们还不存在,所以它不起作用。

你们知道如何度过这个难关吗?它不一定是一个图标,只要我的 JButton 充满了我给它的图像,它就很棒 :)

谢谢!

4

3 回答 3

3

在 Swing 中你可以添加任何JComponent到另一个JComponent,因为ImageJLabel最好的,那为什么不JComponentJLabel#setIcon()JButton

在此处输入图像描述 在此处输入图像描述

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {
    private static final long serialVersionUID = 1L;

    public ResizeIconInButton() {
        JButton myButton = new JButton();
        myButton.setLayout(new BorderLayout());
        myButton.add(new CustomComponents0());
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(getPreferredSize());
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();

            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
}

class CustomComponents0 extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

编辑:

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    private JButton myButton = new JButton();
    private JLabel myLabel = new JLabel();

    public ResizeIconInButton() {
        Icon myIcon = new ImageIcon(IMAGE_PATH);
        myLabel.setIcon(myIcon);
        myButton.setLayout(new BorderLayout());
        myButton.add(myLabel);
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(new Dimension(200, 100));
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();
            }
        });
    }
}
于 2011-11-22T22:49:00.017 回答
3
  1. 覆盖paintComponent
  2. 将图像直接绘制Graphics到它的对象

并且在绘制图像时,提供尺寸参数getWidth()getHeight(). 通过这样做,调整大小将是自动化的。此外,在调整大小时,您需要进行一些抗锯齿处理,这样图像就不会太像素化。

于 2011-11-22T23:07:25.610 回答
2

您可以向该按钮添加一个组件侦听器,该按钮在调整大小时会调整其中的图像大小

yourButton.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {
            // ignore
        }

        @Override
        public void componentResized(ComponentEvent e) {
            resizeIcon(icon, yourButton.getWidth(), yourButton.getHeight());
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            // ignore
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            // ignore
        }
    });

希望能帮助到你!

于 2011-11-22T23:00:40.360 回答