简体   繁体   English

使用Scene Builder和Netbeans IDE构建的基本文件I / O JavaFX FXML应用程序

[英]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来创建带有文本字段的基本用户界面。

User Action Pathway 用户操作途径

  1. User types in Password. 用户输入密码。
  2. User types in the three directories in the three text fields (1. PDB..., 2. 4D..., 3. 2D...). 用户在三个文本字段中输入三个目录(1. PDB ...,2。4D ...,3。2D ...)。
  3. User clicks Enter and JavaFX checks if the password is "passwd_1234" and if it is, JavaFX outputs the three directories on three separate lines in a text file (to be used by other non-Java code as inputs). 用户单击Enter,JavaFX将检查密码是否为“ passwd_1234”,如果是,JavaFX将在文本文件的三行中输出三个目录(供其他非Java代码用作输入)。

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的实现完全不同。

FXML Code 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>

Java Code Java代码

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);
    } 
}

Java Controller Code Java控制器代码

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.

相关问题 场景生成器 JavaFX 打开 FXML 文件时出错 - Scene Builder JavaFX Error opening FXML file NetBeans Standard FXML应用程序的JavaFX错误-.fxml文件的Nullpointer - JavaFX Error with Netbeans Standard FXML application - Nullpointer for .fxml file 有没有一种方法可以创建没有Netbeans,Eclipse或场景构建器的FXML / Javafx桌面应用程序? - Is there a way to create a FXML/Javafx desktop app without Netbeans, eclipse or scene builder? JavaFX和在场景生成器中创建的FXML的使用 - JavaFX and the use of FXML created in scene builder 我的问题是,当我尝试打开其fxml文件时,我在netbeans中打开了一个github项目。场景生成器说:无法打开login.fxml文件 - my question is about i opened a github project in netbeans when i tried to open its fxml file scene builder says: couldnt open login.fxml file 如何在使用FXML构建的JavaFx中重新启动应用程序? - How do i restart an application in JavaFx, built with FXML? 通过fxml或JavaFX中的Scene生成器在场景上动态添加窗口小部件 - Dynamically add widgets on a scene through fxml or Scene builder in JavaFX Netbeans中的JavaFX场景构建器2.0错误 - JavaFX scene builder 2.0 error in Netbeans Scene Builder无法打开FXML文件 - Scene Builder can't open a FXML file Scene Builder 无法打开带有注释的 FXML 文件 - Scene Builder does not open FXML file with comments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM