JavaFX、猜字游戏


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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值