英文:
Icons not appearing in JComboBox and how to resize?
问题
以下为您提供的代码的翻译部分:
// 导入必要的包和类
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JComboBoxWithIcons extends JFrame {
// ... (省略其他内容)
// 主要的自定义图标类
class MyImageIconObject extends ImageIcon {
float x;
ImageIcon ic;
public MyImageIconObject(String iconLocation) {
this.ic = new ImageIcon(iconLocation);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
System.out.println("Painting 2");
}
}
}
当执行上述代码时,会产生以下效果:
示例创建了两个带有相同图标的下拉框。
ComboBox1 - 显示图标,但它们很大。
ComboBox2 - 使用自定义类并重写了 paintIcon() 方法,我计划对图标进行额外的修改,但首先需要让它们出现。
问题1(更多是好奇)- 是否需要渲染器来调整图标的大小?
问题2(主要目标)- 对于 ComboBox 2,为什么图标没有显示?
英文:
The objective is to have images appear within the ComobBox.
When using ImageIcon and the basic calls all work except the icons are very large. Not sure if the combobox uses the icon to set the size and if that can be altered.
When using a custom class that extends ImageIcon, no icons appear which is the main objective to address.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JComboBoxWithIcons extends JFrame {
private static final long serialVersionUID = 1L;
private final ImageIcon ICON1a = new ImageIcon("c:\\Downloads\\Images\\Image1.jpg");
private final ImageIcon ICON2a = new ImageIcon("c:\\Downloads\\Images\\Image2.jpg");
private final ImageIcon ICON3a = new ImageIcon("c:\\Downloads\\Images\\Image3.jpg");
private final MyImageIconObject ICON1b = new MyImageIconObject("c:\\Downloads\\Images\\Image1.jpg");
private final MyImageIconObject ICON2b = new MyImageIconObject("c:\\Downloads\\Images\\Image2.jpg");
private final MyImageIconObject ICON3b = new MyImageIconObject("c:\\Downloads\\Images\\Image3.jpg");
private JComboBox comboBox1;
private JComboBox comboBox2;
private JPanel topPanel;
public JComboBoxWithIcons () {}
public void createGUI(){
setMinimumSize(new Dimension(400,400));
setTitle("Demo");
setLocation(200, 200);
topPanel = new JPanel();
getContentPane().add(topPanel, BorderLayout.CENTER);
ArrayList<ImageIcon> iconsA = new ArrayList<ImageIcon>();
iconsA.add(ICON1a);
iconsA.add(ICON2a);
iconsA.add(ICON3a);
ArrayList<MyImageIconObject> iconsB = new ArrayList<MyImageIconObject>();
iconsB.add(ICON1b);
iconsB.add(ICON2b);
iconsB.add(ICON3b);
comboBox1 = new JComboBox();
comboBox2 = new JComboBox();
for(ImageIcon val : iconsA){
comboBox1.addItem(val);
}
for(MyImageIconObject val : iconsB){
comboBox2.addItem(val);
}
topPanel.add(comboBox1);
topPanel.add(comboBox2);
super.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public static void main(String[] args)
{
JComboBoxWithIcons T = new JComboBoxWithIcons();
T.createGUI();
T.setVisible(true);
}
class MyImageIconObject extends ImageIcon
{
float x;
ImageIcon ic;
public MyImageIconObject(String iconLocation)
{
this.ic = new ImageIcon(iconLocation);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y);
// Graphics2D g2d = (Graphics2D) g;
// ic.paintIcon(c, g2d, x, y);
System.out.println("Painting 2");
}
}
}
When the above code is executed, it produces the following :
The example creates 2 ComboBoxes with the same icons.
ComboBox1 - displays the icons, but they are large.
ComboBox2 - uses the custom class and overrides the paintIcon() as I plan to do additional modifications to the icons but first need them to appear.
Question 1 (more of a curiosity question) - to resize the icons will a renderer be required?
Question 2 (main objective) - for ComboBox 2, why are the icons not appearing?
答案1
得分: 1
你的MyImageIconObject
类继承了ImageIcon
,但你没有调用父类的构造函数,而只是实例化了一个ImageIcon
对象。因此,当你调用paintIcon
方法时,父类将对你的ImageIcon
对象一无所知,并引用一个空值。
以下是一个示例,演示如何修复图标未显示的问题:
class MyImageIconObject extends ImageIcon {
float x;
public MyImageIconObject(String iconLocation) {
super(iconLocation);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
// Graphics2D g2d = (Graphics2D) g;
// ic.paintIcon(c, g2d, x, y);
System.out.println("Painting 2");
}
}
英文:
Your MyImageIconObject class extends ImageIcon but you don't call the constructor of the parent class you just instantiate an ImageIcon object. So when you call the paintIcon method the parent class will have no idea about your ImageIcon object and refer to a null.
Here is an example how you can fix your icon not showing problem
class MyImageIconObject extends ImageIcon
{
float x;
public MyImageIconObject(String iconLocation)
{
super(iconLocation);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y);
// Graphics2D g2d = (Graphics2D) g;
// ic.paintIcon(c, g2d, x, y);
System.out.println("Painting 2");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论