2012-12-18 38 views
1

我有使JComboBox透明的問題。我試圖設置不透明爲背景0的錯誤和alpha,但它不起作用。我想,我需要改變一些的類,它呈現什麼similar.And這裏是代碼..使JComboBox透明

import java.awt.EventQueue; 
    import java.awt.Graphics; 
    import java.awt.Rectangle; 

    import javax.swing.JFrame; 
    import javax.swing.JComboBox; 
    import javax.swing.JTextField; 
    import javax.swing.plaf.basic.BasicComboBoxUI; 

    import java.awt.Color; 


public class App { 

private JFrame frame; 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       App window = new App(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public App() { 
    initialize(); 
} 

private void initialize() { 
    frame = new JFrame(); 
    frame.getContentPane().setBackground(Color.GREEN); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; 
    JComboBox comboBox = new JComboBox(petStrings); 
    comboBox.setBounds(149, 99, 155, 20); 
    comboBox.setOpaque(false); 
    //comboBox.setBackground(new Color(0,0,0,0)); 
    ((JTextField)comboBox.getEditor().getEditorComponent()).setOpaque(false); 
    comboBox.setUI(new BasicComboBoxUI(){ 

     public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus){}}); 
    frame.getContentPane().add(comboBox); 

} 

} 
+0

你能告訴我們哪裏是問題在你的身邊,爲更好地幫助更快張貼[SSCCE(http://sscce.org/),短,可運行,可編譯,只是約JFrame和JComboBox – mKorbel

+0

檢查這[後](http://www.pushing-pixels.org/2008/02/27/translucent-and-shaped-windows-in-core-java.html )出來。 – bonCodigo

+1

@bonCodigo - 您提到的帖子是關於創建造型和半透明的窗戶。不要認爲它適用於JComboBox。 – sreejit

回答

0

您需要預先設置這幾件事情

jcombo.setOpaque(false); 
jcombo.setContentAreaFilled(false); 
jcombo.setBorderPainted(false); 
+0

setContentAreaFilled()甚至可用於JComboBox? – sreejit

+0

不,setContentAreaFilled()不可用 – user1610362

2

假設你只是想組合框的文本字段透明(不是彈出窗口),使用下面的代碼應該可以工作。您需要混淆ComboBox渲染器而不是編輯器。編輯器用於輸入組合框;如果ComboBox只是一個值列表,則使用渲染器。

comboBox.setOpaque(false); 
comboBox.setRenderer(new DefaultListCellRenderer(){ 
    @Override 
    public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 
     JComponent result = (JComponent)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
     result.setOpaque(false); 
     return result; 
    }}); 
+0

爲了理解爲什麼setOpaque技巧不適用於Nimbus L&F,請參閱:http://stackoverflow.com/questions/2451990/setopaquetrue-false-java – lbalazscs

+0

我如何刪除邊框?我試過comboBox.setBorder(null);和comboBox.setBorder(new EmptyBorder(0,0,0,0));但它不起作用。 – user1610362

+0

該解決方案不適用於Windows樣式JComboBox,UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());! – user1610362

0

試試這個。

yourComboBox.setOpaque(false); 
((JTextField)yourComboBox.getEditor().getEditorComponent()).setOpaque(false); 

setUI(new BasicComboBoxUI() { 

    @Override  
    public void paintCurrentValueBackground(
     Graphics g, Rectangle bounds, boolean hasFocus) { 

    } 
}); 
0
JComboBox myComboBox = new JComboBox(array); 
myComboBox .setOpaque(false); 
myComboBox .setEditable(true); 
JTextField boxField = (JTextField)myComboBox .getEditor().getEditorComponent(); 
boxField.setBorder(BorderFactory.createEmptyBorder()); 
boxField.setBackground(new Color(0, 0, 0, 0)); 
boxField.setFocusable(false); 

答案是http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6687960