2012-07-17 68 views
6

在我的問題中,我有一個不透明的JPanel和另一個透明(半透明)的JPanel,它位於第一個JPanel上。當我將單選按鈕添加到頂層JPanel時。問題是每當我在每個單選按鈕的標籤區域輸入鼠標時(每次將鼠標移離標籤時),它都變得越來越黑。Swing:將鼠標懸停在半透明的單選按鈕標籤上JPanel

package trial; 

import java.awt.Color; 
import javax.swing.ButtonGroup; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

public class Test { 

public static void main(String arg[]){ 
    JFrame rootframe = new JFrame("Test panel"); 
    rootframe.setSize(800, 550); 
    rootframe.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    JPanel basePanel = new JPanel(); //fills rootFrame 
    basePanel.setOpaque(true); 
    basePanel.setBackground(Color.yellow);  

    JPanel panelContainingRadioButtons = new JPanel();//wraps radio buttons 
    panelContainingRadioButtons.setOpaque(true); 
    panelContainingRadioButtons.setBackground(new Color(0,0,0,100)); 

    ButtonGroup buttonGroup1 = new ButtonGroup(); 

    JRadioButton jRadioButton1 = new JRadioButton(); 
    jRadioButton1.setText("Text A..............................."); 
    jRadioButton1.setOpaque(false); 
    jRadioButton1.setForeground(Color.white); 
    buttonGroup1.add(jRadioButton1); 

    JRadioButton jRadioButton2 = new JRadioButton(); 
    jRadioButton2.setOpaque(false); 
    jRadioButton2.setForeground(Color.white); 
    buttonGroup1.add(jRadioButton2); 
    jRadioButton2.setText("Text B......................."); 

    JRadioButton jRadioButton3 = new JRadioButton(); 
    jRadioButton3.setOpaque(false); 
    jRadioButton3.setForeground(Color.white); 
    buttonGroup1.add(jRadioButton3); 
    jRadioButton3.setText("Text C................................"); 

    panelContainingRadioButtons.add(jRadioButton1); 
    panelContainingRadioButtons.add(jRadioButton2); 
    panelContainingRadioButtons.add(jRadioButton3); 

    basePanel.add(panelContainingRadioButtons); 

    rootframe.add(basePanel); 
    rootframe.setVisible(true); 

} 
} 

我相信這是不是單選按鈕的一個問題,因爲在另一個場合,我觀察到,同等條件下,如果我添加一個JLabel頂端的JPanel,並添加監聽器頂部面板,使得JLabel的文本的顏色會改變,當鼠標懸停,並恢復到原單的顏色,當鼠標退出時,文本被在不同的地方重新繪製,如下圖所示: -

http://s13.postimage.org/6yn3cw48n/Untitled.png

如果有必要我也會發布該代碼。我認爲這兩種情況都存在同樣的問題。

回答

8

您可能會因爲用於背景的透明顏色而獲得這些繪畫工件。 JComponents不支持透明顏色作爲背景顏色。這是@camickr的一個很好的article,詳細解釋了這個問題,並提供了一個替代解決方案。

+1

+1好文章。 – trashgod 2012-07-17 03:51:14

+1

正是我需要的。非常感謝您的先生! – 2012-07-17 14:14:01

1

而不是使用紅色,綠色,藍色和alpha例如:setBackground(new Color(236,233,216,220));使用setBackground(new Color(236,233,216));這是紅色,綠色,藍色。它會完美地工作。