在 JFrame 上制作透明的 JPanel

Making a transparent JPanel over a JFrame

我是 java 中的图形新手,目前正在开发游戏。本质上,有上升的气泡,用户必须通过将鼠标移到它们上面来弹出它们。

我已经在 J​​Frame 上制作了动画,我需要在顶部为 MouseMotionListener 添加 JPanel。但是,当我将 JPanel 添加到 JFrame 之上时(即使将 setOpaque 设置为 false),它仍然无法让我看到下面的动画。你可以在下面看到我的代码。如果您发现编码错误,请告诉我。

我有两个解决方案,要么在 JPanel 中设置动画(我不知道该怎么做),要么使 JPanel 透明。

游戏Class:

public class Game extends JPanel{

  public static final int WINDOW_WIDTH = 600;
  public static final int WINDOW_HEIGHT = 400;

  private boolean insideCircle = false;
  private static boolean ifPaused = false;
  private static JPanel mainPanel;
  private static JLabel statusbar;
  private static int clicks = 0;
  //Creates a Bubbles object Array
  Bubbles[] BubblesArray = new Bubbles[5];

  public Game() {

    //initializes bubble objects
     for (int i = 0; i < BubblesArray.length; i++)
       BubblesArray[i] = new Bubbles();
     }

public void paint(Graphics graphics) {

  //makes background white
   graphics.setColor(Color.WHITE);
   graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

   //paints square objects to the screen
   for (Bubbles aBubblesArray : BubblesArray) {
     aBubblesArray.paint(graphics);
   }
}

public void update() {

 //calls the Square class update method on the square objects
 for (Bubbles aBubblesArray : BubblesArray) aBubblesArray.update();
}

    public static void main(String[] args) throws InterruptedException {
     statusbar = new JLabel("Default");
     mainPanel = new JPanel();
     mainPanel.setOpaque(false);
     mainPanel.setBackground(new Color(0,0,0,0));
     mainPanel.setVisible(true);
     mainPanel.addMouseMotionListener(new MouseAdapter() {
       @Override
        public void mouseMoved(MouseEvent e) {
          statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
          }

       public void mouseExited(MouseEvent e){
         ifPaused = true;
        }   

        public void mouseEntered(MouseEvent e){
        ifPaused = false;
         }

     });

     Game game = new Game();
    JFrame frame = new JFrame();

    frame.add(game);
     frame.add(mainPanel);



   frame.add(statusbar, BorderLayout.SOUTH);
   frame.setVisible(true);
   frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
   frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   frame.setTitle("Bubble Burst");
   frame.setResizable(false);
   frame.setLocationRelativeTo(null);

   while (true) {
      if (!ifPaused){
       game.update();
       game.repaint();
       Thread.sleep(5);
      }
   }
}    
}

您的 game 面板未显示,因为您将两个组件添加到 BorderLayout 的相同位置(在 BorderLayout.CENTER)。因此 mainPanel 不是添加 "on top of" game,而是替换它。也就是说:

您的 mainPanel 似乎除了监听鼠标移动之外实际上没有做任何事情。你能不能把 MouseAdapter 添加到你的 game 对象,因为它扩展了 JPanel 像这样:

game.addMouseMotionListener(new MouseAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
        statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
    }

    public void mouseExited(MouseEvent e){
        ifPaused = true;
    }   

    public void mouseEntered(MouseEvent e){
        ifPaused = false;
    }
});