具有透明背景和内容的JScrollPane [英] JScrollPane with transparent background and content

查看:104
本文介绍了具有透明背景和内容的JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我显示一个弹出对话框以显示大的卡片列表.我将它们显示为JPanel子类中许多JLabel组件中的图像.然后,将该对象放在JScrollPane中,以允许在卡中进行水平滚动.

In my app, I show a popup dialog to show a large list of cards. I display them as images in many JLabel components in a JPanel subclass. I then put that object in a JScrollPane to allow for horizontal scrolling through the cards.

我希望未使用的空间透明并带有深色背景,以显示其背后的内容已被禁用.我使用setBackground(new Color(50, 50, 50, 200))来实现所需的外观,但是其后面的内容没有重绘,因此出现了瑕疵.

I want the unused space to be transparent with a dark background to show that what's behind it is disabled. I used setBackground(new Color(50, 50, 50, 200)) to achieve the look I want, but the content behind it does not redraw, so I get artifacting.

这是它的样子:

我将如何解决此问题?滚动时如何获取其背后的内容以重绘?

How would I go about fixing this? How do I get the content behind it to redraw when I scroll?

谢谢.

推荐答案

将窗口移出组成要素的方程式.

Taking the window out of the equation for the momement.

JScrollPane包含一个JViewport,然后包含您的内容.因此,您需要将内容窗格设置为透明,将视口设置为透明,然后将滚动窗格设置为透明.

The JScrollPane contains a JViewport which then contains you content. So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent.

您可以通过在每个容器上使用setOpaque(false)来实现此目的.

You can achieve this by using setOpaque(false) on each of these containers.

这将确保重新绘制管理器现在可以在背景中绘制.

This will ensure that the repaint manager will now paint through the background.

下一个问题是,Swing实际上不支持半透明"组件(也就是说,它是不透明的或透明的).

The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent).

您可以通过覆盖主要组件的paintComponent方法来实现此目的(视口上的那个方法可能就足够了)

You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient)

这篇关于具有透明背景和内容的JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆