Java - 透明的 JScrollPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3517722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:12:51  来源:igfitidea点击:

Java - Transparent JScrollPane

javaswingjscrollpane

提问by test

I have a JTextArea and it's riding ontop of a JScrollPane. Anyways, I know I can use the getViewPort()method to set the opaque of of the view port... but I cannot seem to find any sign of how to do that... anywhere. :S

我有一个 JTextArea,它位于 JScrollPane 之上。无论如何,我知道我可以使用该getViewPort()方法来设置视口的不透明......但我似乎无法找到任何迹象表明如何做到这一点......在任何地方。:S

Here is what I have so far:

这是我到目前为止所拥有的:

 if (e.getKeyCode() == KeyEvent.VK_F)
{
    if (sp.isVisible())
    {
        sp.setVisible(false);
    }
    else
    {
        sp.setVisible(true);
    }
}

回答by Serplat

You need to use setOpaque(false)to make it transparent. Call that both on the JScrollPane, and on it's ViewPort.

您需要使用setOpaque(false)使其透明。在 JScrollPane 和 ViewPort 上调用它。

sp.setOpaque(false);
sp.getViewport().setOpaque(false);

You'll also have to call setOpaque(false)on the JTextArea, if you want that transparent as well.

setOpaque(false)如果您也想要透明,您还必须调用JTextArea。

回答by trashgod

Your colloquy with @Serplat suggests that you may be confounding opacityand transparency.

您与@Serplat 的对话表明您可能混淆了不透明度透明度

Opacityis a boolean property of Swing components used to optimize drawing:

不透明度是用于优化绘图的 Swing 组件的布尔属性:

  • true: The component agrees to paint all of the bits contained within its rectangular bounds.
  • false: The component makes no guarantees about painting all the bits within its rectangular bounds.
  • true:组件同意绘制包含在其矩形边界内的所有位。
  • false:该组件不保证在其矩形范围内绘制所有位。

Transparencyis a means of compositing digital images, as seen in this example.

透明度是合成数字图像的一种方式,如本例所示

Considering the distinction may help to clarify your question or focus your search for more information.

考虑区别可能有助于澄清您的问题或集中搜索更多信息。

Addendum: Based on @camickr's example, the example below shows a blue square that "sticks" to the viewport, while the gray checkerboard may be scrolled over it.

附录:基于@camickr 的示例,下面的示例显示了一个“粘”到视口的蓝色方块,而灰色棋盘格可以在它上面滚动。

ScrollPanePaint

滚动面板绘制

import java.awt.*;
import javax.swing.*;

/** @see https://stackoverflow.com/questions/2846497 */
public class ScrollPanePaint extends JFrame {

    private static final int TILE = 64;

    public ScrollPanePaint() {
        JViewport viewport = new MyViewport();
        viewport.setView(new MyPanel());
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewport(viewport);
        this.add(scrollPane);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private static class MyViewport extends JViewport {

        public MyViewport() {
            this.setOpaque(false);
            this.setPreferredSize(new Dimension(6 * TILE, 6 * TILE));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(TILE, TILE, 3 * TILE, 3 * TILE);
        }
    }

    private static class MyPanel extends JPanel {

        public MyPanel() {
            this.setOpaque(false);
            this.setPreferredSize(new Dimension(9 * TILE, 9 * TILE));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.lightGray);
            int w = this.getWidth() / TILE + 1;
            int h = this.getHeight() / TILE + 1;
            for (int row = 0; row < h; row++) {
                for (int col = 0; col < w; col++) {
                    if ((row + col) % 2 == 0) {
                        g.fillRect(col * TILE, row * TILE, TILE, TILE);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ScrollPanePaint();
            }
        });
    }
}

回答by sathya

Code for JScrollpane Transparent Background.

JScrollpane 透明背景的代码。

  JScrollPane scrollPane = new JScrollPane();

   JViewport viewport = new JViewport();


 //Component that need to be added in Scroll pane//

   viewport.setView(new JPanel());

   viewport.setOpaque(false);

   scrollPane.setViewport(viewport);

   scrollPane.getViewport().setOpaque(false);

   scrollPane.setOpaque(false);

 // Add Scrollpane to Jframe or JPanel//

   add( scrollPane,BorderLayout.CENTER);