简体   繁体   English

如何使透明的jframe可调整大小?

[英]How to make a transparent jframe resizable?

Here is my code: 这是我的代码:

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();
  }
}

The problem is if I setOpacity<1.0 i get an error : 问题是如果我setOpacity <1.0我得到一个错误:

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

And if I make do setUndecorated(true) then I cant resize the Jframe 如果我做setUndecorated(true)然后我不能调整Jframe的大小

I need to be able to resize a transparent JFrame 我需要能够调整透明JFrame的大小

I also need to be able to access the folders under the transparent frame I mean if the transparent window is sitting on the desktop and I want to open a particular folder placed under the window then I should be able to do so without the jframe getting minimized. 我还需要能够访问透明框架下的文件夹,我的意思是,如果透明窗口位于桌面上,并且我想打开放置在窗口下的特定文件夹,那么我应该能够做到而不会将jframe最小化。

Is there any way to do this?? 有什么办法吗?

I searched online but couldn't find a suitable solution. 我在网上搜索,但找不到合适的解决方案。

Resizing of the frame is handled by the frame itself. 框架的大小由框架本身处理。 When you remove the Border decorations you lose the resizing functionality. 当您删除边框装饰时,您将失去调整大小的功能。

So, you need to manage the resizing of the frame yourself. 因此,您需要自己管理框架的大小调整。 Check out Component Resizer for a class that will allow you to resize any component. 查看Component Resizer的类,该类可让您调整任何组件的大小。

The change to your code would be: 您的代码更改将是:

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

But is it possible to keep the border opaque 但是有可能保持边界不透明

Yes, but you will only get the Swing decorated Border, not the platform Border and decorations: 是的,但是您只会得到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();
  }
}

Also it is still not possible to access the content behind the frame 而且仍然无法访问框架后面的内容

Yes, but you need full transparency. 是的,但是您需要完全透明。 If you don't use full transparency then the mouse event is passed to the frame, not the component underneath the frame. 如果不使用完全透明,则鼠标事件将传递到框架,而不是框架下面的组件。

If you what semi transparency then theoretically you could add a MouseListener to the frame to intercept the MouseEvent. 如果您是半透明的,那么从理论上讲,您可以将MouseListener添加到框架中以拦截MouseEvent。 Then you can make your frame invisible. 然后,您可以使框架不可见。 Then you could use a Robot to generate a new MouseEvent which will now be dispatched to the screen. 然后,您可以使用Robot生成一个新的MouseEvent,该事件现在将分派到屏幕上。 You would next to use the frames locationOnScreen(...) method to convert the mouse point from the frames coordinates. 接下来,您将使用框架locationOnScreen(...)方法将鼠标点从框架坐标转换为。 I have never tried this approach. 我从未尝试过这种方法。

try this.. working for me.. 试试这个..为我工作..

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);
            }
        });
    }
}

referenced from this 这里引用

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

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