如何设置JPanel的透明背景?

2022-09-01 12:45:27

JPanel的背景可以设置为透明吗?

我的框架有两个s:JPanel

  • 图像面板
  • 功能面板

功能面板是重叠的图像面板图像面板用作背景,并从远程 URL 加载图像。

功能面板上,我想绘制形状。现在,由于功能面板的背景颜色,无法看到图像面板

我需要使功能面板背景透明,同时仍然绘制其形状,并且我希望图像面板可见(因为它正在执行图像的平铺和缓存功能)。

我使用两个,因为我需要将图像和形状绘图分开。JPanel

有没有办法让重叠的Jpanel有一个透明的背景?


答案 1

调用上部应该有效。setOpaque(false)JPanel

从您的评论中,听起来摇摆绘画可能在某个地方被打破了 -

首先 - 您可能希望覆盖而不是覆盖的任何组件。paintComponent()paint()paint()

第二 - 当你覆盖时,你首先要首先调用来做所有默认的Swing绘画的东西(荣誉就是其中之一)。paintComponent()super.paintComponent()setOpaque()

示例 -

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class TwoPanels {
    public static void main(String[] args) {

        JPanel p = new JPanel();
        // setting layout to null so we can make panels overlap
        p.setLayout(null);

        CirclePanel topPanel = new CirclePanel();
        // drawing should be in blue
        topPanel.setForeground(Color.blue);
        // background should be black, except it's not opaque, so 
        // background will not be drawn
        topPanel.setBackground(Color.black);
        // set opaque to false - background not drawn
        topPanel.setOpaque(false);
        topPanel.setBounds(50, 50, 100, 100);
        // add topPanel - components paint in order added, 
        // so add topPanel first
        p.add(topPanel);

        CirclePanel bottomPanel = new CirclePanel();
        // drawing in green
        bottomPanel.setForeground(Color.green);
        // background in cyan
        bottomPanel.setBackground(Color.cyan);
        // and it will show this time, because opaque is true
        bottomPanel.setOpaque(true);
        bottomPanel.setBounds(30, 30, 100, 100);
        // add bottomPanel last...
        p.add(bottomPanel);

        // frame handling code...
        JFrame f = new JFrame("Two Panels");
        f.setContentPane(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    // Panel with a circle drawn on it.
    private static class CirclePanel extends JPanel {

        // This is Swing, so override paint*Component* - not paint
        protected void paintComponent(Graphics g) {
            // call super.paintComponent to get default Swing 
            // painting behavior (opaque honored, etc.)
            super.paintComponent(g);
            int x = 10;
            int y = 10;
            int width = getWidth() - 20;
            int height = getHeight() - 20;
            g.drawArc(x, y, width, height, 0, 360);
        }
    }
}

答案 2

或者,考虑一下“玻璃窗格”,该窗格在文章如何使用根窗格中进行了讨论。您可以在玻璃窗格的方法中绘制“功能”内容。paintComponent()

附录:使用GlassPaneDemo,我添加了一张图片:

//Set up the content pane, where the "main GUI" lives.
frame.add(changeButton, BorderLayout.SOUTH);
frame.add(new JLabel(new ImageIcon("img.jpg")), BorderLayout.CENTER);

并改变了玻璃窗格的方法:paintComponent()

protected void paintComponent(Graphics g) {
    if (point != null) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, 0.3f));
        g2d.setColor(Color.yellow);
        g2d.fillOval(point.x, point.y, 120, 60);
    }
}

enter image description here

如此处所述,Swing 组件必须遵守不透明属性;在此变体中,将完全填充框架的默认布局。ImageIconBorderLayout.CENTER


推荐