如何在透明 JFrame 中显示 JButton?

How to show a JButton in a transparent JFrame?

我有一个顶部带有 JButton 的透明 JFrame,但是如果我四处移动 JFrame,JButton 就会消失,直到我将鼠标悬停在按钮上然后它会再次出现,如果我将 JButton 更改为 Button ,它工作正常,但我确实需要使用 JButton,因为它有一些 Button 没有的属性,那么如何使 JButton 在 JFrame 中正常工作?

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Trans_Frame extends JFrame implements WindowListener,ComponentListener
{
  public static final long serialVersionUID=26362862L;
  Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
  int X,Y,W,H;
  Image image;
  boolean bo=true,Record_Events=false,Press_Ctrl_To_Record=false;
  Robot robot;
  Vector<Object> Events_Vector=new Vector<Object>();
//  static Button Record_Button=new Button("Record");
  static JButton Record_Button=new JButton("Record");

  public Trans_Frame(String Title)
  {
    super(Title);
    try
    {
      robot=new Robot();
      capture();
      addWindowListener(this);
      addComponentListener(this);
    }
    catch(Exception e) { e.printStackTrace(); }
  }

  public void capture()
  {
    try
    {
      Rectangle rect=new Rectangle(0,0,d.width,d.height);
      image=robot.createScreenCapture(rect);
    }
    catch(Exception e) { e.printStackTrace(); }
  }

  public void paint(Graphics g) 
  {
    X=getX();
    Y=getY();
    W=getWidth();
    H=getHeight();
    g.drawImage(image,0,0,W,H,X,Y,X+W,Y+H,this);
  }

  public void update(Graphics g) { paint(g); }

  public void windowClosing(WindowEvent we)
  {
    this.dispose();
    System.exit(0);
  }

  public void windowClosed(WindowEvent we) {}
  public void windowOpened(WindowEvent we) {}
  public void windowIconified(WindowEvent we) {}
  public void windowDeiconified(WindowEvent we) {}
  public void windowDeactivated(WindowEvent we) {}
  public void componentHidden(ComponentEvent ce) {}
  public void componentShown(ComponentEvent ce) {}
  public void componentResized(ComponentEvent ce) {}

  public void windowActivated(WindowEvent we)
  {
    if (bo==false)
    {
      setVisible(false);
      capture();
      bo=true;
      setVisible(true);
    }
    else { bo=false; }
  }

  public void Clear_Events() { Events_Vector.clear(); }

  public void componentMoved(ComponentEvent ce) { repaint(); }

  public void Toggle_Press_Ctrl_To_Record() { Press_Ctrl_To_Record=!Press_Ctrl_To_Record; }

  // Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
  static void Create_And_Show_GUI() 
  {
    Dimension Screen_Size=Toolkit.getDefaultToolkit().getScreenSize();
    String Frame_Title="Trans Frame";
    final Trans_Frame frame=new Trans_Frame(Frame_Title);
    frame.setSize(600,500);
    frame.getContentPane().setLayout(new FlowLayout());
    Record_Button.addActionListener(new ActionListener() 
    {
      public void actionPerformed(ActionEvent evt) 
      {
        frame.Record_Events=!frame.Record_Events;
        if (frame.Record_Events) Record_Button.setLabel("Stop");
        else Record_Button.setLabel("Record");      
      }
    });

    frame.add(Record_Button);             // JButton won't work properly, Button works fine, why ?
    frame.setBounds((Screen_Size.width-frame.getWidth())/2,(Screen_Size.height-frame.getHeight())/2,frame.getWidth(),frame.getHeight());
    frame.setVisible(true);
  }

  public static void main(String[] args) 
  {
    // Schedule a job for the event-dispatching thread : creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } });
  }
}

我想通了,在 paint() 中添加一行:

  public void paint(Graphics g) 
  {
    X=getX();
    Y=getY();
    W=getWidth();
    H=getHeight();
    g.drawImage(image,0,0,W,H,X,Y,X+W,Y+H,this);
    Record_Button.repaint();         // This will fix it
  }