我成功地使用
JWindow
创建了一个无边框窗口,并使用 setOpacity(
) 函数创建了一个透明窗口。
我剩下的就是创建一个基于形状的窗口。我没有任何线索或想法。如果可能的话也使用
JWindow
这是您请求的示例:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class ShapedWindowExample {
public static void main(String[] args) {
JWindow window = new JWindow();
window.setShape(new Ellipse2D.Double(0, 0, 300, 300));
window.setSize(300, 300);
window.getContentPane().setBackground(Color.BLUE);
window.setVisible(true);
}
}
但是我建议您使用 JavaFx,这是一种更现代、更好的
Java
UI 库。 Swing
已经很老了,一般不再使用了。
JavaFx 示例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class ShapedWindowFX extends Application {
@Override
public void start(Stage primaryStage) {
// Creates a circle shape
Circle circleShape = new Circle(150);
circleShape.setFill(Color.BLUE);
// Creates a scene with the wanted circle shape
Scene scene = new Scene(new StackPane(circleShape), 300, 300);
scene.setFill(Color.TRANSPARENT);
// Sets the stage with the scene
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
}
// Main Method
public static void main(String[] args) {
launch(args);
}
}