所以我有一个JFrame,在这种情况下我有一个特定值(1.0f)的不透明度,我想要做的是在释放鼠标后我想要改变窗口的不透明度。我怎样才能做到这一点?我尝试使用, AWTUtilities.setWindowOpacity
但这只是给了我一个错误。提前致谢。这是当前代码。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class WindowOne extends JFrame {
public WindowOne() {
super("TranslucentWindow");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Shared.xPressed = getXPosition();
Shared.yPressed = getYPosition();
System.out.println(Shared.xPressed + " " + Shared.yPressed);
}
});
getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
Shared.xReleased = getXPosition();
Shared.yReleased = getYPosition();
System.out.println(Shared.xReleased + " " + Shared.yReleased);
}
});
getContentPane().setLayout(new GridBagLayout());
setLocation(0, 0);
setSize((int)width,(int)height);
getRootPane().setWindowDecorationStyle(JRootPane.NONE);
setVisible(true);
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
WindowOne tw = new WindowOne();
tw.setOpacity(1.0f);
tw.setVisible(true);
}
});
}
public static int getXPosition() {
PointerInfo location = MouseInfo.getPointerInfo();
int x = (int) location.getLocation().getX();
return x;
}
public static int getYPosition() {
PointerInfo location = MouseInfo.getPointerInfo();
int y = (int) location.getLocation().getY();
return y;
}
}