简体   繁体   中英

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 :

    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

I need to be able to resize a transparent 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.

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.

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:

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. 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. You would next to use the frames locationOnScreen(...) method to convert the mouse point from the frames coordinates. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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