Java 设置 JPanel 背景透明,setOpaque() 不让我画画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9803844/
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-08-16 07:48:52  来源:igfitidea点击:

Set JPanel background transparent, setOpaque() doesn't let me paint

javaswingtransparencypaintpaintcomponent

提问by AR89

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tetris extends JFrame {

public Tetris() {

    add(new GamePanel());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setSize(800, 600);
    setVisible(true);
    setLocationRelativeTo(null);
    setTitle("Tetris");
}

public class GamePanel extends JPanel {


    public GamePanel(){
        TetrisBoard tetraBoard= new TetrisBoard();
        GridBagLayout layout= new GridBagLayout();
        this.setLayout(layout);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 1;
        c.ipadx = 190;
        c.ipady = 390;
        c.insets.left= 360;
        layout.setConstraints(tetraBoard, c);
        this.add(tetraBoard);
        setBackground(Color.WHITE);
    }

   @Override
   public void paint(Graphics g){
       super.paint(g);

       g.setFont(new Font("Birth Std", Font.PLAIN, 12));
       g.setColor(Color.LIGHT_GRAY);
       g.drawString("200", 36, 63);
       g.drawString("200", 36, 88);
       g.drawString("200", 36, 114);
    }

}//GamePanel class

public class TetrisBoard extends JPanel implements Runnable{
    private Thread animator= new Thread(this);
    private final int DELAY= 50;

    public TetrisBoard(){
        setFocusable(true);
        //setBackground(Color.WHITE);
        setDoubleBuffered(true);
        //this.setBackground(Color.BLACK);
        setOpaque(false);
    }

    @Override
    public void addNotify() {
    super.addNotify();
        animator = new Thread(this);
        animator.start();
    }//addNotify


   @Override
   public void paint (Graphics g){
       super.paint(g);
       g.drawRect (20, 30, 130, 50);

   }//paint

   @Override
public void run() {
    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    while (true) {
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep < 0)
            sleep = 2;
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
} 

}//TetrisBoard class


public static void main(String[] args) {
    Tetris t = new Tetris();
}

}

}

With this code the result is that it doesn't paint anything at all. I just want the background to be transparent not the images painted over the background, but it looks like the paint method doesn't paint if I setOpaque(false).

使用此代码,结果是它根本不绘制任何内容。我只希望背景是透明的,而不是在背景上绘制的图像,但是如果我 setOpaque(false),则看起来 Paint 方法不会绘制。

Edit: as requested I posted a simple code, TetraBoard is added to the GamePanel (using that GridBagLayout), and GamePanel is added to the frame, those 3 classes are separate files. I want TetraBoard to have a transparent background, so that I can see the background of GamePanel, but what I paint on tetraboard must be visible. If I setOpaque(false), TetraBoard is transparent, but it set on transparent everything I paint on it.

编辑:根据要求,我发布了一个简单的代码,将 TetraBoard 添加到 GamePanel(使用该 GridBagLayout),并将 GamePanel 添加到框架中,这 3 个类是单独的文件。我想让 TetraBoard 有一个透明的背景,这样我就可以看到 GamePanel 的背景,但是我在 tetraboard 上画的东西必须是可见的。如果我 setOpaque(false),TetraBoard 是透明的,但它设置为透明,我在它上面画的所有东西。

回答by Perry Monschau

Edit:Assuming I understand what you're trying to do, replace the following line in the TetrisBoard constructor:

编辑:假设我了解您要执行的操作,请替换 TetrisBoard 构造函数中的以下行:

setOpaque(false);

with:

和:

setBackground(new Color(0,0,0,0));

回答by cybersoft

For example:

例如:

JPanel p = new JPanel() {
    @Override
    public void paintComponent(Graphics g) { // as suggested Andrew
        g.setColor(Color.RED);
        g.drawArc(0, 0, 100, 100, 0, 360); // arc will be painted on transparent bg
    }
};
p.setBackground(new Color(0, 0, 0, 0)); // as suggested Perry
...

So, you have to do two actions:
1) override paintComponent(Graphics g)of JPanel
2) and set bg color to transparent: new Color(0, 0, 0, 0)

因此,您必须执行两个操作:
1) 覆盖paintComponent(Graphics g)JPanel
2) 并将 bg 颜色设置为透明:new Color(0, 0, 0, 0)