package com.example.javafxproject;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class HangMan extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane(getPane());
TextField textField = new TextField();
pane.setBottom(textField);
BorderPane.setAlignment(pane.getBottom(), Pos.BOTTOM_CENTER);
textField.setAlignment(Pos.BASELINE_RIGHT);
textField.setPadding(new Insets(10));
textField.setOnAction(e -> { //文本域注册动作事件
String text = textField.getText().trim().replaceAll(" ", ""); //从文本域中获取文本
//如果单词猜对,显示正确,否则,显示错误
System.out.println(text.equalsIgnoreCase("hangman") ? "You are correct!" : "You are error!");
});
Scene scene = new Scene(pane, 300, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("猜字游戏");
primaryStage.show();
}
/**返回草图*/
private Pane getPane() {
Group group = new Group();
Arc arc = new Arc(50, 210, 40, 16, 0, 180); //底座
arc.setStyle("-fx-stroke: black; -fx-fill: white;");
arc.setType(ArcType.OPEN);
Circle head = new Circle(130, 55, 16); //头
head.setStyle("-fx-fill: white; -fx-stroke: black;");
Line leftHand = new Line(130, 55, 90, 100); //左手
Line rightHand = new Line(130, 55, 170, 100); //右手
Line body = new Line(130, 55, 130, 120); //身体
Line leftLeg = new Line(130, 120, 95, 150); //左腿
Line rightLeg = new Line(130, 120, 165, 150); //右腿
group.getChildren().addAll(new Polyline(50, 200, 50, 25, 130, 25, 130, 50), arc,leftHand,
rightHand, leftLeg, rightLeg, body, head);
return new BorderPane(group);
}
}
06-10
1218