将鼠标悬停在半透明JPanel上的单选按钮标签

将鼠标悬停在半透明JPanel上的单选按钮标签

本文介绍了摆动:将鼠标悬停在半透明JPanel上的单选按钮标签上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的问题中,我有一个不透明的JPanel和另一个位于第一个JPanel上的半透明(半透明)的JPanel.当我将单选按钮添加到顶部的JPanel上时.问题是,每当我在每个单选按钮的标签区域上输入鼠标时(每次将鼠标从标签上移开时),它就会变得越来越暗.

In my problem I have an opaque JPanel and another JPanel that is translucent(semi-transparent) which sits on the first JPanel. When I have added radio buttons onto the top JPanel. The problem is every time I enter mouse over the area of the label of each radio button(and every time I move mouse away from the label), it gets darker and darker.

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);

}
}

我相信这不是单选按钮的问题,因为在另一情况下,我观​​察到在相同条件下,如果我在顶部的JPanel中添加了JLabel,并在顶部的Panel中添加了侦听器,则文本的颜色鼠标悬停时,jLabel的更改会更改,并且在鼠标退出时重置为原始颜色,文本将在不同的位置重绘,如下图所示:-

I believe this is not a problem with radio buttons because, in another occasion I have observed that under same conditions, if I added a JLabel to the top JPanel, and add listeners to the top Panel so that the color of the text of jLabel will change when the mouse is hovered over, and reset to orginal color when the mouse exits, the text gets redrawn in different places as in the image below:-

如有必要,我也将发布该代码.我认为这两种情况都存在相同的问题.

If necessary I will post that code too. I think it is the same problem that is there in both cases.

推荐答案

您可能会因为用于背景的透明颜色而获得这些绘画伪像. JComponents不支持将透明色用作背景色.这是@camickr撰写的很好的文章,其中有详细说明问题,并提供替代解决方案.

You get these painting artifacts probably because of transparent color that is used for a background. JComponents do not support transparent colors as background colors. Here is a good article by @camickr that explains in details the issue and also provides an alternative solution.

这篇关于摆动:将鼠标悬停在半透明JPanel上的单选按钮标签上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:25