[英]Basic File I/O JavaFX FXML Application built with Scene Builder and Netbeans IDE
I have utilized Netbeans IDE 8.1 and JavaFX Scene Builder from Gluon to create a basic user interface with text fields. 我利用了来自Gluon的Netbeans IDE 8.1和JavaFX Scene Builder来创建带有文本字段的基本用户界面。
I tried learning about Java I/O (FileReader and FileWriter) but it's implementation is quite different from that of JavaFX and FXML. 我尝试学习Java I / O(FileReader和FileWriter),但是它的实现与JavaFX和FXML的实现完全不同。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="292.0" prefWidth="478.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.UIController">
<children>
<PasswordField fx:id="Passwd" layoutX="208.0" layoutY="52.0" />
<TextField fx:id="PDB" layoutX="208.0" layoutY="94.0" />
<TextField fx:id="D4" layoutX="208.0" layoutY="129.0" />
<TextField fx:id="D2" layoutX="208.0" layoutY="169.0" />
<Button fx:id="Enter" layoutX="208.0" layoutY="215.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Enter" />
<Text layoutX="48.0" layoutY="112.0" strokeType="OUTSIDE" strokeWidth="0.0" text="PDB File Directory" wrappingWidth="180.3525390625">
<font>
<Font name="Lucida Sans Regular" size="13.0" />
</font>
</Text>
<Text layoutX="48.0" layoutY="147.0" strokeType="OUTSIDE" strokeWidth="0.0" text="4D NOESY Peak List" wrappingWidth="155.00000256299973">
<font>
<Font name="Lucida Sans Regular" size="13.0" />
</font>
</Text>
<Text layoutX="48.0" layoutY="187.0" strokeType="OUTSIDE" strokeWidth="0.0" text="2D HSQC Peak List" wrappingWidth="142.3525390625">
<font>
<Font name="Lucida Sans Regular" size="13.0" />
</font>
</Text>
<Text layoutX="48.0" layoutY="70.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password" wrappingWidth="180.3525390625">
<font>
<Font name="Lucida Sans Regular" size="13.0" />
</font>
</Text>
</children>
</AnchorPane>
package ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class UI extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("UI.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package ui;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
public class UIController implements Initializable {
private Label label;
@FXML
private PasswordField Passwd;
@FXML
private TextField PDB;
@FXML
private TextField D4;
@FXML
private TextField D2;
@FXML
private Button Enter;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Please assist me in the Java code (I can auto-generate the controller using Source > Make Controller in Netbeans) to implement the User Action Pathway. 请在Java代码方面为我提供帮助(我可以使用Netbeans中的Source> Make Controller自动生成控制器)以实现User Action Pathway。 Thanks.
谢谢。
You can use PrintWriter . 您可以使用PrintWriter 。 To check if the password is the same is quite easy just getText() and use
equals
. 要检查密码是否相同,很简单,只需使用getText()并使用
equals
。
You create the File and you write to it: 您创建文件并写入文件:
PrintWriter writer = new PrintWriter(new FileWriter(file));
Then you read each field and do writer.println(directoryName);
然后,您阅读每个字段并执行
writer.println(directoryName);
Don't forget to use writer.flush();
不要忘记使用
writer.flush();
cause it not auto by default. 导致默认情况下它不是自动的。
-->Oracle tutorial explains it better. -> Oracle 教程对此进行了更好的解释。
Use and try with resources 使用并尝试使用资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.