我是JavaFX的新手,我本周开始学习语法的小教程和示例项目。我创建了一个简单的股票信息表,您可以在其中添加和删除信息,并希望实现该程序的登录窗口。我创建了登录窗口,但不确定如何在我的主函数中正确实现它。
主.java
这包含我的股票应用程序代码。
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;
}
}
控制器.java
这包含我的登录窗口的代码。
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Controller {
@FXML
private Label labelStatus;
@FXML
private TextField textUsername;
@FXML
private TextField textPassword;
public void Login(ActionEvent event) throws Exception {
if (textUsername.getText().equals("CS1113") && textPassword.getText().equals("Section011")) {
labelStatus.setText("Login is Successful");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/sample/Main.fxml"));
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} else {
labelStatus.setText("Login failed!!");
}
}
}
随附的 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 应用程序实现可能的屏幕管理方面的教程:https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1。我自己制作了自己的屏幕来迎合FXML和手动创建的屏幕,灵感来自最初的尝试。