我的名字是Omar,我在javafx中编写了此代码,以便在场景0和场景1之间切换。
引入输入扫描仪激光条形码编号时(6590055780)
它从场景0更改为场景1,一旦进入场景1,如果用户按下按钮,则将更改为场景0。
但是当我编译这段代码时,它给了我以下错误
“线程“ JavaFX Application Thread”中的异常java.lang.illegalArgumentException:网格hgap = 10.0 alignment = CENTER已设置为另一个场景的根“
我不知道为什么会收到此错误,就像setOnKeyPressed不允许我离开该错误一样,当我想从场景0切换到场景1时,出现了此错误消息,请按顺序感谢所有帮助解决这个问题。非常感谢你
这是代码
import javafx.application.Application;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
public class scannerinputfx2 extends Application {
////////////* public class*///////
public static String salida;
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("what's up");
//scene 0
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 300, 275);
TextArea area = new TextArea();
TextField userTextField = new TextField();
//grid.add(userTextField, 0, 0);
//grid.add(area, 0,1);
GridPane.setConstraints(userTextField, 0,0);
GridPane.setConstraints(area,0,1);
grid.getChildren().addAll(area, userTextField);
//scene 1
GridPane grid1 = new GridPane();
grid1.setAlignment(Pos.CENTER);
grid1.setHgap(10);
grid1.setVgap(10);
grid1.setPadding(new Insets(25, 25, 25, 25));
Scene scene1 = new Scene(grid1, 300, 275);
Button btnfinal = new Button("Press button to get back");
GridPane.setConstraints(btnfinal, 0, 0);
grid1.getChildren().addAll(btnfinal);
btnfinal.setOnAction(actionEvent -> {
scene1.setRoot(grid);
});
primaryStage.setScene(scene);
primaryStage.show();
userTextField.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
area.setText( userTextField.getText());
if (userTextField.getText().equals("6590055780"))
{
System.out.println("The number is " + userTextField.getText());
userTextField.setText("");
scene.setRoot(grid1);
}
else
{
System.out.println("The number is not the indicated");
}
}
});
}
////////*Here finish primary stage/////////
public static void main(String[] args) {
launch(args);
}
}