2017-07-20 44 views
1

其實我想在組合框中添加圖像和文本。我已經使用JLabel,但它不起作用,所以我該如何實現這一點。如何在java中的組合框中添加圖像和文本

這裏是我的代碼:

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

看一看[如何使用組合框,提供自定義渲染(https://開頭docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer),它有一個可證明的例子 – MadProgrammer

+0

不要在組件中使用組件作爲模型中的對象,模型只能承載數據,數據的表示是更新到視圖(在這種情況下'JComboBox'和單元格渲染器) – MadProgrammer

+0

我是votin g因爲[如何使用組合框](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer)提供了一個演示示例,它將解決主要問題 – MadProgrammer

回答

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(); 
    } 
} 
相關問題