Java JComboBox更改下拉箭头图像不工作
我试图更改带有自定义图像的JComboBox的下拉箭头,但代码似乎不起作用。我听从了老师的指示。我尝试了Java1.8.131和采用OpenJDK11.0.5,但结果都是一样的。下面您可以找到我使用的完整代码: Main.javaJava JComboBox更改下拉箭头图像不工作,java,swing,jcombobox,Java,Swing,Jcombobox,我试图更改带有自定义图像的JComboBox的下拉箭头,但代码似乎不起作用。我听从了老师的指示。我尝试了Java1.8.131和采用OpenJDK11.0.5,但结果都是一样的。下面您可以找到我使用的完整代码: Main.java public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookA
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);
}
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;
}
}
MyImageProvider.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);
}
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;
}
}
使用12px 12px的图像:
24像素x 24像素的图像:
运行程序时的输出:
箭头上设置的工具提示正在工作。如果我使用自定义背景色也是一样的。但如果我设定了一个形象就不会了。我尝试了:*.jpg、*.gif、*.png和不同的分辨率:16x16、14x14、12x12、8x8等,但没有成功。在所有情况下,图像仅加载在SimpleJButton上,而不加载在combobox的下拉箭头按钮上
Eclipse结构:
你的问题是线路
JButton arrowButton = super.createArrowButton();
你应该把它改成
JButton arrowButton = new JButton();
背景:
super.createArrowButton()
返回ArrowButton
类的一个实例,该类提供自定义箭头绘制,不支持setIcon
方法。非常感谢,这个答案节省了我数小时的搜索时间。