import tkinter as tk import random as ra import threading as td import time as ti def Love(): root=tk.Tk() width=200 height=50 screenwidth=root.winfo_screenwidth() screenheight=root.winfo_screenheight() x=ra.randint(0,screenwidth) y=ra.randint(0,screenheight) root.title("❤") root.geometry("%dx%d+%d+%d"%(width,height,x,y)) tk.Label(root,text='I LOVE YOU!',fg='white',bg='pink',font=("Comic Sans MS",15),width=30,height=5).pack() root.mainloop() def Heart(): root=tk.Tk() screenwidth=root.winfo_screenwidth() screenheight=root.winfo_screenheight() width=600 height=400 x=(screenwidth-width)//2 y=(screenheight-height)//2 root.title("❤") root.geometry("%dx%d+%d+%d"%(screenwidth,screenheight,0,0)) tk.Label(root,text='❤',fg='pink',bg='white',font=("Comic Sans MS",500),width=300,height=20).pack() root.mainloop() t=td.Thread(target=Heart) t.start() for i in range(50): t=td.Thread(target=Love) ti.sleep(0.1) t.start()用java实现
时间: 2024-01-03 09:04:56 浏览: 97
这段代码是使用 Python 编写的,如果你需要用 Java 实现类似的功能,可以使用 JavaFX 框架来创建 GUI 界面。以下是 JavaFX 实现类似的代码:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.util.Random;
public class Love extends Application {
private final Random random = new Random();
@Override
public void start(Stage primaryStage) {
// 创建心形标签
Label heartLabel = new Label("❤");
heartLabel.setFont(Font.font("Comic Sans MS", 500));
heartLabel.setTextFill(Color.PINK);
// 创建场景
StackPane heartPane = new StackPane();
heartPane.getChildren().add(heartLabel);
Scene heartScene = new Scene(heartPane, primaryStage.getWidth(), primaryStage.getHeight());
// 创建小心心标签
Label loveLabel = new Label("I LOVE YOU!");
loveLabel.setFont(Font.font("Comic Sans MS", 15));
loveLabel.setTextFill(Color.WHITE);
loveLabel.setStyle("-fx-background-color: pink;");
loveLabel.setPrefWidth(200);
loveLabel.setPrefHeight(50);
// 循环创建窗口显示小心心
for (int i = 0; i < 50; i++) {
Stage loveStage = new Stage();
loveStage.setTitle("❤");
loveStage.setX(random.nextInt((int) (primaryStage.getWidth() - loveLabel.getPrefWidth())));
loveStage.setY(random.nextInt((int) (primaryStage.getHeight() - loveLabel.getPrefHeight())));
loveStage.setScene(new Scene(new StackPane(loveLabel)));
loveStage.show();
}
// 显示心形窗口
primaryStage.setTitle("❤");
primaryStage.setScene(heartScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
需要注意的是,JavaFX 库需要 JDK 8 以上版本才能使用。
阅读全文