You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX游戏开发:规则按钮无法显示问题求助及规则页实现需求

解决建议

问题根源

你当前的代码里根本没创建规则按钮,也没把它加入启动场景的布局,这就是按钮不显示的核心原因。另外原启动场景的VBox间距设为250,就算加了按钮,位置也会很奇怪甚至超出窗口范围。

具体修改步骤

1. 添加规则按钮并调整布局

在启动场景的代码块里,先创建规则按钮,然后调整VBox的间距,最后把按钮加入布局:

// ********Start scene************
Button PlayButton = new Button("Play");
PlayButton.setMinWidth(55);
PlayButton.setMinHeight(30);

// 新增:创建规则按钮
Button RulesButton = new Button("规则");
RulesButton.setMinWidth(55);
RulesButton.setMinHeight(30);

// 调整VBox的间距,从250改成50,避免元素间距过大
VBox layout1 = new VBox(50.0);
layout1.setAlignment(Pos.CENTER);
Label StartSceneLabel = new Label("Wordle");
StartSceneLabel.setMinWidth(55);
StartSceneLabel.setMinHeight(30);
StartSceneLabel.setAlignment(Pos.CENTER);

StartSceneLabel.setStyle("-fx-background-color: #FFFFFF;");
// 把规则按钮加入布局的children列表
layout1.getChildren().addAll(StartSceneLabel, PlayButton, RulesButton);
primary.setTitle("Start screen");
layout1.setStyle("-fx-background-color:#800000");
// ********Start scene************

2. 创建规则场景

在启动场景代码之后,添加规则页面的布局和场景:

// ********规则场景************
VBox rulesLayout = new VBox(20);
rulesLayout.setAlignment(Pos.CENTER);
rulesLayout.setStyle("-fx-background-color:#800000; -fx-padding: 20;");

Label rulesTitle = new Label("游戏规则");
rulesTitle.setStyle("-fx-text-fill: white; -fx-font-size: 24;");

Label rulesContent = new Label("""
        1. 你需要猜测一个4字母的单词
        2. 每次输入后,系统会给出提示:
           - 绿色:字母正确且位置正确
           - 黄色:字母正确但位置错误
           - 灰色:字母不在目标单词中
        3. 你有6次猜测机会
        """);
rulesContent.setStyle("-fx-text-fill: white; -fx-font-size: 14; -fx-wrap-text: true;");

Button backButton = new Button("返回");
backButton.setMinWidth(55);
backButton.setMinHeight(30);

rulesLayout.getChildren().addAll(rulesTitle, rulesContent, backButton);
Scene RulesScene = new Scene(rulesLayout, 500, 750);
// ********规则场景************

3. 绑定按钮跳转事件

在PlayButton的事件之后,添加规则按钮和返回按钮的点击事件:

// Button actions
PlayButton.setOnAction((e) -> {
    primary.setScene(playscene.playscence());
    primary.setTitle("Game Screen");
});

// 新增:规则按钮跳转事件
RulesButton.setOnAction((e) -> {
    primary.setScene(RulesScene);
    primary.setTitle("游戏规则");
});

// 新增:返回按钮跳转回启动场景
backButton.setOnAction((e) -> {
    primary.setScene(StartScene);
    primary.setTitle("Start screen");
});

primary.setScene(StartScene);
primary.show();

4. 修复main方法

你当前的main方法没有参数,JavaFX的main方法需要接收String数组参数,否则可能无法正常启动:

public static void main(String[] args) {
    launch(args);
}

修改后完整代码片段

把上述修改整合后,你的Main类大概是这样:

package com.example.jaxafx_practice;
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.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.IOException;

public class Main extends Application {
    Scene StartScene;
    @Override
    public void start(Stage primary) throws IOException {

        // ********Start scene************
        Button PlayButton = new Button("Play");
        PlayButton.setMinWidth(55);
        PlayButton.setMinHeight(30);

        Button RulesButton = new Button("规则");
        RulesButton.setMinWidth(55);
        RulesButton.setMinHeight(30);

        VBox layout1 = new VBox(50.0);
        layout1.setAlignment(Pos.CENTER);
        Label StartSceneLabel = new Label("Wordle");
        StartSceneLabel.setMinWidth(55);
        StartSceneLabel.setMinHeight(30);
        StartSceneLabel.setAlignment(Pos.CENTER);

        StartSceneLabel.setStyle("-fx-background-color: #FFFFFF;");
        layout1.getChildren().addAll(StartSceneLabel, PlayButton, RulesButton);
        primary.setTitle("Start screen");
        layout1.setStyle("-fx-background-color:#800000");
        HBox user_interface=new HBox();

        StartScene = new Scene(layout1, 500, 750.0);
        // ********Start scene************

        // ********规则场景************
        VBox rulesLayout = new VBox(20);
        rulesLayout.setAlignment(Pos.CENTER);
        rulesLayout.setStyle("-fx-background-color:#800000; -fx-padding: 20;");

        Label rulesTitle = new Label("游戏规则");
        rulesTitle.setStyle("-fx-text-fill: white; -fx-font-size: 24;");

        Label rulesContent = new Label("""
                1. 你需要猜测一个4字母的单词
                2. 每次输入后,系统会给出提示:
                   - 绿色:字母正确且位置正确
                   - 黄色:字母正确但位置错误
                   - 灰色:字母不在目标单词中
                3. 你有6次猜测机会
                """);
        rulesContent.setStyle("-fx-text-fill: white; -fx-font-size: 14; -fx-wrap-text: true;");

        Button backButton = new Button("返回");
        backButton.setMinWidth(55);
        backButton.setMinHeight(30);

        rulesLayout.getChildren().addAll(rulesTitle, rulesContent, backButton);
        Scene RulesScene = new Scene(rulesLayout, 500, 750);
        // ********规则场景************

        //????????Play scene?????????????
        Playscene playscene = new Playscene();
        //????????Play scene?????????????

        //Create the word to be guessed
        Choose_word Choose_word = new Choose_word();
        String wordToGuess = Choose_word.getWord();

        //Button actions
        PlayButton.setOnAction((e) -> {
            primary.setScene(playscene.playscence());
            primary.setTitle("Game Screen");});

        RulesButton.setOnAction((e) -> {
            primary.setScene(RulesScene);
            primary.setTitle("游戏规则");
        });

        backButton.setOnAction((e) -> {
            primary.setScene(StartScene);
            primary.setTitle("Start screen");
        });

        playscene.enterButton.setOnAction((e) -> {
            String userWord=playscene.guessField.getText();
            if(userWord.length()==4)
                {playscene.displayWord(userWord.toLowerCase(),wordToGuess);
                playscene.count+=1;}
        });

        primary.setScene(StartScene);
        primary.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

内容的提问来源于stack exchange,提问作者Francisco Erramuspe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:30:50