Java - 透明 JScrollPane

发布于 2024-09-15 08:47:45 字数 312 浏览 4 评论 0原文

我有一个 JTextArea,它位于 JScrollPane 之上。无论如何,我知道我可以使用 getViewPort() 方法来设置视口的不透明属性,但我似乎找不到任何迹象表明如何在任何地方执行此操作。

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

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

I have a JTextArea and it's riding on top of a JScrollPane. Anyways, I know I can use the getViewPort() method to set the opaque property of the viewport, but I cannot seem to find any sign of how to do that anywhere.

Here is what I have so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初懵 2024-09-22 08:47:45

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

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

如果您也希望透明,您还必须在 JTextArea 上调用 setOpaque(false)

You need to use setOpaque(false) to make it transparent. Call that both on the JScrollPane, and on it's 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.

忆沫 2024-09-22 08:47:45

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

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

  • true:组件同意绘制其矩形边界内包含的所有位。
  • false:组件不保证绘制其矩形边界内的所有位。

透明度是一种合成数字图像的方法,如图所示在此示例中。

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

附录:基于@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();
            }
        });
    }
}

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

Opacity is a boolean property of Swing components used to optimize drawing:

  • 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.

Transparency is 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.

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();
            }
        });
    }
}
深海夜未眠 2024-09-22 08:47:45

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); 

Code for JScrollpane Transparent Background.

  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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文