Ich habe einen transparenten JFrame mit einem JButton oben, aber wenn ich den JFrame umherbewege, verschwindet der JButton, bis ich mit der Maus über den Button blicke, dann wird er wieder erscheinen, und wenn ich den JButton in Button ändere, funktioniert es richtig, aber ich muss JButton verwenden, da es einige Eigenschaften hat, die Button nicht hat, also wie man JButton richtig im JFrame arbeiten lässt?Wie zeigt man ein JButton in einem transparenten JFrame an?
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(); } });
}
}
Dort gehen wir, und was dies ein transparenter Rahmen macht ?? – gpasch
Sie sollten wahrscheinlich die Methode 'frame.setOpacity (0.0f)' verwenden, anstatt das zu tun, was Sie gerade tun – explv