Javafx基于文本的游戏,按钮开关语句

时间:2017-07-15 03:28:59

标签: string button javafx switch-statement scenebuilder

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


        }
       }
     }

1 个答案:

答案 0 :(得分:0)

当您说'为按钮分配值'时,我不知道您的想法。

有多种选择:

  • 正如您已在代码中编写的那样,您可以使用String或Integer。单击按钮时更改此变量的值
  • 您可以扩展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