1
在我的javaFX程序中,我想從窗口(名稱和收件人)獲取輸入並在控制檯中顯示輸入(使用System.out.println())。但它從不打印該值。如何更改窗口並從javaFX中的窗口獲取輸入?
而我也想改變窗口,當我按「開始」按鈕,我試圖用一個標誌「我」但它沒有工作,我該如何解決它?請任何幫助,將非常感激。
public class Client extends Application{
String name, sname;
TextField userTextField = new TextField();
TextField fTextField = new TextField();
String recip;
TextFlow textFlow = new TextFlow();
ScrollPane sp = new ScrollPane();
Button button = new Button("Send");
int i=0;
public Parent content() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label userName = new Label("User Name:");
grid.add(userName, 0, 1);
grid.add(userTextField, 1, 1);
Label fName = new Label("Friend Name:");
grid.add(fName, 0, 2);
grid.add(fTextField, 1, 2);
Button button = new Button("Start");
DropShadow shadow = new DropShadow();
button.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
button.setEffect(shadow);
});
button.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
button.setEffect(null);
});
button.setOnAction(e -> grid.getScene().getWindow().hide());
grid.add(button,1,6);
String name;
name = userTextField.getText();
System.out.println(name);
recip = fTextField.getText();
System.out.println(recip);
return grid;
}
public Parent createContent(){
textFlow.setPadding(new Insets(10));
textFlow.setLineSpacing(10);
TextField textField = new TextField();
textField.setPrefSize(300,30);
button.setPrefSize(80,30);
VBox container = new VBox();
VBox box = new VBox();
box.getChildren().addAll(sp,textFlow);
container.setPadding(new Insets(10));
container.getChildren().addAll(box, new HBox(textField, button));
VBox.setVgrow(sp, Priority.ALWAYS);
VBox.setVgrow(textFlow, Priority.ALWAYS);
return container;
}
public void start(Stage stage) throws IOException {
Parent q= content();
Scene scene = new Scene(q,300,400);
stage.setScene(scene);
stage.setTitle("Chit-Chat");
stage.show();
if (i == 1) {
VBox vb = new VBox();
vb.getChildren().addAll(textFlow);
sp.setVmax(440);
sp.setPrefSize(400, 300);
sp.setContent(vb);
sp.vvalueProperty().bind((ObservableValue<? extends Number>) vb.heightProperty());
Parent p = createContent();
Scene scene2 = new Scene(p, 400, 300);
stage.setScene(scene2);
stage.setTitle("Chit-Chat");
stage.show();
}
}
}
public static void main(String[] args) throws IOException {
launch(args);
}
}