2017-05-07 59 views
1

我正在使用javafx製作一個gui應用程序,該應用程序從用戶獲取輸入,然後計算其他類中的數據。我試過製作一個對象並訪問它,但無法使它工作。我搜索了很多東西,比如使用抽象方法,但無法使其工作。請告訴我如何從控制器類或任何其它類我無法從其他類訪問GUI(lambda表達式)中使用的變量

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

}

回答

1

如可以從你的例子可以看出,您的變量(ID,黨訪問的變量(如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); 
} 

}

相關問題