早/晚/下午
加载弹出窗口后加载方法有些困难。我已经让代码工作,但只有在你点击一个按钮后才能使用。我是JavaFX的新手,这是我用它写的第一件事。
我发布了signUp控制器和PopUp FXML,问题是当您输入用户名后单击Enter时,弹出框应显示用户名和随机4位数代码。但是我无法将我的代码更改为userShown并将passShown更改为您输入的用户名和随机数字。虽然如果我通过调用setLabels()指定一个按钮来执行此操作,它将会改变。
“守则”是半混乱的,因为我一直在试图找到使其发挥作用的方法,因此它们可能是不需要的。
由于我试图让这个工作失败的尝试很多,他们也可能有很多进口商品。我知道这不是您存储用户名/代码的方式,但它更像是一个迷你系统,可以阻止家人使用您的帐户。它不仅仅是一种学习JavaFX的测试。
signUp Controller -
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import java.util.ResourceBundle;
import org.omg.CORBA.INITIALIZE;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class signUpController {
@FXML
private TextField userName;
@FXML
private Label userShown;
@FXML
private Label passShown;
Writer write = new Writer();
static public String uName;
static public int uCode;
int number = (int)( Math.random() * 9999);
static boolean done = false;
public void homeButton(ActionEvent event) throws IOException{
Parent signUpParent = FXMLLoader.load(getClass().getResource("Splash.fxml"));
Scene signUpScene = new Scene(signUpParent);
//This line gets the Stage information
// Make it a node type object -> get source -> get the scene -> get the window -> window = window(stage)
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(signUpScene);
window.centerOnScreen();
window.show();
window.setResizable(false);
}
// Passing Login Details to Writer to Write to Users.txt
public void signUpButton(ActionEvent event) throws IOException{
// To make sure number is higher than 999
if (number < 1000) {
number += 1000;
}
uCode = number;
uName = userName.getText();
// Write Username/Code to Txt Doc
System.out.println(userName.getText() + number);
write.writeStart(userName.getText(),number);
loginPopUp();
setLabels();
if (done == true) {
homeButton(event);
done = false;
}
// Reset Number
number = (int)( Math.random() * 9999);
}
// Giving Login Details
public void loginPopUp()throws IOException {
Stage window2 = new Stage(StageStyle.UTILITY);
Parent loginPopUpParent = FXMLLoader.load(getClass().getResource("loginPopUp.fxml"));
Scene loginPopUpScene = new Scene(loginPopUpParent);
window2.initModality(Modality.APPLICATION_MODAL);
window2.setScene(loginPopUpScene);
window2.setResizable(false);
window2.centerOnScreen();
window2.show();
done = true;
}
private void setLabels() {
userShown.setText(uName);
passShown.setText(Integer.toString(uCode));
}
public void close(ActionEvent event){
Stage window2 = (Stage)((Node)event.getSource()).getScene().getWindow();
window2.close();
}
}
注册FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="signUpController">
<top>
<ImageView fitHeight="96.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="@Hangman.png" />
</image>
<BorderPane.margin>
<Insets left="20.0" top="50.0" />
</BorderPane.margin>
</ImageView>
</top>
<center>
<VBox alignment="CENTER" spacing="15.0">
<children>
<Label text="Please Enter Your Chosen Username">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
<TextField fx:id="userName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="192.0" promptText="Username" />
<Button mnemonicParsing="false" onAction="#signUpButton" text="Enter" />
<Button layoutX="288.0" layoutY="171.0" mnemonicParsing="false" onAction="#homeButton" text="Back" />
</children>
<BorderPane.margin>
<Insets top="20.0" />
</BorderPane.margin>
</VBox>
</center>
<bottom>
<Pane BorderPane.alignment="CENTER">
<children>
<Pane layoutX="279.0" layoutY="8.0" />
</children>
</Pane>
</bottom>
</BorderPane>
登录弹出FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<VBox alignment="CENTER_LEFT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="267.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="signUpController">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Username:" wrappingWidth="122.45364984856826">
<font>
<Font size="26.0" />
</font>
</Text>
<Label fx:id="userShown" text="Name Here">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Password Code:" wrappingWidth="183.45364984856826">
<font>
<Font size="26.0" />
</font>
</Text>
<Label fx:id="passShown" text="Code Here">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#close" prefHeight="25.0" prefWidth="53.0" text="Close">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
<padding>
<Insets bottom="50.0" left="15.0" top="50.0" />
</padding>
</VBox>
任何帮助都会非常感激,如果有任何指导或教程非常适合教授javaFX,请指点我正确的方向。
非常感谢