javafx - JavaFX Layout complie time
问题描述
I'm trying to achieve this but I'm running into some problems. I got the rough skeleton, but for example when I try to add the output TextArea to my Vbox container, I get an error.
The error is: The method addAll(int, Collection<? extends Node>) in the type List<Node> is not applicable for the arguments (HBox, HBox, HBox, Button, TextArea)
EDIT: I had the wrong import for the TextArea, I had the awt instead of the javafx.scene.control.TextArea;
GridPane g1 = new GridPane();
HBox firstRow = new HBox();
firstRow.setPadding(new Insets(10));
Label name = new Label("Name: ");
TextField nameInput = new TextField();
g1.add(name, 0, 0);
g1.add(nameInput, 1, 0);
firstRow.getChildren().addAll(g1);
GridPane g2 = new GridPane();
HBox secondRow = new HBox();
secondRow.setPadding(new Insets(10));
Label city = new Label("City: ");
TextField cityInput = new TextField();
g2.add(city, 0, 0);
g2.add(cityInput, 1, 0);
secondRow.getChildren().addAll(g2);
HBox thirdRow = new HBox();
thirdRow.setSpacing(20);
thirdRow.setPadding(new Insets(5));
RadioButton radioName = new RadioButton("Name");
RadioButton radioCity = new RadioButton("City");
RadioButton radioZip = new RadioButton("Zip");
ToggleGroup group = new ToggleGroup();
radioName.setToggleGroup(group);
radioCity.setToggleGroup(group);
radioZip.setToggleGroup(group);
thirdRow.getChildren().addAll(radioName, radioCity, radioZip);
Button search = new Button("Search");
HBox fifthRow = new HBox();
TextArea output = new TextArea();
VBox container = new VBox();
container.getChildren().addAll(firstRow, secondRow, thirdRow, search);
解决方案
Your question wasn't too clear initially because you did not include the error you saw. The problem is because you have imported the wrong TextArea
, which import statements are also not included in your question.
You need to change import java.awt.TextArea;
into import javafx.scene.control.TextArea;
. The former is a control for AWT, while the latter is the control for JavaFX.
推荐阅读
- java - 如何编写一个方法(分解),将字符串作为输入并返回在java中组成字符串的字符数组?
- drop-down-menu - 如何让下拉菜单切换工作
- cmd - 在 proc 0 上出现错误:无法在.crack 中打开输入脚本:没有这样的文件或目录(src/lammps.cpp.494)
- python - 是否可以使用 Python 将所有域托管在网络服务器上?
- python - 如果未单击第一个图形,则不会加载第二个图形(Plotly Dash)
- javascript - 为什么我的 parseFloat 只允许将 1.00 到 999.00 的条目发回服务器
- python - 为什么每当我尝试将图像添加到我的 django 项目时都会出错?
- php - 无法访问值抛出雄辩的关系
- c++ - cpp 文件中的 Visual Studio 2019 JUCE C++ 字段“未定义”
- docker - github操作将文件挂载为容器中的卷