登陆界面

来源:互联网 发布:文明之旅刘芳菲 知乎 编辑:程序博客网 时间:2024/04/27 20:12


Login.java文件:

package login;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.stage.Stage;/** * This file includes the code for setting up the application main class and for * defining the stage and scene. More specific to FXML, the file uses the * FXMLLoader class, which is responsible for loading the FXML source file and * returning the resulting object graph. *  * @author HL * */public class Login extends Application {@Overridepublic void start(Stage primaryStage) throws Exception {Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));primaryStage.setTitle("Login");/* * A good practice is to set the width and height of the scene when you * create it, in this case 300 by 275; otherwise the scene defaults to * the minimum size needed to display its contents. */primaryStage.setScene(new Scene(root, 300, 275));primaryStage.show();}public static void main(String[] args) {launch(args);// Launch a standalone application.}}


login.fxml文件:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.layout.GridPane?><?import javafx.scene.layout.HBox?><?import javafx.scene.control.Label?><?import javafx.scene.control.TextField?><?import javafx.scene.control.PasswordField?><?import javafx.scene.control.Button?><?import javafx.scene.text.Text?><?import javafx.geometry.Insets?><?import java.net.URL?><!--The fx:controller is required when you specify controller-based event handlers in markup. The xmlns:fx is always required and specifies the fx namespace.--><GridPane fx:controller="login.LoginController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10" styleClass="root"><padding><Insets top="25" right="25" bottom="10" left="25"/></padding> <Text id="welcome-text" text="Welcome" GridPane.columnIndex="0" GridPane.rowIndex="0"  GridPane.columnSpan="2"/> <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <PasswordField GridPane.columnIndex="1" GridPane.rowIndex="2"/> <!--An HBox pane is needed to set an alignment for the button that is different from the  default alignment applied to the other controls in the GridPane layout.--> <HBox alignment="bottom_right" spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="4"> <Button text="Sign In" onAction="#handleSubmitButtonAction"/> </HBox> <Text fx:id="actionTarget" GridPane.columnIndex="0" GridPane.rowIndex="6"  GridPane.columnSpan="2" GridPane.halignment="right"/> <stylesheets>  <!--The @ symbol before the name of the style sheet in the URL indicates that the style   sheet is in the same directory as the FXML file.-->  <URL value="@Login.css"/> </stylesheets></GridPane>


LoginController.java文件:

package login;import javafx.event.ActionEvent;import javafx.fxml.FXML;import javafx.scene.text.Text;/** * The @FXML annotation is used to tag nonpublic controller member fields and * handler methods for use by FXML markup. *  * @author HL * */public class LoginController {@FXMLprivate Text actionTarget;@FXMLprotected void handleSubmitButtonAction(ActionEvent event) {actionTarget.setText("Sign in button pressed");}}


Login.css文件:

.root {/* The root style is used to set the global properties. */-fx-background-image: url("background.jpg");}.label {-fx-effect: dropshadow(gaussian, rgba(255,255,255,0.5), 0,0,0,1);-fx-font-weight: bold;/* * Normally we use the "Arial" for screen controls display;  * the "Times New Roman" for print use;  * the "Courier New" specially for code display. */-fx-font-family: "Arial";// Normally I prefer to specify the font family for surety.}#welcome-text {-fx-effect: innershadow(three-pass-box, rgba(0,0,0,0.7), 6,0,0,2);-fx-font-size: 32px;-fx-font-family: "Arial Black";-fx-fill: #818181;}#actionTarget {-fx-effect: dropshadow(gaussian, rgba(255,255,255,0.5), 0,0,0,1);-fx-font-weight: bold;-fx-font-family: "Arial";-fx-fill: FIREBRICK;}.button {-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.6), 5,0,0,1);-fx-background-color: linear-gradient(#61a2b1, #2a5058);-fx-text-fill: white;}.button:hover {-fx-background-color: linear-gradient(#2a5058, #61a2b1);}

以上项目结构的优点就是分工明确、便于维护。

可以在平时用的Eclipse下编译运行;打包的话也和平常一样使用Eclipse内建的Export->Jar工具即可,例如双击如下.jar文件图标就可以运行了:



0 0
原创粉丝点击