【问题标题】:Javafx Text Based Game, Button-switch statementJavafx 基于文本的游戏,按钮开关语句
【发布时间】:2017-07-15 03:28:58
【问题描述】:

您好,我正在尝试使用新的 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;
                }


        }
       }
     }

【问题讨论】:

    标签: string button javafx switch-statement scenebuilder


    【解决方案1】:

    我不知道您说“为按钮分配值”时的确切想法。

    有多种选择:

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

      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 的知识: Click here

    【讨论】:

    • 我不想改变按钮的值。我正在尝试更改变量“选择”以及该变量如何与我的 switch 语句交互。
    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2015-09-10
    相关资源
    最近更新 更多