2017-05-07 3 views
1

私はユーザーからの入力を受け取り、他のクラスのデータを計算するjavafxを使ってGUIアプリケーションを作成しています。私はオブジェクトを作ってアクセスしようとしましたが、動作させることはできません。私はグーグルで、抽象メソッドを使うようなことをたくさん試しましたが、それを動作させることはできません。あなたの例からわかるように、あなたの変数(ID、パーティー、、私はコントローラクラスまたは他のクラス他のクラスのGUI(ラムダ式)で使用されている変数にアクセスできません

  Submit.setOnAction((ActionEvent e) -> { 
       primaryStage.setScene(scene1); 
     String ID = VoterIdtext.getText(); 
     String Party=VoteTotext.getText(); 
     Integer VoterAge=Integer.parseInt(Agetext.getText()); 

}

答えて

1

から(ID、パーティー、VoterAgeなど)の変数にアクセスする方法を教えてくださいVoterAge)がメソッドに記述され、ローカル変数です。 他のクラスで使用する場合は、他のクラスで宣言する必要があります。例えば:

public static String ID = ""; 
public static String Party; 
public static int VoterAge = null; 
... 

    Submit.setOnAction((ActionEvent e) -> { 
     primaryStage.setScene(scene1); 
     ID = VoterIdtext.getText(); 
     Party=VoteTotext.getText(); 
     VoterAge=Integer.parseInt(Agetext.getText()); 
} 
1
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ButtonBar; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class SampleApp extends Application { 

public class Info { 

    private String name; 
    private int age; 

    public String getName() { 
     return name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setAge(String age) { 
     this.age = Integer.parseInt(age); 
    } 
} 

private TextField ageField; 
private TextField nameField; 
private Info info; 
private Button printButton; 
private Button submitButton; 

@Override public void start(Stage primaryStage) throws Exception { 
    VBox root = new VBox(); 
    primaryStage.setScene(new Scene(root)); 
    createNameField(root); 
    createAgeField(root); 
    submitButton = new Button("Submit"); 
    submitButton.setOnAction((e) -> setInfo()); 
    ButtonBar e = new ButtonBar(); 
    printButton = new Button("Print Info"); 
    printButton.setDisable(true); 
    printButton.setOnAction((eve) -> printInfo()); 
    e.getButtons().addAll(submitButton, printButton); 
    root.getChildren().add(e); 
    primaryStage.show(); 
} 

private void printInfo() { 
    System.out.println("Name: " + info.getName()); 
    System.out.println("Age: " + info.getAge()); 
} 

private void setInfo() { 
    info = new Info(); 
    info.setName(nameField.getText()); 
    info.setAge(ageField.getText()); 
    printButton.setDisable(false); 
} 

private void createAgeField(VBox root) { 
    HBox ageBox = new HBox(10); 
    root.getChildren().add(ageBox); 
    ageField = new TextField(); 
    ageBox.getChildren().addAll(new Label("Age: "), ageField); 
} 

private void createNameField(VBox root) { 
    HBox ageBox = new HBox(); 
    root.getChildren().add(ageBox); 
    nameField = new TextField(); 
    ageBox.getChildren().addAll(new Label("Name: "), nameField); 
} 

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

}

関連する問題