简体   繁体   English

JScrollPane具有透明的背景和内容

[英]JScrollPane with transparent background and content

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. 我在JPanel子类中的许多JLabel组件中将它们显示为图像。 I then put that object in a JScrollPane to allow for horizontal scrolling through the cards. 然后我将该对象放在JScrollPane以允许水平滚动卡片。

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. 我使用setBackground(new Color(50, 50, 50, 200))来实现我想要的外观,但它背后的内容不会重绘,所以我得到了瑕疵。

Here's what it looks like: 这是它的样子:

滚动时的工件

How would I go about fixing this? 我该如何解决这个问题? How do I get the content behind it to redraw when I scroll? 滚动时如何获取其背后的内容重绘?

Thanks in advance. 提前致谢。

Taking the window out of the equation for the momement. 将窗口从等式中拉出来。

The JScrollPane contains a JViewport which then contains you content. JScrollPane包含一个JViewport ,然后包含您的内容。 So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent. 因此,您需要将内容窗格设置为透明,将视口设置为透明,然后将滚动窗格设置为透明。

You can achieve this by using setOpaque(false) on each of these containers. 您可以通过在每个容器上使用setOpaque(false)来实现此目的。

This will ensure that the repaint manager will now paint through the background. 这将确保重绘管理器现在将在后台绘制。

The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent). 接下来的问题是,Swing实际上并不支持“半透明”组件(即,它是不透明的或透明的)。

You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient) 你可以通过覆盖主要组件的paintComponent方法来实现这一点(视口上的那个可能就足够了)

Try the following...might give you some relief during scrolling. 尝试以下...在滚动期间可能会让您感到宽慰。 You likely also have a problem when the main frame is maximized or restored. 主框架最大化或恢复时,您可能也会遇到问题。 You will need a listener for those events and a similar fix. 您将需要一个用于这些事件的侦听器和类似的修复程序。

    jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            sevenWondersframe.repaint();
        }
    });
    jScrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            sevenWondersframe.repaint();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM