图标在JComboBox中没有显示出来,如何调整大小?

huangapple go评论73阅读模式
英文:

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");
        }
    }
}

当执行上述代码时,会产生以下效果:

图标在JComboBox中没有显示出来,如何调整大小?

示例创建了两个带有相同图标的下拉框。

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 :

图标在JComboBox中没有显示出来,如何调整大小?

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");
        }
    }

huangapple
  • 本文由 发表于 2020年9月16日 19:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63919474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定