2014-01-15 227 views
-1

我問here如何正確添加背景圖片的JComboBox,但沒有看到答案,所以我想,我應該對這兩個問題分開......如何將背景圖像添加到JComboBox?

所以。如何將JComboBox文本字段和JComboBox彈出式面板的背景圖像正確添加?

UPD: 一些對你的代碼)

當前代碼:

// ... Renderer 

public class CBoxListCellRenderer implements ListCellRenderer { 

    ImagePanel panel; 
    JLabel label = new JLabel(); 

    public CBoxListCellRenderer(Image img) {panel = new ImagePanel(img);} 

    public void setImage(Image img) {panel.setImage(img);} 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      label.setText(value.toString()); 
      panel.add(label); 
      return panel; 
    } 
} 

// ... ImagePanel 

public class ImagePanel extends JPanel { 

    private BufferedImage img; 

    public ImagePanel(String img) { 
     setImage(img); 
    } 

    public void setImage(String img) 
    { 
     try { 
      this.img = ImageIO.read(this.getClass().getResource(img)); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     Dimension size = new Dimension(this.img.getWidth(), this.img.getHeight()); 
     setSize(size); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     g.drawImage(img, 0, 0, getWidth(), getHeight(), 0, 0, img.getWidth(), img.getHeight(), this); 
    } 
} 

// ... Colorizing arrow 

class ColorArrowUI extends BasicComboBoxUI { 

    public static ComboBoxUI createUI(JComponent c) { 
     return new ColorArrowUI(); 
    } 

    @Override protected JButton createArrowButton() { 
      return new BasicArrowButton(
       BasicArrowButton.SOUTH, 
       Color.cyan, Color.magenta, 
       Color.yellow, Color.blue); 
    } 
} 

// ... Creating object 

combo_language = new JComboBox(new DefaultComboBoxModel(new String[] { "English", "日本語", "Русский" })); 
combo_language.setBorder(null); 
combo_language.setRenderer(new CBoxListCellRenderer(new ImageIcon(getClass().getResource("/Images/form.png")).getImage())); 
combo_language.setUI(new ColorArrowUI()); 


// ... Putting JComboBox to JFrame 
+0

但未看到答案,請查看incl。合理的代碼,也適用於JComboBox – mKorbel

+0

@mKorbel接受的答案或...? – RussianVodka

+0

是的,但是您必須爲JComponents使用不透明度,默認情況下每個setOpaque都可以透明 – mKorbel

回答

2

這裏有一個自定義ListCellRenderer的例子。我返回一個JPanel,它已經繪製了一個圖像,然後添加了一個保存列表對象值的JLabel。它有改進的空間,但它只是你能做的一個例子。

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class TestCBoxListCellRenderer { 

    public TestCBoxListCellRenderer() { 
     String[] list = {"Hello World 1", "Hello World 2", "Hello World 3"}; 
     JComboBox box = new JComboBox(list); 
     box.setRenderer(new CBoxListCellRenderer()); 

     JOptionPane.showMessageDialog(null, box, "Check out this Renderer", JOptionPane.PLAIN_MESSAGE); 
    } 

    public static void main(String[] args) { 
     new TestCBoxListCellRenderer(); 
    } 

    class BackGroundPanel extends JPanel { 

     BufferedImage img = null; 

     public BackGroundPanel() { 
      try { 
       img = ImageIO.read(TestCBoxListCellRenderer.class.getResource("/res/leafcell.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(img, 0, 0, getWidth(), getHeight(), 
        0, 0, img.getWidth(), img.getHeight(), this); 
     } 
    } 

    private class CBoxListCellRenderer implements ListCellRenderer { 

     final BackGroundPanel panel = new BackGroundPanel(); 
     JLabel label = new JLabel(); 

     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 

      label.setText(value.toString()); 
      panel.add(label); 

      return panel; 
     } 
    } 
} 

enter image description here

您可能需要添加一個細線邊框或它的選擇時,它給你做一些事情。使用getListCellRendererComponent方法中的變量根據您的喜好進行渲染

+3

**從來沒有**在每個getXX中重新創建渲染組件!不正確的代碼往往是粘性的:-) – kleopatra

+0

@kleopatra你能詳細說明一下。我不確定你的意思。 –

+0

並且不在Renderer中加載FileIO,不管是否來自Java包,仍然很難並且長時間運行FileIO(通過@kleopatra)將JPanel(final)創建爲本地變量,停止重新繪製JPanel(請參閱API) – mKorbel

相關問題