2011-07-21 31 views
5

我想用圖標替換單選按鈕列表中的文本。JRadioButton:如何通過IconImage替換文本?

我已經試過這樣:

rotateButton = new JRadioButton(rotateIcon.getImage()); 

但到了這個圖標替換單選按鈕和文本。我想保留單選按鈕並顯示圖像。

我該怎麼辦?


什麼我目前得到的是:

Enter image description here

但我希望它這樣結束了:

Enter image description here

回答

5
+2

+1不錯。我總是看到你在gui-questions :) –

+0

@Eng。 Fouad:我也看到他在大多數Swing的問題中看起來像mKorble是一個揮杆愛好者。 –

+0

@ Eng.Fouad,因爲有很多樂趣,善良的人與和平FullFaces,並學習英語:-)對我來說,就像我知道的任何PL難度更大, – mKorbel

5

我只是轉載使用這個源你描述的行爲:

import java.awt.Image; 
import javax.swing.*; 
import javax.imageio.ImageIO; 
import java.net.URL; 

class RadioWithImage { 

    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://www.gravatar.com/avatar/" + 
      "a1ab0af4997654345d7a949877f8037e?s=128"); 
     Image image = ImageIO.read(url); 
     final ImageIcon imageIcon = new ImageIcon(image); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JRadioButton radioButton = new JRadioButton("A.T.", imageIcon); 
       JOptionPane.showMessageDialog(null, radioButton); 
      } 
     }); 
    } 
} 

Where is the radio button 'selected' icon?

這似乎是我的錯誤,但我不記得看到收音機的圖標。他們應該怎麼看?


時間到達我的'黑箱'。

import javax.swing.*; 

class RadioWithImage { 

    public static void main(String[] args) throws Exception { 
     String url = "http://www.gravatar.com/avatar/" + 
      "a1ab0af4997654345d7a949877f8037e?s=128"; 
     final String html = "<html><body><img src='" + 
      url + 
      "' width=128 height=128>"; 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JRadioButton radioButton = new JRadioButton(html); 
       JOptionPane.showMessageDialog(null, radioButton); 
      } 
     }); 
    } 
} 

Radio Button with Image

如果該技術將不起作用:

  1. 用例需要其他類型的圖標(按壓,翻車,選擇等)
  2. 按鈕被禁用(它會渲染不正確)。
+0

看起來像Java6那樣忘了,但我認爲你在這裏是Htlm Gurus的其中之一... +1 – mKorbel

+0

@mKorbel哦不!我可能看起來像Java程序員的HTML大師,但對網頁設計師來說,我是一個新手。不過謝謝你這麼說。 )順便說一下,你的答案很好,+1。我喜歡包含最新JDocs鏈接的答案。 :) –

+0

我真的很困惑如何快速編碼今天新手 – mKorbel

9

創建一個沒有文本的JRadioButton,並將JLabel放在它旁邊的圖像上。你也可以創建一個類來隱藏複雜性。

import java.awt.event.ActionListener; 

import javax.swing.*; 
import javax.swing.event.ChangeListener; 

public class RadioButtonWithImage extends JPanel { 

private JRadioButton radio = new JRadioButton(); 
private JLabel image; 

public RadioButtonWithImage(Icon icon) { 
    image = new JLabel(icon); 
    add(radio); 
    add(image); 
} 

public void addToButtonGroup(ButtonGroup group) { 
    group.add(radio); 
} 

public void addActionListener(ActionListener listener) { 
    radio.addActionListener(listener); 
} 

public void addChangeListener(ChangeListener listener) { 
    radio.addChangeListener(listener); 
} 

public Icon getImage() { 
    return image.getIcon(); 
} 

public void setImage(Icon icon) { 
    image.setIcon(icon); 
} 

} // end class RadioButtonWithImage 
+0

+1對於答@Eva – Krishna

3

沒有單選按鈕構造函數允許圖像作爲內容參數而不是文本。通過圖像替換單選按鈕的文本的唯一方法是生成html,並將其作爲參數傳遞給默認構造函數。

import javax.swing.*; 

class RadioWithImage { 

    public static void main(String[] args) throws Exception { 
     URL url = Windows_ChordsGenerator.class.getResource("/images/img1.png"); 

     final String html = "<html><body><img src='" + url.toString() +"'>"; 

     JRadioButton radioButton = new JRadioButton(html);  
     } 
} 
+0

很好的答案!這比@Andrew Thompson的答案更有用,因爲它使用的是與jar捆綁的圖像,而不是外部資源。 –