我已经使用NetBeansIDE8.1和JavaFX场景生成器从胶子创建一个基本的用户界面与文本字段。
用户操作路径
我试着学习Java /O (FileReader和FileWriter),但是它的实现与JavaFX和FXML的实现非常不同。
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代码
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控制器代码
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) {
}
}请协助我编写Java代码(我可以使用Source >在Netbeans中自动生成控制器)来实现用户操作路径。谢谢。
发布于 2016-07-12 11:37:23
您可以使用PrintWriter。要检查密码是否相同很容易,只需使用getText()并使用equals。
您创建该文件并将其写入:
PrintWriter writer = new PrintWriter(new FileWriter(file));然后读取每个字段并执行writer.println(directoryName);
不要忘记使用writer.flush();,因为默认情况下它不是自动的。
->甲骨文教程解释得更好。
使用与尝试使用资源
https://stackoverflow.com/questions/38316162
复制相似问题