繁体   English   中英

自动调整JButton图标的大小

[英]Auto-resizing JButton Icon

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

有没有办法让Icon只填充它附加的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是一个个人函数,它接受一个I​​con,一个width参数和一个height参数,并返回一个调整大小的Icon(显然)。 我尝试更改布局,但它没有改变任何东西。 我尝试获取JButton的宽度/高度,但是因为它们在添加Icon时尚不存在,所以它不起作用。

你们有任何想法如何解决这个问题吗? 它不一定是一个图标,只要我的JButton充满我给它的图像,它真棒:)

谢谢!

在Swing中你可以将任何JComponent添加到另一个JComponent ,因为ImageJLabel最好的JComponent ,那么为什么不将JLabel#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();
            }
        });
    }
}
  1. 覆盖paintComponent
  2. 将图像直接绘制到它的Graphics对象

在绘制图像时,提供维度参数getWidth()getHeight() 通过这样做,调整大小将自动化。 此外,在调整大小时,您需要进行一些抗锯齿处理,以使图像不会过于像素化。

您可以向该按钮添加一个Component-Listener,在调整大小时调整Image中的Image

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
        }
    });

希望能帮助到你!

暂无
暂无

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

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