问题描述
花了几分钟来调整桌面时钟时,我发现了一个问题,如果没有帮助,我似乎无法解决...我读了一些类似问题的帖子,但是解决方案对我不起作用.
When spending a few minutes tweaking my desktop clock, I discovered a problem that I seem unable to resolve without help... I read some posts with similar problem but solutions did not work for me.
时钟(典型的Java形式,带有动作监听器和日历)可以正常工作. 预期的调整:将框架",内容窗格"和标签"背景设置为透明,以便仅显示时间/文字..
The clock (in typical java form with an Action Listener and Calendar) works just fine. The intended tweak: To set the Frame, ContentPane and Label backgrounds to transparent so only the time/text shows.
会发生什么情况::如果标签背景是透明的(或者直到不透明为true时,通过设置Alpha直到它变得不透明),基础上的先前显示仍会保留并且不会清除.
What happens is this: When the label background is transparent (or until it's opaque enough by setting the Alpha when Opaque is true), the underlying previous display stay's and does not clear.
为帮助弄清这一点,我整理了以下代码-时间和日期Calendar内容/等被排除在外.这段代码只是我尝试使用/不使用不透明,调用放置等的许多版本中的一个.
To help figure this out, I put together the following code - the time and date Calendar stuff/etc is excluded. This code is just one version of many I tried with/without opaque, placement of calls...etc.
使用动作侦听器确实有所不同-如果对动作侦听器进行了注释/删除,则标签的显示效果很好.取消注释动作侦听器,然后发生问题.
What does make a difference is use of the Action Listener - if the Action Listener is commented/deleted, label's display fine. Un-comment the Action Listener and the problem occurs.
查看图片...感谢您的帮助...谢谢!
See the images… Any help appreciated… Thanks!
fyi-下面:该代码没有导入和注释...
fyi - below: the code sans imports and comments...
黑色bg时钟的屏幕截图
Screenshot of clock with black bg
问题的屏幕截图:
public class Clear extends JFrame {
private JPanel contentPane;
Color ppColor = new Color(255, 255, 0, 0); // r,g,b,a
Color lblColor = new Color(225, 200, 200, 0);
Color lbl2Color = new Color(225, 200, 200, 254);
int delay = 1000;
JLabel lblTime = new JLabel("TESTING");
JLabel lblTime2 = new JLabel("XXXXXX");
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
final Clear frame = new Clear();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Clear() {
setUndecorated(true);
setBackground(ppColor);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(1680, 975, 128, 74);
contentPane = new JPanel();
contentPane.setBackground(ppColor);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblTime.setOpaque(true);
lblTime.setBackground(lblColor);
lblTime.setBounds(0, 0, 125, 30);
contentPane.add(lblTime);
lblTime2.setOpaque(true);
lblTime2.setBackground(lbl2Color);
lblTime2.setBounds(0, 33, 125, 16);
contentPane.add(lblTime2);
ActionListener myTaskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
lblTime.setText("Does it");
lblTime2.setText("work? ");
}
};
new Timer(delay, myTaskPerformer).start();
}
}
推荐答案
Swing组件不适用于基于alpha的颜色.它们要么完全透明,要么完全不透明.
Swing components do not work well with alpha based colors. They are either completely transparent or completely opaque.
如果您指定某个组件为isOpaque
,但使用半透明(alpha)颜色填充,则重新绘制管理器将不会更新该组件后面的区域,并且Graphics
上下文将无法正确清除.请记住,Graphics
上下文是共享资源,因此在组件之前已绘制的所有内容仍将被绘制
If you specifiy that a component is isOpaque
, but fill it using a translucent (alpha) color, the repaint manager won't update the area behind the component and the Graphics
context will not be cleared properly. Remember, the Graphics
context is shared resource, so everything that was painted before your component will still be painted
您可以查看 Java摆动处理透明度和图像的图形毛刺以获取更多详细信息.
You can take a look at Java Swing Graphical Glitches Dealing with Transparency and Images for more details.
但是.最简单的解决方案是创建一个TranslucentPane
,它是从JPanel
之类的东西延伸而来的,使它透明(不是不透明),覆盖它的paintComponent
方法,并从其中绘制半透明(alpha)颜色.然后在上面添加标签.
However. The simplest solution would be to create a TranslucentPane
, the extends from something like JPanel
, make it transparent (not opaque), override it's paintComponent
method and paint the translucent (alpha) color from within it. Then add your label onto this.
查看执行自定义绘画和在AWT和Swing中绘画以获取更多详细信息
Check out Performing Custom Painting and Painting in AWT and Swing for more details
这篇关于JLabel保留先前的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!