有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java透明JScrollPane

我有一个JTextArea,它位于JScrollPane的顶部。无论如何,我知道我可以使用getViewPort()方法设置视口的不透明属性,但我似乎找不到任何迹象表明如何在任何地方这样做

以下是我到目前为止的情况:

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

共 (3) 个答案

  1. # 1 楼答案

    您与@Serplat的讨论表明您可能混淆了不透明度透明度

    Opacity是用于优化图形的Swing组件的布尔属性:

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

    Transparency是一种合成数字图像的方法,如图example所示

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

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

    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();
                }
            });
        }
    }
    
  2. # 2 楼答案

    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); 
    
  3. # 3 楼答案

    您需要使用setOpaque(false)使其透明。在JScrollPane和它的视口中调用它

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

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