2013-09-21 46 views
2

這是我寫的,到目前爲止的例子:如何將jpg放入JComboBox?

import javax.swing.*; 
import java.awt.*; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class program { 

    JFrame win = new JFrame("bla bla"); 

    final private String[] animals = { "dog", "cat", "mouse" }; 

    private void Start() { 
     JPanel superior = new JPanel(); 
     superior.setLayout(new GridLayout(3, 3)); 
     win.getContentPane().add(superior, BorderLayout.PAGE_START); 
     final JComboBox<String> comboBox = new JComboBox<String>(animals); 
     ((JLabel) comboBox.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER); 
     superior.add(comboBox); 
     win.setSize(440, 290); 
     win.setResizable(false); 
     win.setLocationRelativeTo(null); 
     win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     win.setVisible(true); 
    } 

    public static void main(String args[]) { 
     program window = new program(); 
     window.Start(); 
    } 
} 

我有一個名爲JPG文件夾中 String數組放置在同一水平動物的每個項目一個JPG(默認包)。我正在使用eclipse。

我的想法是做一個JComboBox,能夠顯示的JPG文件,而使用帶我已經編碼的(但不是剛剛報道,使其短)某些鼠標點擊事件的字符串。

我讀過thisthisthis,但我真的不能完成工作:(

任何人都可以解釋我如何得到我想要的東西,也許我修改代碼,這樣我就可以研究它?

+0

請看看,如何[圖像添加到Eclipse項目(http://stackoverflow.com/a/9278270/1057230),有關使用相同的更多信息,請訪問此[Load ImageIcon Exception](http://stackoverflow.com/a/9866659/1057230),此鏈接中的最後一個鏈接將提供同樣的東西在鏈接1中解釋。一個[示例](http://stackoverflow.com/a/15187181/1057230),雖然不相關,但足以提供一個想法。希望它有幫助:-) –

+1

請參閱[本示例](http://stackoverflow.com/a/9544652/418556)瞭解提示。 –

回答

2

你需要一個定製的ListCellRenderer提供的是能夠顯示圖像的組合框(和你需要其他信息)

詳情請參閱Providing a custom renderer

您可以使用ImageIO API加載圖片。您可能需要包裹導致ImageIcon以便更輕鬆地呈現它,但是這將取決於你的API實現

我會建議使用DefaultListCellRenderer,因爲它從JLabel延伸,會讓你的生活更輕鬆

真正簡單的例子

我沒有足夠的信息來形成一個完全可運行的例子,但本質上,添加到組合框模型中的值應該以某種方式,包含你要像一個參考加載。

這樣,在需要時,可以提取圖像,並使用單元格渲染器顯示它...

public class ImageCellRenderer extends DefaultListCellRenderer { 

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     if (value instanceof ??) { 
      ImageIcon icon = ...; 
      setIcon(icon); 
     } 
     return this; 
    } 

} 

,並應用渲染...

JComboBox cb = new JComboBox(); 
cb.setRenderer(new ImageCellRenderer()); 

更新

現在假設圖像被命名爲[animal].jpg(所以dog將是dog.jpg),你應該LD能夠構建一個簡單的Map,名稱映射到動物形象...

// List of animals... 
final private String[] animals = { "dog", "cat", "mouse" }; 

/*...*/ 

// Map of animal icons... 
Map<String, Icon> mapImages = new HashMap<>(); 
// Build the icon image mapping 
for (String animal : animals) { 
    mapImages.put(animal, new ImageIcon(ImageIO.read(getClass().getResource("/" + animal + ".jpg)))) 
} 

// Create a new cell renderer, passing the mappings 
ImageCellRenderer renderer = new ImageCellRenderer(mapImages); 

// Create a new combo box 
JComboBox<String> comboBox = new JComboBox<String>(animals); 
// Apply the renderer 
comboBox.setRenderer(renderer); 

/*...*/ 

public class ImageCellRenderer extends DefaultListCellRenderer { 

    // Icon mappings 
    private Map<String, Icon> mapImages 

    public ImageCellRenderer(Map<String, Icon> mapImages) { 
     // Make a new reference to the icon mappings 
     this.mapImages = new HashMap<>(mapImages); 
     setHorizontalAlignment(SwingConstants.CENTER); 
    } 

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     if (value instanceof String) { 
      // Look up the icon associated with the animal... 
      Icon icon = mapImages.get(value.toString()); 
      setIcon(icon); 
     } 
     return this; 
    } 

} 
+0

感謝您的回覆。我已經閱讀了第一個鏈接,我正在閱讀第二和第三個鏈接。我問得太多如果我祈禱你寫下一些代碼作爲例子......?對不起,如果是的話,並感謝信息! – Goet

+0

對不起,無法理解......我應該使用什麼參數作爲if?我應該在哪裏放置我的字符串數組?如何將圖標鏈接到字符串? – Goet

+0

哇!我們快到了!現在我在組合框列表中獲得的是左側的jpg和右側的字符串名稱。有一種方法只顯示jpgs?如果是的話,我是否可以使用setSelectedItem(animals [i])和getSelectedItem(animals [i])方法來實現與動物字符串數組中的項目相關的一些鼠標單擊操作?如果我需要發佈代碼,我會同時非常感謝! – Goet