问题
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.
回答1:
See Backgrounds With Transparency for the probable problem and a couple of solutions.
回答2:
Swing only has a concept of opaque or transparent components, it does not, by itself, know how to deal with a component that is opaque, but has a translucent background color. As far as Swing is concerned, the component is opaque, so it doesn't paint what's underneath the component.
Normally, I would supply a alpha
value which I would then apply to a solid background, but in this example, I'm simply filling the background with what ever background color you supply, so unless you supply a translucent color, it will be filled with a solid color.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTranslucentLabel {
public static void main(String[] args) {
new TestTranslucentLabel();
}
public TestTranslucentLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
try {
TranslucentLabel label = new TranslucentLabel("This is a translucent label");
label.setBackground(new Color(255, 0, 0, 128));
label.setForeground(Color.WHITE);
JLabel background = new JLabel();
background.setIcon(new ImageIcon(ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Rampage_Small.png"))));
background.setLayout(new GridBagLayout());
background.add(label);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(background);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
public class TranslucentLabel extends JLabel {
public TranslucentLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
}
public TranslucentLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
}
public TranslucentLabel(String text) {
super(text);
}
public TranslucentLabel(Icon image, int horizontalAlignment) {
super(image, horizontalAlignment);
}
public TranslucentLabel(Icon image) {
super(image);
}
public TranslucentLabel() {
super();
}
@Override
public boolean isOpaque() {
return false;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g2d);
g2d.dispose();
}
}
}
来源:https://stackoverflow.com/questions/20361432/translucent-jlabel-not-properly-showing-background