JFrame 或 JPanel 中的透明背景

问题描述

我一直试图让我的 JFrame 透明,但每次我做我的绘画图形时,JPanel 都会消失 根据我的理解,JPanel 位于 JFrame 之上 我希望 Paint 图形可见但我很确定 The Window 的背景是 JFrame 100% 透明(清晰) 我做错了什么?

TheBallandTheBoard-JFrame

package brickBracker;

import javax.swing.JFrame;

import java.awt.*;

public class TheBallandTheBoard extends JFrame{
    public static void main(String[] args) {
        JFrame obj = new JFrame();
        GamePlay gamePlay = new GamePlay();
        obj.setBounds(10,10,700,600);
        obj.setTitle("TheBall and TheBoard");
        obj.setResizable(true);
        obj.setVisible(true);
        // obj.setBackground(new Color(0,0));
        // obj.setUndecorated(true);
        // obj.getContentPane().setBackground(new Color(0,0));
        // obj.setopacity(0.0F);
        obj.add(gamePlay);
        obj.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);





    }
    /* public TheBallandTheBoard (){
        setUndecorated(true);
        setopacity(0.0F);
    } */

线

14 - 17

26 - 29

GamePlay-JPanel

package brickBracker;

import javax.swing.*;
// import javafx.scene.shape.Sphere;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GamePlay extends JPanel implements KeyListener,ActionListener {
    private boolean play;

    {
        play = false;
    }
    private int score = 0;

    private int totalBricks = 21;

    private Timer timer;
    private  int delay = 8;

    private int playerX = 310;

    private int ballposX = 120;
    private int ballposY = 350;
    private int ballXdir = -1;
    private int ballYdir = -2;

    private MapGenerater map;

    public GamePlay(){
        map = new MapGenerater(3,7);
        setBackground(new Color(0,0));
        setopaque(false);
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(true);
        timer = new Timer(delay,this);
        timer.start();
    }

    public void paint(Graphics g) {
        //BackGround
        g.setColor(new Color(0,0));
        setopaque(false);
        g.fillRect(1,1,692,592);

        //drawing map
        map.draw((Graphics2D) g);

        //Borders
        g.setColor(new Color(0,0));
        setopaque(false);

        g.fillRect(0,3,592);
        g.fillRect(0,3);
        g.fillRect(691,592);

        //score
        g.setColor(Color.WHITE);
        g.setFont(new Font("kai",Font.BOLD,25));
        g.drawString(""+score,590,30);

        //timer
        //g.drawString(""+timer,300,30);

        // The Board
        g.setColor(Color.WHITE);
        g.fillRect(playerX,550,100,8);

        //The Ball
        g.setColor(Color.RED);
        g.filloval(ballposX,ballposY,20,20);

        g.dispose();


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
        if (play){
            if (new Rectangle(ballposX,20).intersects(new Rectangle(playerX,8))) {
                ballYdir = -ballYdir;
            }
            A: for(int i = 0 ; i<map.map.length; i++) {
                for(int j = 0; j<map.map[0].length; j++) {
                    if(map.map[i][j] > 0) {
                        int brickX = j * map.brickWidth + 80;
                        int brickY = i * map.brickHight + 50;
                        int brickWidth = map.brickWidth;
                        int brickHight = map.brickWidth;

                        Rectangle rect = new Rectangle(brickX,brickY,brickWidth,brickHight);
                        Rectangle ballRect = new Rectangle(ballposX,20);
                        Rectangle brickRect = rect;

                        if(ballRect.intersects(brickRect)) {
                            map.setBrickValue(0,i,j);
                            totalBricks--;
                            score += 5;

                            if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + ballRect.width) {
                                ballXdir = -ballXdir;
                            } else {
                                ballYdir = -ballYdir;
                            }

                            break A;
                        }
                    }
                }
            }

            ballposX += ballXdir;
            ballposY += ballYdir;
            if (ballposX < 0){
                ballXdir = -ballXdir;

            }
            if (ballposY < 0){
                ballYdir = -ballYdir;

            }
            if (ballposX > 670){
                ballXdir = -ballXdir;

            }
        }
        repaint();

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keypressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            if (playerX >=600) {
                playerX = 600;
            } else {
                moveRight();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            if (playerX < 10) {
                playerX = 10;
            } else {
                moveLeft();
            }
        }
    }

    @Override
    public void keyreleased(KeyEvent e) {

    }

    public void moveRight(){
        play = true;
        playerX+=20;
    }
    public void moveLeft(){
        play = true;
        playerX-=20;


}}

线

35 和 36

45 - 48

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)