使用swing调整图标大小并将其显示在按钮上

k5hmc34c  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(367)

我有一个带有jmenubar的jframe,在这里我想显示一些带有lupe图标的jtogglebutton,而不是文本(用于缩放功能)。当我尝试缩小我的lupe图像时,得到的图标看起来很可怕。但是,如果我举个例子,将鼠标光标设置为我用来创建按钮图标的同一个图像,它看起来会更好。
下面是一个简单的例子,你知道我的意思:ui\u frame.java

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Ui_Frame {
    private static JFrame f;
    private static JToggleButton toggle_zoomIn;
    private static JToggleButton toggle_zoomOut;
    private static JMenu test_item;

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

    private static void createAndShowGUI() throws IOException {
        f = new JFrame();
        JMenuBar menubar = new JMenuBar();

        toggle_zoomIn = new JToggleButton();
        ImageIcon plus_icon = new ImageIcon(new URL("https://i.stack.imgur.com/aB5Pp.png"));
        setup_toggleButton(toggle_zoomIn, "Plus Zoom", plus_icon, 25);
        toggle_zoomOut = new JToggleButton();
        ImageIcon minus_icon = new ImageIcon(new URL("https://i.stack.imgur.com/XqjEP.png"));
        setup_toggleButton(toggle_zoomOut, "Minus Zoom",minus_icon, 25);
        test_item = new JMenu("Test");

        menubar.add(test_item);
        menubar.add(Box.createHorizontalStrut(30));
        menubar.add(toggle_zoomIn);
        menubar.add(Box.createHorizontalStrut(3));
        menubar.add(toggle_zoomOut);

        f.setJMenuBar(menubar);
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.pack();
        f.setVisible(true);

        //set mouse to the same icon as the button, looks much better however
        menubar.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
                new ImageIcon(new URL("https://i.stack.imgur.com/aB5Pp.png")).getImage(),
                new Point(0,0),"custom cursor"));

    }

    private static void setup_toggleButton(JToggleButton button, String altText,ImageIcon imageIcon, int size){
        Image image = imageIcon.getImage(); // transform it
        Image newimg = image.getScaledInstance(size , size ,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
        imageIcon = new ImageIcon(newimg);  // transform it back
        button.setIcon(imageIcon);
        button.setBackground(Color.LIGHT_GRAY);
        button.setToolTipText(altText);
        button.setOpaque(false);
        button.setBorderPainted(false);
        button.setPreferredSize(new java.awt.Dimension(size, size));
        button.setSelected(false);
    }
}

请注意,按钮和鼠标图标是如何相同的图像,并缩小到非常相似的大小,但鼠标图标看起来更好。
当我使用gimp手动缩小图像(因此不在代码中)时,也会发生类似的情况:当我在windows资源管理器的预览中查看图像时,它看起来相当不错,但是,一旦我在按钮上显示它,它就会变得很难看。
这可能是什么原因?
以下是我用于lupe图标的图像:


编辑:
以下是我的结果:

u4vypkhs

u4vypkhs1#

在windows10上,自定义光标看起来更糟。根据我的经验,自定义windows光标是32x32,不支持半透明像素(没有alpha通道)。

相关问题