2017-07-15 72 views
0

您好我正在尝试使用新的javafx功能创建基于文本的游戏。 我很难让我的按钮分配一个值,然后在switch语句中使用该值。 有人有什么建议吗? 或者如果有人知道任何有关javafx基于文本的游戏的教程让我知道。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; 
       } 


     } 
     } 
    } 

回答

0

我不知道你exacly认为当你说“分配一个按钮的值“。

可以有多种选择:

  • 因为你已经在代码中写的,你可以使用一个字符串或整数。当按钮被点击时,您可以更改此变量的值
  • 您可以扩展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; 
    } 
    } 
    

你可以学习在网上的教程很多关于JavaFX从Oracle: Click here

+0

我并不想改变按钮的值。我正在尝试更改变量“choice”以及该变量如何与我的switch语句交互。 –