2017-08-20 11 views
0

私はこのプログラムを持っており、comboBoxに背景画像を追加したいと思います。 私は多くの方法を試してみることができないので、誰かを助けるかもしれませんか? myComboBoxコンボボックスの背景としてcomboBoxImageを設定する方法JComboBoxに背景画像を追加する

class myClass 
{ 

    public static void main(String args[]) 
    { 
    JFrame myFrame = new JFrame(); 
    myFrame.setBounds(500,500,500,500); 
    myFrame.setLayout(null); 
    myFrame.setVisible(true); 

    JComboBox myComboBox = new JComboBox(); 
    myComboBox.setBounds(100,100,100,20); 
    myComboBox.add("item1"); 
    myComboBox.add("item2"); 
    myComboBox.setVisible(true); 

    Image comboBoxImage = new ImageIcon(
     myClass.class.getResources("/Image.png")).getImage(); 
    } 

} 

myComboBox.setRenderer(...を);:あなたが使用してボックスをコンボに

答えて

0

カスタムレンダラを設定することができます

レンダラーの可能な実装は次のようになります。

class BackgroundRenderer extends JLabel implements ListCellRenderer<String> { 
    private final Image image; 

    public BackgroundRenderer(Image image) { 
     this.image = image; 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     g.drawImage(image, 0, 0, this); 

     super.paintComponent(g); 
    } 

    @Override 
    public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, 
      boolean isSelected, boolean cellHasFocus) { 
     setText(value); 

     return this; 
    } 
} 
+0

あなたは非常に良いです!ありがとうございました – darkagandal

関連する問題