在我的问题中,我有一个不透明的JPanel和另一个半透明(半透明)的JPanel,它位于第一个JPanel上.当我在顶部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,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文本的颜色将当鼠标悬停时更改,并在鼠标退出时重置为原始颜色,文本将在不同位置重绘,如下图所示: –
如有必要,我也会发布该代码.我认为这两个案例都存在同样的问题.
解决方法
您可能会因为用于背景的透明颜色而获得这些绘画工件. JComponents不支持透明颜色作为背景颜色.这是@camickr的一个很好的
article,详细解释了这个问题,并提供了另一种解决方案.