Login JavaFX-将登录窗口插入我的主函数
我对JavaFX非常陌生,本周开始我的知识完全基于学习语法的小教程和示例项目。我已经创建了一个简单的股票信息表,您可以在其中添加和删除信息,并希望实现该程序的登录窗口。我创建了登录窗口,但不确定如何在主功能中正确实现它 Main.java 这包含我的股票应用程序代码Login JavaFX-将登录窗口插入我的主函数,login,javafx,Login,Javafx,我对JavaFX非常陌生,本周开始我的知识完全基于学习语法的小教程和示例项目。我已经创建了一个简单的股票信息表,您可以在其中添加和删除信息,并希望实现该程序的登录窗口。我创建了登录窗口,但不确定如何在主功能中正确实现它 Main.java 这包含我的股票应用程序代码 package sample; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collec
package sample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
TableView<Stock> table;
TextField symbolInput, nameInput, openingPriceInput, closingPriceInput, changeInPriceInput;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Stock Application");
//Symbol column
TableColumn<Stock, String> symbolColumn = new TableColumn<>("Symbol");
symbolColumn.setMinWidth(100);
symbolColumn.setCellValueFactory(new PropertyValueFactory<>("symbol"));
//Name column
TableColumn<Stock, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(100);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//Opening Price column
TableColumn<Stock, Double> openingPriceColumn = new TableColumn<>("Opening Price");
openingPriceColumn.setMinWidth(100);
openingPriceColumn.setCellValueFactory(new PropertyValueFactory<>("openingPrice"));
//Closing Price column
TableColumn<Stock, Double> closingPriceColumn = new TableColumn<>("Closing Price");
closingPriceColumn.setMinWidth(100);
closingPriceColumn.setCellValueFactory(new PropertyValueFactory<>("closingPrice"));
//Change in Price column
TableColumn<Stock, Double> changeInPriceColumn = new TableColumn<>("Change in Price");
changeInPriceColumn.setMinWidth(100);
changeInPriceColumn.setCellValueFactory(new PropertyValueFactory<>("changeInPrice"));
//Symbol input
symbolInput = new TextField();
symbolInput.setPromptText("Symbol");
symbolInput.setMinWidth(100);
//Name input
nameInput = new TextField();
nameInput.setPromptText("Name");
//Opening Price input
openingPriceInput = new TextField();
openingPriceInput.setPromptText("Opening Price");
//Closing Price input
closingPriceInput = new TextField();
closingPriceInput.setPromptText("Closing Price");
//Change in Price Input
changeInPriceInput = new TextField();
closingPriceInput.setPromptText("Change in Price");
//Button
Button addButton = new Button("Add");
addButton.setOnAction(e -> addButtonClicked());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());
HBox hBox = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.getChildren().addAll(symbolInput, nameInput, openingPriceInput, closingPriceInput, changeInPriceInput, addButton, deleteButton);
table = new TableView<>();
table.setItems(getStock());
table.getColumns().addAll(symbolColumn, nameColumn, openingPriceColumn, closingPriceColumn, changeInPriceColumn);
VBox vBox = new VBox();
vBox.getChildren().addAll(table, hBox);
Scene scene = new Scene(vBox);
window.setScene(scene);
window.show();
}
//Add button clicked
public void addButtonClicked(){
Stock Stock = new Stock();
Stock.setSymbol(symbolInput.getText());
Stock.setName(nameInput.getText());
Stock.setOpeningPrice(Double.parseDouble(openingPriceInput.getText()));
Stock.setClosingPrice(Double.parseDouble(closingPriceInput.getText()));
Stock.setChangeInPrice(Double.parseDouble(changeInPriceInput.getText()));
table.getItems().add(Stock);
symbolInput.clear();
nameInput.clear();
openingPriceInput.clear();
closingPriceInput.clear();
changeInPriceInput.clear();
}
//Delete button clicked
public void deleteButtonClicked(){
ObservableList<Stock> StockSelected, allStocks;
allStocks = table.getItems();
StockSelected = table.getSelectionModel().getSelectedItems();
StockSelected.forEach(allStocks::remove);
}
//Get all of the Stocks
public ObservableList<Stock> getStock(){
ObservableList<Stock> stocks = FXCollections.observableArrayList();
stocks.add(new Stock("AMZN", "Amazon", 571, 576.4583, 5.4583));
stocks.add(new Stock("EBAY", "eBay", 24.10, 23.7318 , -0.3682));
stocks.add(new Stock("AAPL", "Apple Inc.", 103.91, 104.516, 0.606));
stocks.add(new Stock("SNEJF", "Sony Corp", 24.375, 24.375, 0.00));
stocks.add(new Stock("SBUX", "Starbucks", 58.32, 58.86, 0.54));
return stocks;
}
}
附带的FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<children>
<Button fx:id="buttonLogin" layoutX="113.0" layoutY="224.0" mnemonicParsing="false" text="Login">
<font>
<Font size="18.0" fx:id="x1" />
</font>
</Button>
<TextField fx:id="textUsername" layoutX="50.0" layoutY="68.0" prefWidth="200.0" promptText="username" />
<PasswordField id="txtPassword" fx:id="textPassword" layoutX="50.0" layoutY="150.0" prefWidth="200.0" promptText="password" />
<Label fx:id="labelStatus" font="$x1" layoutX="14.0" layoutY="14.0" prefWidth="272.0" text="Status" textAlignment="LEFT" textFill="#cc0000" />
</children>
</AnchorPane>
我确信有一种更简单的方法来实现我的大量代码,或者有一种更简单的方法来实现我创建的一些方法或函数
非常感谢您的帮助。您可以通过以下几种方式实现此目的。从FXML文件加载时,您正在加载父节点。因此,为了从一个屏幕切换到另一个屏幕,可以将现有场景的父节点设置为新的父节点,也可以基于新节点创建新场景,并在主应用程序的舞台上设置新场景。您可能需要公开一些方法,这些方法允许您这样做,或者确保您的屏幕可以访问在主应用程序类中创建的舞台或场景对象
这是一个关于如何在JavaFX应用程序中实现可能的屏幕管理方面的教程,还不错。受最初尝试的启发,我推出了自己的FXML和手动创建的屏幕。应用程序类是入口点,而不是控制器。如果希望先显示登录窗口,则必须先在
start
方法中显示登录,并确保在登录成功之前不显示主窗口。
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<children>
<Button fx:id="buttonLogin" layoutX="113.0" layoutY="224.0" mnemonicParsing="false" text="Login">
<font>
<Font size="18.0" fx:id="x1" />
</font>
</Button>
<TextField fx:id="textUsername" layoutX="50.0" layoutY="68.0" prefWidth="200.0" promptText="username" />
<PasswordField id="txtPassword" fx:id="textPassword" layoutX="50.0" layoutY="150.0" prefWidth="200.0" promptText="password" />
<Label fx:id="labelStatus" font="$x1" layoutX="14.0" layoutY="14.0" prefWidth="272.0" text="Status" textAlignment="LEFT" textFill="#cc0000" />
</children>
</AnchorPane>