调整图标大小以适应Java中的JButton? [英] Resizing icon to fit on JButton in Java?

查看:193
本文介绍了调整图标大小以适应Java中的JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我为JButton设置一个图标时,它的大小总是不正确。如何调整图标大小以完全适合按钮?

Whenever I set an icon for my JButton it is always not sized correctly. How can I resize the icon to fit the button fully?

final JButton btnSanic = new JButton();
Image img = icon.getImage();
Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);  
btnSanic.setIcon(icon);


推荐答案

有很多问题。首先,所有Swing组件都不会自动缩放图像。当然,这可能是一个好主意,但考虑到有效地完成所需的时间和处理,我明白为什么他们不这样做,所以你需要做所有的工作......

There are any number of issues. To start with, all Swing components DON'T auto scale images. Sure, might be a nice idea, but given the amount of time and processing required to do it efficiently, I understand why they don't, so you need to do all the work...

你还应该记住,组件的大小在布局之前是不确定的,虽然你可以提供你可能想要的所有大小提示,但布局管理器完全有权忽略一个或者更多这些提示。

You should also remember, that the size of a component is not determined until it is laid out, and while you can provide all the sizing hints you might like, the layout manager is well within its rights to ignore one or more of these hints.

您应该使用 ComponentListener 用于接收实际调整组件大小的通知的API ...

Instead of "hoping" you know the size of the button, you should make use of the ComponentListener API to receive notifications of when the component is actually resized...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestButton {

    public static void main(String[] args) {
        new TestButton();
    }

    private BufferedImage master;

    public TestButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                try {
                    master = ImageIO.read(new File("C:\\svg\\Revert 256x256.png"));

                    JButton btn = new JButton() {

                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(90, 50);
                        }

                    };
                    btn.addComponentListener(new ComponentAdapter() {

                        @Override
                        public void componentResized(ComponentEvent e) {
                            JButton btn = (JButton) e.getComponent();
                            Dimension size = btn.getSize();
                            Insets insets = btn.getInsets();
                            size.width -= insets.left + insets.right;
                            size.height -= insets.top + insets.bottom;
                            if (size.width > size.height) {
                                size.width = -1;
                            } else {
                                size.height = -1;
                            }
                            Image scaled = master.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH);
                            btn.setIcon(new ImageIcon(scaled));
                        }

                    });

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(btn);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}

注意:这个例子远非优化,但只是提供了一个可能的解决方案的广泛概念...

Note: This example is far from optimised, but simply provides a broad concept of a possible solution...

现在,一句警告。 Image#getScaledInstance 既不是最快或最好的缩放算法......

Now, a word of warning. Image#getScaledInstance is neither the fastest or greatest of scaling algorithms...

看看......

  • The Perils of Image.getScaledInstance()
  • Quality of Image after resize very low -- Java

了解更多详情......

for more details...

这篇关于调整图标大小以适应Java中的JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆