【问题标题】:JComboBox changing drop down arrow image not working [duplicate]JComboBox更改下拉箭头图像不起作用[重复]
【发布时间】:2020-02-25 19:50:57
【问题描述】:

我正在尝试使用自定义图像更改 JComboBox 的下拉箭头,但代码似乎不起作用。我遵循了here 的指示。我尝试使用 Java 1.8.131 和 AdoptOpenJDK 11.0.5,但它们的结果是相同的。您可以在下面找到我正在使用的完整代码:

Main.java

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { 
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    JFrame f = new JFrame("Window");
    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 400, 400);
    panel.setBackground(Color.gray);

    JComboBox<String> box = new JComboBox<>();//simple JComboBox with custom UI
    box.setPreferredSize(new Dimension(150, 30));
    box.setUI(MyBasicComboboxUI.createUI(box));

    box.addItem("BasicComboBoxUI1");
    box.addItem("BasicComboBoxUI2");
    box.addItem("BasicComboBoxUI3");

    panel.add(box);

    JButton btn = new JButton("SimpleJButton");//simple JButton with an image on it (to prove that the image loads)
    btn.setPreferredSize(new Dimension(120, 30));
    MyImageProvider imageProvider = new MyImageProvider();
    btn.setIcon(new ImageIcon(imageProvider.getImage()));
    btn.setBorder(new EmptyBorder(0, 0, 0, 0));

    panel.add(btn);
    f.add(panel);
    f.setSize(400, 400);
    f.setLayout(null);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

MyBasicComboboxUI.java:

public class MyBasicComboboxUI extends BasicComboBoxUI {
    public static ComboBoxUI createUI(JComponent c) {
        return new MyBasicComboboxUI();
    }

    @Override
    protected JButton createArrowButton() {
        JButton arrowButton = super.createArrowButton();
        MyImageProvider imgProvider = new MyImageProvider();
        arrowButton.setSize(new Dimension(40, 40));
        arrowButton.setToolTipText("My tooltip");
        arrowButton.setIcon(new ImageIcon(imgProvider.getImage()));//set the same icon here
        arrowButton.setBorder(new EmptyBorder(0, 0, 0, 0));
        return arrowButton;
    }
}

MyImageProvider.java

public class MyImageProvider {

    public Image getImage() {
        try {
            Image img = ImageIO.read(getClass().getResource("/icons/arrow.gif"));
            return img;
        } catch (IOException e) {
            System.out.println("The image was not loaded.");
        }
        return null;
    }
}

使用的图片尺寸为 12 像素 x 12 像素:

图片尺寸为 24 像素 x 24 像素:

运行程序时的输出:

箭头上设置的工具提示正在工作。如果我使用自定义背景颜色也是一样。但如果我设置图像则不会。我尝试了:*.jpg、*.gif、*.png 和不同的分辨率:16x16、14x14、12x12、8x8 等,但没有成功。在所有情况下,图像仅在 SimpleJButton 上加载,而不是在组合框的下拉箭头按钮上。

Eclipse 结构:

【问题讨论】:

    标签: java swing jcombobox


    【解决方案1】:

    你的问题是线路

    JButton arrowButton = super.createArrowButton();
    

    你应该改成

    JButton arrowButton = new JButton();
    

    背景:super.createArrowButton() 返回ArrowButton 类的实例,提供自定义箭头绘制,不支持setIcon 方法。

    【讨论】:

    • 非常感谢,经过数小时的搜索,这个答案救了我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多