2015-05-09 107 views
1

这里是我的代码:如何制作一个透明的jframe可调整大小?

package trialruns; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class TransparentFrame extends JFrame 
{ 
    JButton b1; 
    public TransparentFrame() 
    { 
    setTitle("Transparent Frame Demo"); 
    setSize(400,400); 
    setLayout(new GridBagLayout()); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setUndecorated(true); 
    setVisible(true); 
    setResizable(true); 
    setOpacity(0.4f); 
    } 

    public static void main(String args[]) 
    { 
    new TransparentFrame(); 
    } 
} 

的问题是,如果我setOpacity < 1.0我得到一个错误:

The frame is decorated at java.awt.Frame.setOpacity(Frame.java:960) 

如果我凑合setUndecorated(真),那么我不能调整的JFrame

我需要能够调整透明JFrame的大小

我还需要能够访问tran下的文件夹sparent frame 我的意思是,如果透明窗口坐在桌面上,我想打开一个放置在窗口下的特定文件夹,那么我应该可以在不使jframe最小化的情况下这样做。

有没有办法做到这一点?

我在网上搜索,但找不到合适的解决方案。

回答

2

试试这个..我..工作

import java.awt.*; 
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT; 
import javax.swing.*; 

class TransparentFrame extends JFrame { 

    JButton b1; 

    public TransparentFrame() { 
     setTitle("Transparent Frame Demo"); 
     setSize(400, 400); 
     setAlwaysOnTop(true); 
     setLayout(new GridBagLayout()); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
     setResizable(true); 
     JPanel panel = new JPanel() { 
      @Override 
      protected void paintComponent(Graphics g) { 
       if (g instanceof Graphics2D) { 
        final int R = 255; 
        final int G = 255; 
        final int B = 255; 

        Paint p = 
         new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 
          0.0f, getHeight(), new Color(R, G, B, 0), true); 
        Graphics2D g2d = (Graphics2D)g; 
        g2d.setPaint(p); 
        g2d.fillRect(0, 0, getWidth(), getHeight()); 
       } 
      } 
     }; 
     setContentPane(panel); 
     JButton button = new JButton("Button"); 
     setBackground(new Color(0,0,0,0)); 
     panel.add(button); 
    } 

    public static void main(String args[]) { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice gd = ge.getDefaultScreenDevice(); 
     boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); 
     //If translucent windows aren't supported, exit. 
     if (!isPerPixelTranslucencySupported) { 
      System.err.println("PerPixel Translucency is not supported"); 
      System.exit(0); 
     } 

     JFrame.setDefaultLookAndFeelDecorated(true); 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       TransparentFrame tw = new TransparentFrame(); 
       tw.setVisible(true); 
      } 
     }); 
    } 
} 

引用从框架的this

+0

不,我仍然无法调整它 – newbie2015

+0

k ...我编辑了答案。请检查。 – ELITE

+0

是的,它现在是可以改变大小的 谢谢你的帮助 但是有可能让边框不透明并且内容透明 另外它仍然不可能访问框架后面的内容 即,如果我想打开文件夹或东西在框架后面那么这是不可能的 有没有什么办法来实现呢? – newbie2015

3

调整大小是由框架本身处理。当你删除边界装饰时,你失去了调整功能。

所以,你需要自己管理框架的大小调整。请查看Component Resizer了解可以调整任何组件的类。

你的代码的变化将是:

//setResizable(true); // not needed as this is the default anyway 
setOpacity(0.4f); 
new ComponentResizer(this); 

但有可能保持边境不透明

是的,但你只会得到Swing的装饰边框,而不是平台边框和装饰:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class TransparentFrame2 extends JFrame 
{ 
    public TransparentFrame2() 
    { 
    setTitle("Transparent Frame Demo"); 
    setUndecorated(true); 
    getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 
    setBackground(new Color(0, 0, 0, 0)); 

    setSize(400,400); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
    new TransparentFrame2(); 
    } 
} 

此外它仍然不可能访问框架后面的内容

是的,但你需要完全透明。如果不使用完全透明度,则将鼠标事件传递给框架,而不是框架下方的组件。

如果你是半透明的,那么理论上你可以添加一个MouseListener来截取MouseEvent。然后你可以让你的框架看不见。然后你可以使用Robot生成一个新的MouseEvent,它现在将被分派到屏幕上。接下来使用框架locationOnScreen(...)方法从框架坐标转换鼠标点。我从来没有尝试过这种方法。

相关问题