组合框中同时添加图像和文本

组合框中同时添加图像和文本

实际上我想在组合框中同时添加图像和文本。我已经使用JLabel了,但是它不起作用,所以我怎么能做到这一点。

这是我的代码:

package swing;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ComboBox {
  public ComboBox() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ComboBOx");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container con = frame.getContentPane();
    JLabel ar[] = new JLabel[5];
    ar[0] = new JLabel("ganesh",new ImageIcon("/images/g.jpg"),JLabel.HORIZONTAL);
    ar[1] = new JLabel("ganes",new ImageIcon("/images/g.jpg"),JLabel.HORIZONTAL);
    ar[2] = new JLabel("gane",new ImageIcon("/images/g.jpg"),JLabel.HORIZONTAL);
    ar[3] = new JLabel("gan",new ImageIcon("/images/g.jpg"),JLabel.HORIZONTAL);
    ar[4] = new JLabel("ga",new ImageIcon("/images/g.jpg"),JLabel.HORIZONTAL);

    JComboBox<JLabel> box = new JComboBox<JLabel>(ar);
    con.add(box);
    con.setBackground(Color.white);
    con.setLayout(new FlowLayout());
    frame.setVisible(true);
    frame.pack();
  }
  public static void main(String args[]) {
    new ComboBox();
  }
}

最佳答案

@MadProgrammer谢谢,我找到了答案

package swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class ComboBox {
  ImageIcon imageIcon[] = { new ImageIcon("/images/g.jpg"), new ImageIcon("/images/g.jpg"),
      new ImageIcon("/images/g.jpg"), new ImageIcon("/images/g.jpg"), new ImageIcon("/images/g.jpg") };
  Integer array[] = {1,2,3,4,5};
  String names[] = {"img1","img2","img3","img4","img5"};
  public ComboBox() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ComboBOx");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container con = frame.getContentPane();

    ComboBoxRenderar rendrar = new ComboBoxRenderar();

    JComboBox box = new JComboBox(array);

    box.setRenderer(rendrar);
    con.add(box);

    con.setLayout(new FlowLayout());

    frame.setVisible(true);
    frame.pack();
  }
  public class ComboBoxRenderar extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus) {
      int offset = ((Integer)value).intValue() - 1 ;

      ImageIcon icon = imageIcon[offset];
      String name = names[offset];

      setIcon(icon);
      setText(name);

      return this;
    }


  }
  public static void main(String args[]) {
    new ComboBox();
  }
}

关于java - 如何在Java的组合框中同时添加图像和文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45205328/

10-10 14:26