【发布时间】:2015-09-08 12:18:45
【问题描述】:
我已经制作了一个运行良好的应用程序,但我不想添加功能,为此我需要在更改组合框的值时添加一些 TextFields 和 Label。 这是我的主要代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;
public class maincombo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/combobox.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Application");
stage.setScene(scene);
stage.show();
}
}
我的控制器:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
public class comboboxcontroller implements Initializable {
@FXML
private ComboBox<Integer>methode;
ObservableList<Integer>nombre = FXCollections.observableArrayList();
@FXML
private void metho (ActionEvent event){
switch (methode.getValue()) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
default :
break;
}
}
public comboboxcontroller (){
}
public void initialize(URL url,ResourceBundle rb){
nombre.add(new Integer(0));
nombre.add(new Integer(1));
nombre.add(new Integer(2));
nombre.add(new Integer(3));
nombre.add(new Integer(4));
nombre.add(new Integer(5));
methode.setItems(nombre);
}
}
我的 fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="150.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="comboboxcontroller">
<children>
<GridPane layoutX="-180.0" layoutY="-205.0" prefHeight="0.0" prefWidth="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ComboBox fx:id="methode" prefWidth="150.0" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" right="5.0" />
</GridPane.margin>
</ComboBox>
<Label text="MyComboBox" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
</children>
</AnchorPane>
我尝试用 switch 语句做一些事情,但没有成功。我想要的是当用户选择值 4 时,我希望它添加 4 个标签和文本字段,但如果他改变主意并选择值 2,它应该显示 2 个标签和文本字段。
编辑:我试图把我所有的 5 个文本字段和标签,并尝试用这一行隐藏它们:
MyTextField.setVisible(false);
但一切仍然可见。所以我开始认为动态添加它们或者使用弹出窗口可能更容易。这也是我在这里寻求帮助的原因。
【问题讨论】:
-
是的,但是当我不放任何代码时,每个人都在要求它,而当我放我的代码时,没有人想要它!我不知道我可以从该代码中删除什么,因为我需要让自己清楚我的代码结构。它还展示了它是如何制作的,并且可能可以帮助我添加我想要的内容并最终改进它。但是,如果您在这里看到任何无用的内容,请告诉我,我会更改它!
-
这里就完成了。拿走我所有的代码,你就可以像我一样执行它!我怎么能向你证明我不知道该怎么做呢?因为这实际上是我的问题!我没有任何异常或错误。我只是不知道。所有这些都与问题有关,因为当我更改组合框上的选择时,我需要添加 Textfield 和 Label !只是觉得它是最小的(即使它很多我不知道如何减少它或者它不会是可执行的),完整的(不知道你还需要什么),可验证的(你可以看到什么都没有发生时更改我的组合框的值
-
那个新的呢?
-
我再次编辑主题
标签: java javafx combobox textfield fxml