簡體   English   中英

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

[英]How to make a transparent jframe resizable?

這是我的代碼:

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(true)然后我不能調整Jframe的大小

我需要能夠調整透明JFrame的大小

我還需要能夠訪問透明框架下的文件夾,我的意思是,如果透明窗口位於桌面上,並且我想打開放置在窗口下的特定文件夾,那么我應該能夠做到而不會將jframe最小化。

有什么辦法嗎?

我在網上搜索,但找不到合適的解決方案。

框架的大小由框架本身處理。 當您刪除邊框裝飾時,您將失去調整大小的功能。

因此,您需要自己管理框架的大小調整。 查看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(...)方法將鼠標點從框架坐標轉換為。 我從未嘗試過這種方法。

試試這個..為我工作..

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

這里引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM