Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

java - Translucent JLabel not properly showing background

I have the following line:

        label.setBackground(new java.awt.Color(0, 150, 0, 50));

I place this in a mouseReleased method within a MouseAdapter.

Basically, I want to make the label highlight itself in translucent green when I click on it.

I have several labels in a panel, all with this MouseAdapter added to them.

My problem is this:

-When I click on the label, it shows the translucent green color, but it is showing the background of ANOTHER JLabel, not the one I click on.

No matter which label I click on, it always paints the background of the same label.

-Whenever I click on a label, it repeats the same background. -Weirdly, every time I click on a JLabel, the opacity of the green color seems to increase, as if it were painting the translucent green over itself each time I click on a new JLabel.

Any tips on what's going on? Should I try to post an SSCCE on this? Or is there a simple answer I'm missing. The reason that I didn't post an SSCCE yet is that my code is large and spread across multiple files, so I must trim it out first.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See Backgrounds With Transparency for the probable problem and a couple of solutions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...