TextArea将不会显示用户输入

时间:2019-02-26 20:48:58

标签: java javafx scenebuilder

TextArea将不会显示任何信息。我的目标是将用户输入的内容从TextField显示到TextArea中。这是我的控制器文件:

package application.controller;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class MainController implements EventHandler<ActionEvent>{


@FXML
TextField starshipname;
TextArea shipInfo = new TextArea();
Button shipEnter;

@Override
public void handle( ActionEvent event) {

    shipInfo.appendText(starshipname.getText());

}

我看过其他与我的问题类似的帖子,但没有一个可以解决正在发生的事情。我尝试了多种操作,例如shipInfo.setText("test"),删除new TextArea();会给我一个NullPointerException错误。最终,我想从控制器调用另一个函数,以根据用户输入在TextArea上显示特定信息。首先,我要确保可以完全在TextArea上显示。这是我的主文件:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {

public static Stage stage;

@Override
public void start(Stage primaryStage) {

     try {
         Parent root = FXMLLoader.load(getClass().getResource("../Main.fxml"));
         primaryStage.setScene(new Scene(root, 800, 800));
         primaryStage.show();

         stage = primaryStage;

     } catch(Exception e) {
         e.printStackTrace();
     }
}

public static void main(String[] args) {
    launch(args);
}
}

这也是我的Main.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="800.0" prefWidth="800.0" 
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="application.controller.MainController">
<children>
  <Label alignment="CENTER" prefHeight="179.0" prefWidth="800.0" 
text="United Federation of Planets" textAlignment="CENTER">
     <font>
        <Font name="Old English Text MT" size="40.0" />
     </font>
  </Label>
  <Label alignment="CENTER" layoutX="35.0" layoutY="50.0" prefHeight="90.0" prefWidth="93.0" text="O">
     <font>
        <Font name="Old English Text MT" size="96.0" />
     </font>
  </Label>
  <Label alignment="CENTER" layoutX="7.0" prefHeight="111.0" prefWidth="93.0" text="O">
     <font>
        <Font name="Old English Text MT" size="96.0" />
     </font></Label>
  <Label layoutX="81.0" layoutY="-6.0" text="O">
     <font>
        <Font name="Old English Text MT" size="96.0" />
     </font>
  </Label>
  <Label layoutX="217.0" layoutY="179.0" text="Enter a starship name to view personnel:">
     <font>
        <Font name="Centaur" size="25.0" />
     </font>
  </Label>
  <TextField fx:id="starshipname" layoutX="257.0" layoutY="223.0" prefHeight="0.0" prefWidth="275.0">
     <font>
        <Font name="Centaur" size="18.0" />
     </font>
  </TextField>
  <Button fx:id="shipEnter" layoutX="360.0" layoutY="278.0" mnemonicParsing="false" onAction="#handle" prefHeight="36.0" prefWidth="82.0" text="Submit">
     <font>
        <Font name="Centaur" size="17.0" />
     </font>
     <effect>
        <DropShadow blurType="TWO_PASS_BOX">
           <input>
              <InnerShadow />
           </input>
        </DropShadow>
     </effect>
  </Button>
  <TextArea fx:id="shipInfo" editable="false" layoutX="54.0" layoutY="330.0" prefHeight="409.0" prefWidth="692.0">
     <font>
        <Font name="Centaur" size="18.0" />
     </font>
  </TextArea>
  </children>
  <opaqueInsets>
  <Insets />
  </opaqueInsets>
 </AnchorPane>

非常感谢您的帮助。

0 个答案:

没有答案