图标未出现在 JComboBox 中以及如何调整大小?
Icons not appearing in JComboBox and how to resize?
objective是让图像出现在ComobBox中。
当使用 ImageIcon 和基本调用时,除了图标非常大外,其他都可以。不确定组合框是否使用图标来设置大小以及是否可以更改。
使用扩展 ImageIcon 的自定义 class 时,没有出现图标,这是主要 objective 要解决的问题。
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");
}
}
}
执行上述代码时,会产生以下内容:
该示例创建了 2 个具有相同图标的组合框。
ComboBox1 - 显示图标,但它们很大。
ComboBox2 - 使用自定义 class 并覆盖 paintIcon(),因为我计划对图标进行其他修改但首先需要它们出现。
问题 1(更多的是好奇问题)- 调整图标大小是否需要渲染器?
问题 2(主要 objective)- 对于 ComboBox 2,为什么没有出现图标?
您的 MyImageIconObject class 扩展了 ImageIcon 但您不调用父对象的构造函数 class 您只是实例化一个 ImageIcon 对象。因此,当您调用 paintIcon 方法时,父级 class 将不知道您的 ImageIcon 对象并引用 null。
这是一个示例,您可以如何修复图标不显示的问题
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");
}
}
objective是让图像出现在ComobBox中。
当使用 ImageIcon 和基本调用时,除了图标非常大外,其他都可以。不确定组合框是否使用图标来设置大小以及是否可以更改。
使用扩展 ImageIcon 的自定义 class 时,没有出现图标,这是主要 objective 要解决的问题。
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");
}
}
}
执行上述代码时,会产生以下内容:
该示例创建了 2 个具有相同图标的组合框。
ComboBox1 - 显示图标,但它们很大。
ComboBox2 - 使用自定义 class 并覆盖 paintIcon(),因为我计划对图标进行其他修改但首先需要它们出现。
问题 1(更多的是好奇问题)- 调整图标大小是否需要渲染器?
问题 2(主要 objective)- 对于 ComboBox 2,为什么没有出现图标?
您的 MyImageIconObject class 扩展了 ImageIcon 但您不调用父对象的构造函数 class 您只是实例化一个 ImageIcon 对象。因此,当您调用 paintIcon 方法时,父级 class 将不知道您的 ImageIcon 对象并引用 null。
这是一个示例,您可以如何修复图标不显示的问题
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");
}
}