Remove JTextPane's white background without setOpaque() over a translucent JFrame

前端 未结 2 784
遇见更好的自我
遇见更好的自我 2021-01-24 20:40

I have a Java code where I implemented a translucent JPanel with an image drawn on it with Graphics 2D. This image is a PNG that includes a white rectangle, 80% opaque, all over

相关标签:
2条回答
  • 2021-01-24 21:22

    to make it work with Nimbus L&F:

    jta.setOpaque(false);
    jta.setBorder(BorderFactory.createEmptyBorder());
    jta.setBackground(new Color(0,0,0,0));
    
    0 讨论(0)
  • 2021-01-24 21:24

    This is how I might approach this kind of idea...

    enter image description here

    public class OverlayTextArea {
    
        public static void main(String[] args) {
            new OverlayTextArea();
        }
    
        public OverlayTextArea() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(new ImagePane());
                    frame.setLayout(new BorderLayout());
                    frame.add(new TransparentTextArea());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TransparentTextArea extends JTextArea {
    
            public TransparentTextArea() {
                setOpaque(false);
                setBorder(new CompoundBorder(new EmptyBorder(10, 10, 10, 10), new LineBorder(Color.LIGHT_GRAY)));
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(new Color(255, 255, 255, 128));
                Insets insets = getInsets();
                int x = insets.left;
                int y = insets.top;
                int width = getWidth() - (insets.left + insets.right);
                int height = getHeight() - (insets.top + insets.bottom);
                g.fillRect(x, y, width, height);
                super.paintComponent(g);
            }
    
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage background;
    
            public ImagePane() {
                try {
                    background = ImageIO.read(new File("/path/to/background.img"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
                    g.drawImage(background, x, y, this);
                }
            }
    
        }
    
    }
    

    Feedback

    • You really should call super.paintComponent, failing to do so can lead it some serious trouble, especially when you're dealing with transparent components.
    • Don't perform any long running tasks within the paintXxx methods, like loading images. These methods are intended to return quickly and may be called multiple times within succession...
    0 讨论(0)
提交回复
热议问题