如何在java中的组合框中添加图像和文本

时间:2017-07-20 04:42:56

标签: java swing jcombobox

实际上我想在组合框中添加图像和文本。我已经使用了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();
  }
}

1 个答案:

答案 0 :(得分:1)

@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();
  }
}
相关问题