Thursday, December 12, 2013

Draggable Transparent JFrame in JAVA

Here i am going to post about a JFrame that is transparent ,having a close button and is draggable.The JAVA code is given below.

Code:
public class transparentJFrame {
 public static void main(String[] args)
 {
    new transparentJFrame(Date,Head,Detal);
     }
public transparentJFrame(String Date,String Head,String Detal) 
     {  
  
  JFrame frame = new JFrame();
  frame.setUndecorated(true);
  frame.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),1));
  //Change value of '1' above to have diff transparency
    ((JComponent)frame.getContentPane()).setBorder(    
           BorderFactory.createMatteBorder( 3, 2, 2, 2, Color.black));
  frame.setBounds(100, 50, 500, 400);
  frame.getContentPane().setLayout(null);
  
   final JLabel lblX = new JLabel("X");
   lblX.setBounds(frame.getWidth()-15,5,15,15);
   lblX.setForeground(new Color(255, 255,255));
   lblX.setToolTipText("Close\r\n");
   lblX.addMouseListener(new MouseAdapter() {
   @Override
   public void mouseClicked(MouseEvent arg0) {
    frame.dispose();
   }
   @Override
   public void mouseEntered(MouseEvent e) {
    lblX.setForeground(new Color(255, 0, 0));
   }
   public void mouseExited(MouseEvent e) {
    lblX.setForeground(new Color(255, 255,255));
   }
  });
  lblX.setFocusable(false); 
  frame.getContentPane().add(lblX);
  frame.setVisible(true);
 }
}