String 基于Javafx文本的游戏,按钮切换语句

String 基于Javafx文本的游戏,按钮切换语句,string,button,javafx,switch-statement,scenebuilder,String,Button,Javafx,Switch Statement,Scenebuilder,嗨,我正在尝试用新的javafx特性创建一个基于文本的游戏。 我很难让我的按钮指定一个值,然后在switch语句中使用该值。 有人有什么建议吗? 或者,如果有人知道任何关于基于文本的javafx游戏的教程,请告诉我 我认为问题可能在于,当调用包含switch语句的方法时,switch语句将在已分配的值上执行,然后调用结束。 这不是我想要达到的。 以下是我在控制器类中的代码: package sample.view; import java.io.*; import javafx.fxml.F

嗨,我正在尝试用新的javafx特性创建一个基于文本的游戏。 我很难让我的按钮指定一个值,然后在switch语句中使用该值。 有人有什么建议吗? 或者,如果有人知道任何关于基于文本的javafx游戏的教程,请告诉我

我认为问题可能在于,当调用包含switch语句的方法时,switch语句将在已分配的值上执行,然后调用结束。 这不是我想要达到的。 以下是我在控制器类中的代码:

package sample.view;

import java.io.*;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import sample.Main;
import javafx.scene.control.Button;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.lang.System.out;
import javafx.scene.control.*;
import sample.chapter1;

public class gameScreenController {
    public Button button0;
    public Button button1;
    public Button button2;
    public Button button3;
    public TextArea textArea;
    public ImageView image1;
    public ImageView image2;
    public String question = "0";
    public String choice = "";
    public Label label;

    /*
    this method is used when the user clicks button1
     */


    public gameScreenController() {
    }

    @FXML
    public void button0() {
        textArea.setText(setTextArea("ch1Start"));

        button1.setText("what a beautiful poem");
        button2.setText("She know nothing. She got nothing");
        button3.setText("...");
        button0.setVisible(false);
        chapter1();
    }

    @FXML
    public void button1() {
        choice = "c1";
        out.println("c1");



    }

    /*
    this method is used when the user clicks button2
     */
    @FXML
    public void button2() {
        choice = "c2";
        out.println("c2");
    }

    /*
    this method is used when the user clicks button3
     */
    @FXML
    public void button3() {
        choice = "c3";
        out.println("c3");

    }

    @FXML
    public String setTextArea(String fileName) {

        String line;
        String content = null;

        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader buffer = new BufferedReader(fileReader);

            while ((line = buffer.readLine()) != null) {
                out.println(line);
                 content += line + '\n';
            }
            buffer.close();
        } catch (FileNotFoundException ex) {

            out.println("Unable to open file " + fileName + ".");

        } catch (IOException ex) {
            out.println("error reading file " + fileName + ".");
        }
        return content;
    }



    public void chapter1 () {

        switch (question) {
            case "0":
                switch (choice) {
                    case "c1":
                        textArea.setText(setTextArea("hi"));
                        out.println("it worked");
                        question = "1";
                        break;
                    case "c2":
                        out.println("it worked");
                        break;
                    case "c3":
                        out.println("it worked");
                        break;


                } break;
            case "1":
                break;
   } }}
这是我试图复制到javafx中的代码:

 public class ChoiceHandler implements ActionListener
 {
    public void actionPerformed(ActionEvent event)
    {
        // takes in button click -> youtChoice variable
        String yourChoice = event.getActionCommand();

        switch (position)
        {
            case "townGate":
                switch(yourChoice)
                {
                    case "c1": flirt(); break;
                    case "c2": talkGuard(); break;
                    case "c3": leave(); break;
                    case "c4": break;
                }
            case "talkguard":
                switch (yourChoice)
                {
                    case "c1": townGate(); break;

                }
            case "flirt":
                switch (yourChoice)
                {
                    case "c1": townGate(); break;

                }
            case "outside":
                switch (yourChoice)
                {
                    case "c1": townGate(); break;
                    case "c2": break;
                    case "c3": break;
                    case "c4": break;
                }


        }
       }
     }

我不知道当你说“给按钮赋值”时你到底是怎么想的

有多种选择:

  • 因为您已经在代码中编写了它,所以可以使用字符串或整数。单击按钮时,可以更改此变量的值
  • 您可以扩展按钮类,例如:

    public class MyButton extends javafx.scene.control.Button {
    
    private String value;
    
    public MyButton() {
        super();
    }
    
    public MyButton(String text) {
        super(text);
    }
    
    public void setValue(String newValue) {
        value = newValue;
    }
    
    public String getValue() {
        return value;
    }
    }
    
您可以在oracle的在线教程中了解到很多关于javafx的内容:

我没有试图更改按钮的值。我试图更改变量“choice”,以及该变量如何与switch语句交互。