在Java1.5中如何设置 jwindow透明,查了下其他有说用AWTUtilities.setWindowOpacity(frame, 0.5f);但这个是在JDK1.6中的,是否可以把1.6的这个包 import com.sun.awt.AWTUtilities 导入到1.5??还是有其他的方法??请给点实例哈。。谢谢;

解决方案 »

  1.   

    你把你的jre环境设置成1.6的不行么
      

  2.   

    不然你只能重写jwindow了:class MyWindow extends JWindow{
       /** the screenshot that is used to draw transparency */
       protected BufferedImage screenShot = null;
       /** the robot instance that captures screen shots */
       protected Robot robot;
       /** the rectangle that defines the screen dimensions */
       protected Rectangle rect;
       public MyWindow(JFrame frame){
           super(frame);
           try {
    robot = new Robot();
    } catch (AWTException e) {
    e.printStackTrace();
    }
       }
       /* (non-Javadoc)
        * @see java.awt.Component#paint(java.awt.Graphics)
        */
       public void paint(Graphics g) {
           if (robot != null){
               Graphics2D g2D = (Graphics2D)g;            
               g2D.drawImage(screenShot,null,0,0);
               g2D.setColor(Color.RED);
               g2D.drawLine(rect.x,rect.y,rect.x + rect.width,rect.y + rect.height);
           }
       }
       /**
        * Sets the frame visibility and also, if transparency is turned on, captures
        * the screen shot.
        * @param isVisible the visibility of the frame
        */
       public void setVisible(boolean isVisible)
       {
           if (isVisible){
               captureScreenShot();
           }
           super.setVisible(isVisible);
       }
       private void captureScreenShot()
       {
           try
           {
               robot = new Robot();
               rect = getBounds();
               screenShot = robot.createScreenCapture(rect);
           }
           catch (java.awt.AWTException ex) {}
       }
    }
      

  3.   

    使用 jna。下载jna.jar + platform.jar
    使用 WindowUtils的setWindowAlpha方法