写一个java成语接龙可视化软件,要求美观
时间: 2024-03-04 21:51:05 浏览: 19
好的,我可以为您提供一个美观的Java成语接龙可视化软件,使用JavaFX库来实现图形界面。以下是程序代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class IdiomGame extends Application {
// 成语列表
private List<String> idioms = new ArrayList<String>();
// 随机选择一个成语
private String currentIdiom;
@Override
public void start(Stage primaryStage) {
// 初始化成语列表
idioms.add("一鸣惊人");
idioms.add("二话不说");
idioms.add("三心二意");
idioms.add("四面楚歌");
idioms.add("五湖四海");
idioms.add("六神无主");
idioms.add("七上八下");
idioms.add("八仙过海");
idioms.add("九牛二虎之力");
idioms.add("十年树木,百年树人");
// 随机选择一个成语
currentIdiom = idioms.get(new Random().nextInt(idioms.size()));
// 创建组件
Label currentIdiomLabel = new Label("当前成语:" + currentIdiom);
currentIdiomLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");
Label inputLabel = new Label("请输入一个成语:");
inputLabel.setStyle("-fx-font-size: 16px;");
TextField inputField = new TextField();
inputField.setPrefWidth(200);
Button submitButton = new Button("提交");
submitButton.setStyle("-fx-font-size: 16px; -fx-background-color: #4CAF50; -fx-text-fill: white;");
submitButton.setOnAction(e -> {
String userInput = inputField.getText();
// 判断用户输入的成语是否符合规则
if (userInput.charAt(0) == currentIdiom.charAt(currentIdiom.length()-1)) {
// 判断用户输入的成语是否在成语列表中
if (idioms.contains(userInput)) {
currentIdiom = userInput;
currentIdiomLabel.setText("当前成语:" + currentIdiom);
inputField.setText("");
} else {
AlertBox.display("错误", "您输入的成语不在成语列表中,请重新输入。");
}
} else {
AlertBox.display("错误", "您输入的成语不符合规则,请重新输入。");
}
});
Button quitButton = new Button("退出");
quitButton.setStyle("-fx-font-size: 16px; -fx-background-color: #f44336; -fx-text-fill: white;");
quitButton.setOnAction(e -> {
primaryStage.close();
});
HBox inputBox = new HBox(10);
inputBox.setAlignment(Pos.CENTER);
inputBox.getChildren().addAll(inputLabel, inputField);
HBox buttonBox = new HBox(10);
buttonBox.setAlignment(Pos.CENTER);
buttonBox.getChildren().addAll(submitButton, quitButton);
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(currentIdiomLabel, inputBox, buttonBox);
// 创建场景并显示窗口
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("成语接龙");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
// 弹出消息框
public static class AlertBox {
public static void display(String title, String message) {
Stage window = new Stage();
window.setTitle(title);
window.setMinWidth(250);
Label label = new Label(message);
label.setStyle("-fx-font-size: 16px;");
Button closeButton = new Button("关闭");
closeButton.setStyle("-fx-font-size: 16px; -fx-background-color: #2196F3; -fx-text-fill: white;");
closeButton.setOnAction(e -> window.close());
VBox layout = new VBox(20);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(label, closeButton);
Scene scene = new Scene(layout, 300, 150);
window.setScene(scene);
window.showAndWait();
}
}
}
```
这个程序使用JavaFX库来实现图形界面,界面中有一个标签用于显示当前成语,一个文本框用于用户输入成语,一个提交按钮和一个退出按钮。程序会不断循环,直到用户输入的成语不符合规则或不在成语列表中。如果用户输入的成语不符合规则或不在成语列表中,程序会弹出消息框提示用户重新输入。程序还提供了一个AlertBox类用于创建消息框。