代码之家  ›  专栏  ›  技术社区  ›  Ganesh Patel

在java中,如何在组合框中同时添加图像和文本

  •  2
  • Ganesh Patel  · 技术社区  · 7 年前

    实际上,我想在组合框中添加图像和文本。我有使用 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 回复  |  直到 7 年前
        1
  •  1
  •   Ganesh Patel    7 年前

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