简体   繁体   English

如何在透明的JFrame中显示JButton?

[英]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 ? 我有一个透明的JFrame,顶部有一个JButton,但是如果我移动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(); } });
  }
}

I figured it out, add a line to the paint() : 我想通了,在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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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