2017-02-15 2 views
0
package RockPaperScissors; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 

public class Main extends Application{ 

    String x; 

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

    public void start(Stage primarystage) throws Exception{ 

     Stage window; 
     window = primarystage; 
     window.setTitle("Rock, Paper, Scissor"); 

     Button rockButton = new Button("Rock"); 
     rockButton.setLayoutX(50); 
     rockButton.setLayoutY(50); 
     rockButton.setPrefSize(50,20); 
     rockButton.setOnAction(e -> System.out.println("Rock")); 

     Button paperButton = new Button("Paper"); 
     paperButton.setLayoutX(120); 
     paperButton.setLayoutY(50); 
     paperButton.setPrefSize(50,20); 
     paperButton.setOnAction(e -> System.out.println("Paper")); 

     Button scissorButton = new Button("Scissor"); 
     scissorButton.setLayoutX(190); 
     scissorButton.setLayoutY(50); 
     scissorButton.setPrefSize(60, 20); 
     scissorButton.setOnAction(e -> System.out.println("Scissor")); 

     Label direction = new Label("Pick Rock, Paper, or Scissor:"); 
     direction.setLayoutX(50); 
     direction.setLayoutY(30); 

     Pane pane = new Pane(); 
     pane.getChildren().addAll(rockButton, paperButton, scissorButton, direction); 

     Scene scene = new Scene(pane, 500, 400); 

     window.setScene(scene); 
     window.isFullScreen(); 
     window.show(); 
    } 
} 

変数を変更して、コンピュータで生成されたintと比較できるようにしたいと考えています。私はまた、ユーザーが結果を見ることができるようにラベルを更新する方法を知りたいと思っています。今は、イベントをコンソールにユーザの選択肢を表示させるだけです。私はちょうどその場所の所有者として使用しています。前もって感謝します。イベントを変数xに変更するにはどうすればよいですか?

答えて

0

あなたが例えばプロパティにユーザー入力のための変数を変えることができます:

StringProperty x = new SimpleStringProperty(); 

プロパティが保持している文字列は、このようなボタンイベントに更新することができます。

rockButton.setOnAction(e -> x.set("Rock")); 
paperButton.setOnAction(e -> x.set("Paper")); 
scissorButton.setOnAction(e -> x.set("Scissor")); 

次に、このようなプロパティの変更を聞くことができます:

x.addListener(new ChangeListener<String>(){ 

     @Override 
     public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
      String outcome = ""; 
      //determine outcome depending on newValue.intern(); 
      outcomeLabel.setText(outcome); 
     }   

}); 

newValue.intern();は更新された文字列を返します。 setText()メソッドを使用して、結果を表示するラベルを更新することができます。

関連する問題