Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何在组合框中同时添加图像和文本_Java_Swing_Jcombobox - Fatal编程技术网

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

在java中,如何在组合框中同时添加图像和文本,java,swing,jcombobox,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

实际上,我想在组合框中同时添加图像和文本。我已经使用了
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();
  }
}
package-swing;
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.FlowLayout;
导入java.util.Vector;
导入javax.swing.ImageIcon;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
公共类组合框{
公共组合框(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame=新的JFrame(“组合框”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
容器con=frame.getContentPane();
JLabel ar[]=新JLabel[5];
ar[0]=newjlabel(“ganesh”,newimageicon(“/images/g.jpg”),JLabel.HORIZONTAL);
ar[1]=新JLabel(“ganes”,新图像图标(“/images/g.jpg”),JLabel.HORIZONTAL);
ar[2]=new JLabel(“gane”,new ImageIcon(“/images/g.jpg”),JLabel.HORIZONTAL);
ar[3]=新的JLabel(“gan”,新图像图标(“/images/g.jpg”),JLabel.HORIZONTAL);
ar[4]=新的JLabel(“ga”,新的图像图标(“/images/g.jpg”),JLabel.HORIZONTAL);
JComboBox=新JComboBox(ar);
con.添加(框);
con.立根背景(颜色:白色);
con.setLayout(新的FlowLayout());
frame.setVisible(true);
frame.pack();
}
公共静态void main(字符串参数[]){
新建组合框();
}
}

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

看看哪个有一个可演示的示例不要将组件用作模型中的对象,假设模型仅承载数据,数据的表示将更新到视图中(在本例中为
JComboBox
和单元渲染器)我投票结束这个问题,因为它提供了一个可以解决主要问题的演示示例。我只是一个初学者。官方教程应该永远是你的首选;)