JavaFx登录注册窗口的制作及组件学习汇总

一年一度的软工课设即将到来,View层尽管麻烦但是还必须得做,为了使自己做的TTMS跑起来能更美观,好好学View层的组件,学JavaFX。下面是登录窗口的制作,加入一些css元素使得自己的窗口更加漂亮。在此之后总结一下之前学的做管理系统常用的javafx组件,这些组件真的不总结就会忘掉!

Login.java

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;

public class Login extends Application {
    
    Stage windows ;
    public static void main(String[]args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        windows = primaryStage;
        windows.setTitle("css样式");


        VBox layout = new VBox() ;
        layout.setPadding(new Insets(50,50,50,50));
        layout.setSpacing(20);
        layout.setAlignment(Pos.CENTER);

        HBox layout1 = new HBox() ;
        layout1.setPadding(new Insets(50,50,50,50));
        layout1.setSpacing(20);
        layout1.setAlignment(Pos.CENTER);


        Label lb = new Label("XX管理系统") ;
        lb.setId("bold-label") ;

        TextField tf = new TextField() ;
        tf.setPromptText("请输入用户名");
        tf.setMinWidth(90);
        PasswordField pf = new PasswordField();
        pf.setPromptText("请输入密码");
        pf.setMinWidth(90);

        //除去css中默认的button风格,设置自己的风格
        Button bt= new Button("Login") ;
        Button bt1 = new Button("Sign up") ;
        bt1.getStyleClass().add("button-blue") ;
        
        layout1.getChildren().addAll(bt1,bt) ;
        bt.setMinWidth(120);
        layout.getChildren().addAll(lb, tf,pf,layout1) ;
        Scene sne = new Scene(layout, 500,500) ;
        windows.setScene(sne);
        sne.getStylesheets().add("Viper.css") ;
        windows.show() ;
    }
}	

Viper.css

.root{
    -fx-background-color: #688;
    -fx-font-size:20 ;
}


.button{
    /*混合颜色样式*/
    -fx-background-color: linear-gradient(#dc9656,#ab4642);
    -fx-text-fill: #ffffff;
    /*圆形按钮*/
    -fx-background-radius: 6;
}


.button-blue{
    -fx-background-color: linear-gradient(#dc9634,#bc23fc);
    -fx-text-fill: #198;
    -fx-background-radius: 6;
}

#bold-label{
    /*-fx-highlight-text-fill: ;*/
    -fx-text-fill: #dd8190;
    -fx-font-weight:bold;
    -fx-font-size:30 ;
}	

效果
在这里插入图片描述
建立一个表单,并对表单中的元素进行增删操作

class Product {

    private String name ;
    private int age ;
    private String desc ;

    public Product() {
        name = null ;
        age = 0 ;
        desc = null ;
    }

    public Product(String name, int age, String desc) {
        this.name = name;
        this.age = age;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getDesc() {
        return desc;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
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;

import javax.xml.soap.Text;


public class Tables extends Application {

    Stage window ;
    TextField ageText ;
    TextField descText ;
    TextField nameText ;
    TableView<Product> table ;

    public static void main(String[]args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        window = primaryStage ;
        window.setTitle("列表清单") ;

        //设置姓名列
        TableColumn<Product, String> nameColumn = new TableColumn<>("姓名") ;
        nameColumn.setMinWidth(200);
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        //设置价格列
        TableColumn<Product, Integer> priceColumn = new TableColumn<>("年龄") ;
        priceColumn.setMinWidth(200);
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("age"));

        //设置描述列
        TableColumn<Product, String> descColumn = new TableColumn<>("描述") ;
        descColumn.setMinWidth(200);
        descColumn.setCellValueFactory(new PropertyValueFactory<>("desc"));

        table = new TableView<>() ;
        table.setItems(getProduct()) ;
        table.getColumns().addAll(nameColumn, priceColumn, descColumn) ;

        HBox hLayout ;
        hLayout = new HBox()  ;
        hLayout.setPadding(new Insets(10,10,10,10));
        hLayout.setSpacing(10);

        nameText= new TextField() ;
        nameText.setPromptText("name");
        nameText.setMinWidth(40);
        ageText = new TextField() ;
        ageText.setPromptText("age");
        ageText.setMinWidth(40);
        descText = new TextField() ;
        descText.setMinWidth(40);
        descText.setPromptText("desc");

        Button add = new Button("add") ;
        add.setMinWidth(30);
        add.setOnAction(e->AddClickProduct());
        Button del = new Button("delete") ;
        del.setMinWidth(30);
        del.setOnAction(e-> DeleteClick());
        Button mod = new Button("modify") ;
        mod.setMinWidth(30);
        hLayout.getChildren().addAll(nameText, ageText, descText, add, del, mod) ;


        VBox layout = new VBox() ;
        layout.getChildren().addAll(table, hLayout) ;
        Scene scene = new Scene(layout, 600,600) ;
        window.setScene(scene);
        window.show();
    }

    private void AddClickProduct() {

        Product product = new Product() ;
        product.setAge(Integer.parseInt(ageText.getText())) ;
        product.setDesc(descText.getText());
        product.setName(nameText.getText()) ;
        table.getItems().add(product) ;
        nameText.clear() ;
        descText.clear() ;
        ageText.clear() ;
    }

    //点击删除
    public void DeleteClick() {
        ObservableList<Product>productsSelect,allProducts ;
        allProducts = table.getItems() ;
        productsSelect = table.getSelectionModel().getSelectedItems() ;
        productsSelect.forEach(allProducts::remove);
    }

    //添加元素
    public ObservableList<Product>getProduct() {

        ObservableList<Product> products = FXCollections.observableArrayList() ;
        products.add(new Product("laptop", 566, "socheap!")) ;
        products.add(new Product("kkkksak", 2, "socheap!")) ;
        products.add(new Product("lasss", 244, "so!")) ;
        products.add(new Product("lop", 566, "sop!")) ;
        products.add(new Product("ltop", 56, "cheap!")) ;
        products.add(new Product("lop", 6, "socheap!")) ;
        products.add(new Product("ltop", 66, "soeap!")) ;
        return products ;
    }
}

截图
在这里插入图片描述

四种菜单选项

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class MenuPPP extends Application {

    Stage windows ;

    public static void main(String[]args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        windows = primaryStage ;
        windows.setTitle("菜单组件");

        Menu fileMenu = new Menu("File") ;

        MenuItem newFile = new MenuItem("New...") ;
        newFile.setOnAction(e->System.out.println("想创建一个文件"));
        fileMenu.getItems().add(newFile) ;
        fileMenu.getItems().add(new MenuItem("Open...")) ;
        fileMenu.getItems().add(new SeparatorMenuItem()) ;
        fileMenu.getItems().add(new MenuItem("Module...")) ;
        fileMenu.getItems().add(new SeparatorMenuItem()) ;
        fileMenu.getItems().add(new MenuItem("New Projects...")) ;
        fileMenu.getItems().add(new SeparatorMenuItem()) ;
        fileMenu.getItems().add(new MenuItem("setting...")) ;
        fileMenu.getItems().add(new SeparatorMenuItem()) ;
        fileMenu.getItems().add(new MenuItem("exit...")) ;

        //加下划线表示使用快捷键alt选择
        Menu editMenu = new Menu("_Edit") ;
        editMenu.getItems().add(new MenuItem("cut"));
        editMenu.getItems().add(new MenuItem("copy"));
//        editMenu.getItems().add(new MenuItem("paste"));

        //设置粘贴选项为无效
        MenuItem paste = new MenuItem("paste") ;
        paste.setDisable(true);
        editMenu.getItems().add(paste) ;


        //点击栏
        Menu HelpMenu = new Menu("help") ;
        CheckMenuItem showLine = new CheckMenuItem("Show Line Numbers") ;
        showLine.setOnAction(e->{
            if(showLine.isSelected()) {
                System.out.println("选择了这个选项") ;
            }
            else {
                System.out.println("隐藏了这个选项") ;
            }
        });
        CheckMenuItem autoMenu = new CheckMenuItem("EnAble autoSave") ;
        autoMenu.setSelected(true);
        HelpMenu.getItems().addAll(showLine, autoMenu) ;

        //单选菜单
        Menu diff = new Menu("difficult") ;
        ToggleGroup differ = new ToggleGroup() ;
        RadioMenuItem easy = new RadioMenuItem("Easy") ;
        RadioMenuItem medium= new RadioMenuItem("medium") ;
        RadioMenuItem diffcult = new RadioMenuItem("hard") ;

        easy.setToggleGroup(differ);
        medium.setToggleGroup(differ);
        diffcult.setToggleGroup(differ);

        diff.getItems().addAll(easy,medium,diffcult) ;

        //设置菜单栏选项
        MenuBar menubar = new MenuBar() ;
        menubar.getMenus().addAll(fileMenu,editMenu, HelpMenu,diff) ;

        BorderPane layout = new BorderPane() ;
        layout.setTop(menubar);
        Scene scene = new Scene(layout,600,600) ;

        windows.setScene(scene);
        windows.show() ;
    }
}

运行截图
在这里插入图片描述
多窗口验证的操作

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

//创建多个窗口
public class Change extends Application {

    Button bt1 ;
    Stage window ;
    boolean result  ;
    public static void main(String[]args){
        launch(args) ;
    }
    public void start(Stage primaryStage){

        window = primaryStage ;
        window.setTitle("the new Window!");
        bt1 = new Button("Click on") ;

        bt1.setOnAction(e->{
            result = ConfirmBox.display("pop windows","Are you love me?");
            if(!result){
                System.out.println("NO") ;
            }
        }) ;
        StackPane layout = new StackPane() ;

        layout.getChildren().add(bt1) ;
        Scene scene = new Scene(layout, 200,200) ;
        window.setScene(scene);
        window.show() ;
    }
}

创建多个窗口,互相进行传值
Confirm.java

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;



public class Change extends Application {

    Button bt1 ;
    Stage window ;
    boolean result  ;
    public static void main(String[]args){
        launch(args) ;
    }
    public void start(Stage primaryStage){

        window = primaryStage ;
        window.setTitle("the new Window!");
        bt1 = new Button("Click on") ;

        bt1.setOnAction(e->{
            result = ConfirmBox.display("pop windows","Are you love me?");
            if(!result){
                System.out.println("NO") ;
            }
        }) ;
        StackPane layout = new StackPane() ;

        layout.getChildren().add(bt1) ;
        Scene scene = new Scene(layout, 200,200) ;
        window.setScene(scene);
        window.show() ;
    }
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.awt.event.ActionEvent;
import java.util.Stack;

public class ConfirmBox extends Application {

    static boolean result ;
    static Stage window ;

    public static boolean display(String title, String info) {

        window = new Stage() ;
        VBox layout = new VBox() ;

        window.setTitle(title);

        Label  lb = new Label(info) ;

        Button yes_bt1 = new Button("Yes") ;
        Button no_bt1 = new Button("No") ;

        yes_bt1.setOnAction(e->{
            result = true ;
            window.close() ;
        });

        no_bt1.setOnAction(e->{
            result = false ;
            window.close() ;
        });

        layout.getChildren().add(lb) ;
        layout.getChildren().add(yes_bt1) ;
        layout.getChildren().add(no_bt1) ;
        Scene sne = new Scene(layout,200,200) ;
        window.setScene(sne);
        window.show();
        return result ;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

    }
}

多选项提交ListView

import com.sun.org.apache.bcel.internal.generic.Select;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListViewP extends Application {


    Stage window ;
    public static void main(String[]args) {
            launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage ;
        window.setTitle("剧目管理");

        ListView<String> list  = new ListView<String>() ;
        list.getItems().addAll("apple", "banala","orange","bench", "egg") ;

        list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        Button bt = new Button("提交");

        bt.setOnAction(e->getSelectInfo(list));
        VBox layout = new VBox() ;
        
        layout.setPadding(new Insets(20,20,20,20));
        layout.getChildren().addAll(list, bt) ;
        Scene sc = new Scene(layout,500,500) ;
        window.setScene(sc);
        window.show() ;
    }

    void getSelectInfo(ListView<String>list) {

        String Message ;
        ObservableList<String>movie ;

        movie = list.getSelectionModel().getSelectedItems() ;

        for(String m : movie) {
            System.out.println(m) ;
        }
    }
}

在这里插入图片描述
目录和子目录选项

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeViewLists extends Application {


    Stage window ;
    public static void main(String[]args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        window = primaryStage ;
        window.setTitle("TreeView");

        TreeView tree ;
        TreeItem<String> root, bucky, megan ;

        //root
        root = new TreeItem<>() ;
        root.setExpanded(true);

        //bucky
        bucky = makeBranch("bucky", root) ;
        makeBranch("thenewButton",bucky) ;
        makeBranch("youtube", bucky) ;
        makeBranch("Chilldren", bucky) ;


        ///megan
        megan = makeBranch("megan", root) ;
        makeBranch("thenewButton",megan) ;
        makeBranch("youtube", megan) ;
        makeBranch("Chilldren", megan) ;
        tree = new TreeView<>(root) ;

        tree.setShowRoot(false);

        tree.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue)->{
            if(newValue != null) System.out.println(newValue.toString()) ;
        });
        StackPane layout = new StackPane() ;
        layout.getChildren().add(tree) ;
        Scene sc = new Scene(layout,300,400) ;
        window.setScene(sc) ;
        window.show() ;
    }

    public TreeItem<String>makeBranch(String title, TreeItem<String>parent) {

        TreeItem<String>item = new TreeItem<>(title) ;
        item.setExpanded(true);
        parent.getChildren().add(item) ;
        return item ;
    }
}

在这里插入图片描述
复选框

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.net.ServerSocket;


//复选框
public class CheckBoxS extends Application {

    Stage window ;

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        window = primaryStage ;
        CheckBox check = new CheckBox("篮球");
        CheckBox check1 = new CheckBox("足球") ;
        CheckBox check2 = new CheckBox("乒乓球") ;
        VBox layout = new VBox() ;

        Label lb = new Label("选择一项或者多项");
        lb.setFont(Font.font("Tahoma",20));
        Button bt = new Button("提交") ;
        bt.setOnAction(e->getSelect(check,check1,check2));
        layout.getChildren().addAll(lb, check,check1,check2,bt) ;
        Scene sne = new Scene(layout,500,500) ;

        window.setScene(sne);
        window.show() ;
    }

    private void getSelect(CheckBox b1,CheckBox b2,CheckBox b3){

        if(b1.isSelected()){
            System.out.println("篮球") ;
        }

        if(b2.isSelected()){
            System.out.println("足球") ;
        }

        if(b3.isSelected()){
            System.out.println("乒乓球") ;
        }

    }

    private void register(User user){

        if(user.name.equals("zs")&&user.pass.equals("zs")){
            System.out.println("验证成功") ;
        }

        else{
            System.out.println("验证失败") ;
            window.close() ;
        }
    }
}

class User{
    String name ;
    String pass ;
}

在这里插入图片描述
多窗口的交互

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import static javafx.application.Application.launch;

public class Consult extends Application {

    Stage window ;
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception{

        window = primaryStage ;
        window.setTitle("优乐影院管理系统");
        Button bt1 = new Button("close the window!") ;
        bt1.setOnAction(e->closeProgram());
        StackPane layout = new StackPane() ;
        layout.getChildren().add(bt1) ;
        Scene sne = new Scene(layout, 300, 300);
        window.setScene(sne);
        window.show();

    }
    public void closeProgram(){

        boolean answer = ConfirmBox.display("咨询","你确定要关闭窗口吗?") ;
        if(answer == true) {
            System.out.println("window has been closed!");
            window.close() ;
        }
        else {
            System.out.println(answer) ;
        }
    }
}

在这里插入图片描述
Scene场景的切换

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public class Change2 extends Application{

    Stage window;
    Scene scene1,scene2;

    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception{

        window = primaryStage;
        Label label1 = new Label("This is Scene1");
        Button button1 = new Button("Go to Scene2");
        button1.setOnAction(e -> window.setScene(scene2));

        //Layout 1 - children are laid out in vertical column
        VBox layout1 = new VBox(20);
        layout1.getChildren().addAll(label1,button1);
        scene1 = new Scene(layout1,600,600); //200x200 pixel

        //Button2
        Button button2 = new Button("Go back to Scene1");
        button2.setOnAction(e -> window.setScene(scene1));

        //layout2
        StackPane layout2 = new StackPane();
        layout2.getChildren().addAll(button2);
        scene2 = new Scene(layout2, 600, 600);
        window.setScene(scene1);
        window.setTitle("This is a title");
        window.show();
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值