JComboBox 更改下拉箭头图像不起作用
JComboBox changing drop down arrow image not working
我正在尝试更改带有自定义图像的 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;
}
}
图片使用 12px x 12px:
图片尺寸为 24px x 24px:
程序运行时输出:
箭头上设置的工具提示有效。如果我使用自定义背景颜色,则相同。但如果我设置图像则不会。我试过:*.jpg、*.gif、*.png 和不同的分辨率:16x16、14x14、12x12、8x8 等,但没有成功。在所有情况下,图像仅加载到 SimpleJButton 上,而不加载到组合框的下拉箭头按钮上。
Eclipse 结构:
你的问题是线路
JButton arrowButton = super.createArrowButton();
你应该把它改成
JButton arrowButton = new JButton();
背景:super.createArrowButton()
return ArrowButton
class 的实例,提供自定义箭头绘制,不支持 setIcon
方法。
我正在尝试更改带有自定义图像的 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;
}
}
图片使用 12px x 12px:
图片尺寸为 24px x 24px:
程序运行时输出:
箭头上设置的工具提示有效。如果我使用自定义背景颜色,则相同。但如果我设置图像则不会。我试过:*.jpg、*.gif、*.png 和不同的分辨率:16x16、14x14、12x12、8x8 等,但没有成功。在所有情况下,图像仅加载到 SimpleJButton 上,而不加载到组合框的下拉箭头按钮上。
Eclipse 结构:
你的问题是线路
JButton arrowButton = super.createArrowButton();
你应该把它改成
JButton arrowButton = new JButton();
背景:super.createArrowButton()
return ArrowButton
class 的实例,提供自定义箭头绘制,不支持 setIcon
方法。