简体   繁体   English

Java Swing:鼠标悬停时按钮仅显示图标

[英]Java Swing: Button only displays Icon upon Mouse-Over

I have Swing Application where I want to display an Icon on a JButton.我有 Swing 应用程序,我想在 JButton 上显示一个图标。 I had some problems with Windows scaling the Icon and making it very ugly. Windows 缩放图标并使其非常难看时遇到了一些问题。 I was able to solve that problem by using an Icon-Wrapper Class as described here .我能够通过使用 Icon-Wrapper Class 来解决这个问题,如此所述。

Using the wrapper class solves the scaling problem I had but causes a new problem: The icon only shows once I mouse over the button, as can be seen here:使用包装器 class 解决了我遇到的缩放问题,但导致了一个新问题:该图标仅在我将鼠标悬停在按钮上时才显示,如下所示:

在此处输入图像描述

Here is a minimal example:这是一个最小的例子:

import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class Ui {
    private static JFrame frame;
    private static JButton button1;
    private static JButton button2;
    private static JMenuBar jMenuBar;


    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                createGui();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        });
    }

    private static void createGui() throws MalformedURLException {
        frame = new JFrame("Test");

        button1 = new JButton();
        button2 = new JButton();
        jMenuBar = new JMenuBar();


        Icon icon = new NoScalingIcon(new ImageIcon(new URL("https://i.stack.imgur.com/wgKeq.png")));
        button1.setIcon(icon);
        button2.setIcon(icon);

        jMenuBar.add(button1);
        jMenuBar.add(button2);
        frame.setJMenuBar(jMenuBar);
        frame.pack();
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
}

The icon wrapper class:图标包装器 class:

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

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

        g2d.setTransform(new AffineTransform());

        icon.paintIcon(c, g2d, scaleX + offsetX, scaleY + offsetY);

        g2d.dispose();
    }
}

What could be the cause for this problem?这个问题的原因可能是什么?

I think I found a solution.我想我找到了解决办法。

Instead of creating a completely new AffineTransform , the code below merges a new scaled transform with the inverse of the existing scale so the scale factor is set back to 1:下面的代码不是创建一个全新的AffineTransform ,而是将一个新的缩放变换与现有比例的倒数合并,因此比例因子设置回 1:

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

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

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