我是Java的新手,我知道这是一个非常基本的问题,但是我不断遇到这些运行时错误,我创建了一个方法并将该方法添加到场景构建器的事件处理部分中,但是它不起作用,请看看我的代码,看看有什么问题。(编辑)
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import sun.applet.Main;
import java.io.IOException;
public class registration extends Application {
// Button button;
@Override
public void start(Stage primaryStage)throws Exception{
primaryStage.setTitle("Event Handling");
FXMLLoader loader = new
FXMLLoader(registration.class.getResource("jfs.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
@FXML
private Button submit;
@FXML
private TextField text;
@FXML
private void initialize() {
// Handle Button event.
submit.setOnAction((event) -> {
text.appendText("somthn");
});
public static void main(String[] args) {
launch(args);
}
}
fxml部分-我已将方法初始化添加到按钮onAction上,但运行该功能后该功能不起作用。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane nodeOrientation="LEFT_TO_RIGHT" prefHeight="530.0" prefWidth="832.0" style="-fx-background-color: pink;"
xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="registration">
<children>
<VBox layoutX="347.0" layoutY="71.0" prefHeight="359.0" prefWidth="433.0" style="-fx-background-color: AliceBlue;">
<children>
<TextField id="name" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="Username"
style="-fx-background-color: white; -fx-background-radius: 0px;" VBox.vgrow="SOMETIMES">
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets left="29.0" right="10.0" top="30.0" />
</VBox.margin>
<padding>
<Insets top="10.0" />
</padding>
</TextField>
<TextField id="e-mail" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="e-mail" VBox.vgrow="SOMETIMES">
<VBox.margin>
<Insets left="29.0" right="10.0" top="30.0" />
</VBox.margin>
<padding>
<Insets top="10.0" />
</padding>
</TextField>
<PasswordField id="pwd" alignment="TOP_CENTER" maxWidth="-Infinity" prefWidth="200.0" promptText="new password">
<VBox.margin>
<Insets left="29.0" right="10.0" top="30.0" />
</VBox.margin>
</PasswordField>
<PasswordField id="pwd" alignment="TOP_CENTER" layoutX="39.0" layoutY="164.0" maxWidth="-Infinity" prefWidth="200.0" promptText="confirm password">
<VBox.margin>
<Insets left="29.0" right="10.0" top="30.0" />
</VBox.margin>
</PasswordField>
<Button mnemonicParsing="false" prefHeight="38.0" prefWidth="75.0" text="Register">
<VBox.margin>
<Insets left="160.0" top="70.0" />
</VBox.margin>
</Button>
</children>
<padding>
<Insets right="10.0" top="10.0" />
</padding></VBox>
<Label layoutX="353.0" layoutY="34.0" text="Please fill in the following details to register">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
<VBox id="labelo" layoutX="14.0" layoutY="72.0" prefHeight="359.0" prefWidth="320.0" style="-fx-background-color: AliceBlue;">
<children>
<Label text="Welcome to JFS ">
<font>
<Font name="System Bold Italic" size="15.0" />
</font>
<VBox.margin>
<Insets left="50.0" top="30.0" />
</VBox.margin>
</Label>
<Label />
<TextField id="mail" alignment="TOP_CENTER" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="200.0" promptText="E-mail">
<VBox.margin>
<Insets left="30.0" top="30.0" />
</VBox.margin>
</TextField>
<Label text="Your query:">
<VBox.margin>
<Insets left="30.0" top="20.0" />
</VBox.margin>
</Label>
<TextArea id="query" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="91.0" prefWidth="213.0">
<padding>
<Insets top="5.0" />
</padding>
<VBox.margin>
<Insets left="30.0" top="10.0" />
</VBox.margin>
</TextArea>
<CheckBox id="check" mnemonicParsing="false" text="Receive newsletters">
<VBox.margin>
<Insets left="120.0" top="20.0" />
</VBox.margin>
</CheckBox>
<Label id="lab" prefHeight="17.0" prefWidth="104.0">
<VBox.margin>
<Insets left="200.0" right="40.0" top="90.0" />
</VBox.margin></Label>
<TextField id="text" fx:id="text" visible="false" />
<Button id="submit" fx:id="submit" mnemonicParsing="false" onAction="#initialize" text="Submit">
<VBox.margin>
<Insets left="100.0" top="20.0" />
</VBox.margin>
</Button>
</children>
</VBox>
</children>
<cursor>
<Cursor fx:constant="MOVE" />
</cursor>
</AnchorPane>