且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有透明度的java全屏窗口

更新时间:2023-01-18 10:59:36


  • 只能使用可见的TaskBar ei

GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();




  • 否则你得到和例外

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
The effects for full-screen windows are not supported. 

或使用 brutte_force DirectX 冻结我的PC twicw,只有power_off来保存PC的GPU

or by using brutte_force to DirectX freezed my PC twicw, only power_off to save PC's GPU

import com.sun.awt.AWTUtilities;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameOpacityExample {

    private JFrame myFrame = new JFrame("Test Frame");
    private boolean opacity = true;
    private boolean resize = true;
    private JButton button = new JButton("Opacity");
    private JButton button1 = new JButton("Resize");

    public JFrameOpacityExample() {
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                Object src = evt.getSource();
                if (opacity) {
                    AWTUtilities.setWindowOpacity(myFrame, 0.50f);
                    opacity = false;
                } else {
                    AWTUtilities.setWindowOpacity(myFrame, 1.0f);
                    opacity = true;
                }
            }
        });
        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                Object src = evt.getSource();
                if (resize) {
                    Rectangle dim = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
                    int h = dim.height;
                    int w = dim.width;
                    myFrame.setBounds(00, 00, w, h);
                    resize = false;
                } else {
                    myFrame.setBounds(100, 100, 400, 400);
                    resize = true;
                }
            }
        });
        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(button1);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.add(panel);
        myFrame.setSize(400, 400);
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrameOpacityExample jFrameOpacityExample = new JFrameOpacityExample();
            }
        });
    }
}