“学生”按钮将您带到另一个必须填写文本字段的场景。我希望将在数组中输入的数据填充到表的“名称”列中。但是,在单击“提交”时,该表仍为空白。我尝试了一些替代方法,但似乎无济于事。任何帮助将不胜感激。谢谢。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.*;
import java.util.List;
import java.util.ArrayList;
public class Main extends Application{
public static Scene sceneMain,sceneStudent;
public static Stage pStage;
public String nameData[]=new String[20];
public int i=0;
public TableView<Product> table;
Button student=new Button("Student");
Button backStudent=new Button("Go Back");
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Della Amusement Park");
pStage = primaryStage;
VBox layout = new VBox(10);
//Opens selected scene on clicking the respective option
student.setOnAction( e -> {
primaryStage.setScene(sceneStudent);
});
backStudent.setOnAction(e -> {
primaryStage.setScene(sceneMain);
});
StudentScene ss=new StudentScene();
sceneStudent=new Scene(ss.sceneView(),400,300);
//Name column
TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
table = new TableView<>();
table.setItems(getProduct());
table.getColumns().addAll(nameColumn);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(table,student);
layout.setAlignment(Pos.CENTER);
sceneMain = new Scene(layout, 500, 350);
primaryStage.setScene(sceneMain);
primaryStage.show();
}
public ObservableList<Product> getProduct(){
ObservableList<Product> products = FXCollections.observableArrayList();
for(String s:nameData)
{
products.add(new Product(s));
}
return products;
}
public static void main(String[] args) {
launch(args);
}
}
Blockquote
import javafx.beans.property.SimpleStringProperty;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import java.util.*;
import java.time.LocalDate;
public class StudentScene extends Main{
public GridPane sceneView()
{
GridPane grid= new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(8);
grid.setHgap(10);
Button submit=new Button("Submit");
//Name Label - constrains use (child, column, row)
Label nameStudent=new Label("Name:");
GridPane.setConstraints(nameStudent,0,0);
//Name Input
TextField nameInput = new TextField("");
nameInput.setPromptText("Name");
GridPane.setConstraints(nameInput, 1, 0);
GridPane.setConstraints(submit, 1, 8);
GridPane.setConstraints(backStudent, 0,8);
submit.setOnAction(e->{
storeValue(nameInput.getText());
nameInput.clear();
pStage.setScene(sceneMain);
});
grid.getChildren().addAll(nameStudent, nameInput,submit,backStudent);
return grid;
}
public void storeValue(String name)
{
nameData[i]=name;
i++;
}
}
Blockquote
public class Product {
private String name;
public Product(){
this.name = "";
}
public Product(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}