Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaFX中动态创建场景?_Java_User Interface_Javafx - Fatal编程技术网

如何在JavaFX中动态创建场景?

如何在JavaFX中动态创建场景?,java,user-interface,javafx,Java,User Interface,Javafx,因此,我在JavaFX中构建了一个TableView,如图(People表)所示,其中包含一个姓名列表。对于最后一个程序,信息可以从数据库导入,因此我们事先不知道人数 我的问题是如何为每个人动态创建一个场景,这样当单击“详细信息”按钮时,它将切换到一个唯一的场景,以便我随后可以添加关于该特定人的信息(电话号码、地址等) 非常感谢。以下代码供参考: import javafx.application.Application; import javafx.beans.property.*; impo

因此,我在JavaFX中构建了一个TableView,如图(People表)所示,其中包含一个姓名列表。对于最后一个程序,信息可以从数据库导入,因此我们事先不知道人数

我的问题是如何为每个人动态创建一个场景,这样当单击“详细信息”按钮时,它将切换到一个唯一的场景,以便我随后可以添加关于该特定人的信息(电话号码、地址等)

非常感谢。以下代码供参考:

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.util.Callback;

public class cutExample extends Application {
  public static void main(String[] args) { launch(args); }


  @Override public void start(final Stage stage) {
    stage.setTitle("People");

    // create a table.
    final TableView<Person> table = new TableView<>(
      FXCollections.observableArrayList(
        new Person("Jacob", "Smith"),
        new Person("Isabella", "Johnson"),
        new Person("Ethan", "Williams"),
        new Person("Emma", "Jones"),
        new Person("Michael", "Brown")
      )
    );

    // define the table columns.
    TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
    firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
    TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
    lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
    TableColumn<Person, Boolean> actionCol = new TableColumn<>("Action");
    actionCol.setSortable(false);


    // create a cell value factory with a details button for each row in the table.
    actionCol.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
      @Override public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> personBooleanTableColumn) {
        return new AddPersonCell(stage, table);
      }
    });

    table.getColumns().setAll(firstNameCol, lastNameCol, actionCol);
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

    stage.setScene(new Scene(table));
    stage.show();
  }

  /** A table cell containing a button for details on each person. */
  private class AddPersonCell extends TableCell<Person, Boolean> {
    // a button for a specific person's details
    final Button details       = new Button("Details");
    // pads and centers the button in the cell.
    final StackPane paddedButton = new StackPane();


    /**
     * AddPersonCell constructor
     * @param stage the stage in which the table is placed.
     * @param table the table to which a new person can be added.
     */
    AddPersonCell(final Stage stage, final TableView table) {
      paddedButton.setPadding(new Insets(3));
      paddedButton.getChildren().add(details);


    }

    /** places an details button in the row only if the row is not empty. */
    @Override protected void updateItem(Boolean item, boolean empty) {
      super.updateItem(item, empty);
      if (!empty) {
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(paddedButton);
      } else {
        setGraphic(null);
      }
    }
  }


}
导入javafx.application.application;
导入javafx.beans.property.*;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.event.*;
导入javafx.geometry.*;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.image.image;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.*;
导入javafx.stage.*;
导入javafx.util.Callback;
公共类示例扩展了应用程序{
公共静态void main(字符串[]args){launch(args);}
@覆盖公共无效开始(最后阶段){
舞台。片名(“人物”);
//创建一个表。
最终表视图表=新表视图(
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”),
新人(“伊莎贝拉”、“约翰逊”),
新人(“伊桑”、“威廉姆斯”),
新人(“艾玛”、“琼斯”),
新人(“迈克尔”、“布朗”)
)
);
//定义表列。
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn actionCol=新的TableColumn(“操作”);
actionCol.setSortable(假);
//为表中的每一行创建带有详细信息按钮的单元格值工厂。
actionCol.setCellFactory(新回调(){
@重写公共TableCell调用(TableColumn personBooleanTableColumn){
返回新的AddPersonCell(stage,table);
}
});
table.getColumns().setAll(firstNameCol、lastNameCol、actionCol);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_策略);
舞台场景(新场景(表格));
stage.show();
}
/**包含按钮的表格单元格,用于显示每个人的详细信息*/
私有类AddPersonCell扩展了TableCell{
//用于显示特定人员详细信息的按钮
最终按钮详细信息=新按钮(“详细信息”);
//将按钮放置在单元格中并居中。
最终StackPane paddedButton=新StackPane();
/**
*AddPersonCell构造函数
*@param stage放置表格的阶段。
*@param table可添加新人员的表。
*/
AddPersonCell(最终阶段,最终表格视图表格){
paddedButton.setPadding(新插图(3));
paddedButton.getChildren().add(详细信息);
}
/**仅当行不为空时,才会在行中放置详细信息按钮*/
@覆盖受保护的void updateItem(布尔项,布尔空){
super.updateItem(项,空);
如果(!空){
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
设置图形(填充按钮);
}否则{
设置图形(空);
}
}
}
}

关于场景,你的确切意思是什么?例如,你的意思是弹出一个新窗口吗?不,我想保持相同的舞台(窗口),但切换场景。场景只是窗口中的内容。但是在这种情况下,我想如果你能提供一个解决方案来创建一个新的弹出窗口,它将很好地回答这个问题,因为我可以为场景配置它。你在哪一个特定的部分卡住了?您已经在窗口中设置了一个场景,因此您可能知道如何在窗口中设置另一个场景。问题是“按下按钮时如何执行某些代码?”还是您被困在其他地方?(如果是,在哪里?)我知道如何手动声明每个场景,但在本例中,我们不知道将填充此列表的人数。所以我想知道如何设置程序,以便为每个人动态创建场景。