Java swing JButton/JLabel:图标未以其原始大小显示 [英] Java swing JButton/JLabel: icons are not displayed in their original size

查看:77
本文介绍了Java swing JButton/JLabel:图标未以其原始大小显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有png图标,并将它们用作JButton/JLabel中的图标.
问题在于,在运行时显示的图像大于原始图标,并且由于这种调整大小,因此非常难看.

I have png icons and use them as icons in JButton / JLabel.
The problem is that the image displayed at runtime is larger than the original icon, and because of this resizing, it's super ugly.

这里是一个例子:
原始图标(左)及其在JButton(右)

Here is an example:
Original icon (left) and how it's rendered in the JButton (right)

这个最小示例的源代码很简单:

The source code for this minimal example is simply:

public class Main {

    public static void main(String... args) {

        JFrame frame = new JFrame("Test");
        frame.setBounds(0, 0, 120, 80);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());

        ImageIcon icon = new ImageIcon("icon.png");
        frame.getContentPane().add(new JButton("Test", icon));
        frame.setVisible(true);
    }
}

这是预期的吗?如果没有,我该如何避免呢?我尝试过很多方法来强制图像的大小,按钮等,但是无法显示正确的图像.

Is this expected? If not, how can I avoid this? I tried many things around forcing the size of the image, the button, etc. but could not get a proper image displayed.

我用各种尺寸的图标进行了测试:16x16、17x17、18x18、19x19、20x20,并且每次JButton上显示的图标都比原始图标大一点,使它看起来很丑:

I have tested with icons of various sizes: 16x16, 17x17, 18x18, 19x19, 20x20, and each time the icon displayed on the JButton is a bit larger than the original which makes it look ugly:

谢谢!

干杯.

推荐答案

这是因为您正在使用Windows缩放.整个组件都按比例缩放,包括图标和文本.

This is because you are using Windows scaling. The entire component is scaled, both the icon and the text.

您可以通过使用包装器图标来关闭图标的缩放比例:

You could turn the scaling of the Icon off by using a wrapper Icon:

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


public class NoScalingIcon implements Icon
{
    private Icon icon;

    public NoScalingIcon(Icon icon)
    {
        this.icon = icon;
    }

    public int getIconWidth()
    {
        return icon.getIconWidth();
    }

    public int getIconHeight()
    {
        return icon.getIconHeight();
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        Graphics2D g2d = (Graphics2D)g.create();

        AffineTransform at = g2d.getTransform();

        int scaleX = (int)(x * at.getScaleX());
        int scaleY = (int)(y * at.getScaleY());

        int offsetX = (int)(icon.getIconWidth() * (at.getScaleX() - 1) / 2);
        int offsetY = (int)(icon.getIconHeight() * (at.getScaleY() - 1) / 2);

        int locationX = scaleX + offsetX;
        int locationY = scaleY + offsetY;

        AffineTransform scaled = AffineTransform.getScaleInstance(1.0 / at.getScaleX(), 1.0 / at.getScaleY());
        at.concatenate( scaled );
        g2d.setTransform( at );

        icon.paintIcon(c, g2d, locationX, locationY);

        g2d.dispose();
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        JButton button = new JButton( "Button" );
        NoScalingIcon icon = new NoScalingIcon( new ImageIcon("box.jpg") );
        button.setIcon( icon );

        JPanel panel = new JPanel( );
        panel.add( button );

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.setSize(200, 200);
        f.setLocationRelativeTo( null );
        f.setVisible(true);
    }
}

  1. 缩放比例调整会将图标放置在按钮区域的顶部/左侧.

  1. The scaling adjustment will position the Icon at the top/left of the button area.

然后,偏移量调整将尝试使图标在缩放的图标绘画区域中居中.

The offset adjustment will then attempt to center the Icon in the scaled icon painting area.

使用默认变换时,图标的缩放比例为0.

Using the default transform will have a scaling factor of 0 for the Icon.

这篇关于Java swing JButton/JLabel:图标未以其原始大小显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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