我想創建JComboBox
控件與Firefox的URL文本框類似。有誰知道如何自定義JComboBox
的文本框。我想補充的ALIGN.HORIZONTAL_RIGHT
一些圖標附近JComboBox
如何在JComboBox的箭頭圖標附近添加圖標
感謝的箭頭按鈕爲您一絲不苟地解釋。事實上,我將結合DefaultListCellRenderer
和圖標添加到組合框像下面的代碼
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public Main() {
// Create icon "C"
JButton jb = new JButton("C");
jb.setMargin(new Insets(0, 0, 0, 0));
jb.setBounds(245, 2, 18, 18);
// Create combo box
String[] languages = new String[]{"Java", "C#", "PHP"};
JComboBox jc = new JComboBox(languages);
// jc.setEditable(true);
jc.add(jb);
getContentPane().add(jc);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 58));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final Main main = new Main();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main.setVisible(true);
}
});
}
}
但是當我把jc.setEditable(true)
;組合編輯器隱藏了我的圖標。我想另一種方式來模擬Firefox真棒吧。你有什麼想法嗎?
嗨aberrant80, 感謝您的快速答覆。我檢出了DefaultListCellRenderer,這將幫助我自定義像Firefox這樣的列表項。 但另一個在JComboBox文本框中添加圖標,我將使用自定義繪畫來繪製應該在控件右側的圖標。 http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html 謝謝, Minh – Minh 2009-07-03 03:45:54