简体   繁体   中英

How to show a JButton in a transparent JFrame?

I have a transparent JFrame with a JButton on top, but if I move the JFrame around, the JButton will disappear, until I mouse over the button then it will show up again, and if I change the JButton to Button, it works correctly, but I do need to use JButton because it has some properties that Button doesn't have, so how to make JButton work properly in the 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(); } });
  }
}

I figured it out, add a line to the 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
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM