javaFX 界面例子

18 篇文章 3 订阅

入口代码

package com.company;


import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        File file = new File("info/fxml/mainFrame.fxml");
        URL url = file.toURI().toURL();
        Parent root = FXMLLoader.load(url);
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.setTitle("标题");
        primaryStage.setResizable(false);

        primaryStage.getIcons().add(new Image(new FileInputStream("info/img/logo.png")));
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.out.println("程序退出了");
                System.exit(0);
            }
        });
        primaryStage.show();
    }

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

}

Controller

package com.company;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.control.ChoiceBox;
import javafx.scene.input.*;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    @FXML private TextField mubanTextField;
    @FXML private TextField t_serial_txt;
    @FXML private TextField t_pdf_path;
    @FXML private ChoiceBox choiceBoxFiled;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        t_serial_txt.setText("我支持.txt文件拖入");
        t_action2(t_serial_txt, ".txt");
        t_pdf_path.setText("我支持.pdf文件拖入");
        t_action2(t_pdf_path, ".pdf");


        //初始化 choiceBoxFiled
        choiceBoxFiled.getItems().clear();
        choiceBoxFiled.getItems().add("123");
        choiceBoxFiled.getItems().add("456");
    }

    private void t_action2(TextField textField, String suffix) {
        textField.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                if (event.getGestureSource() != textField) {
                    event.acceptTransferModes(TransferMode.ANY);
                }
            }
        });
        textField.setOnDragDropped(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                Dragboard dragboard = event.getDragboard();
                if (dragboard.hasFiles()) {
                    try {
                        File file = dragboard.getFiles().get(0);
                        System.out.println(file);
                        if (file != null && file.getName().toLowerCase().indexOf(suffix) != -1) {
                            textField.setText(file.getAbsolutePath());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void action1(ActionEvent actionEvent) {
        System.out.println("点击了配置");
    }

    public void action2(ActionEvent actionEvent) {
        System.out.println("点击了教程");
    }

    public void work(ActionEvent actionEvent) {
        System.out.println("点击了生成");
    }

    public void checkBoxClick(MouseEvent mouseEvent) {
        System.out.println("点击了保存模板");
    }

    public void keyReleased(KeyEvent keyEvent) {
        System.out.println("mubanTextField 模板输入框 内容发生了改变:"+mubanTextField.getText());
    }

    public void del(ActionEvent actionEvent) {
        System.out.println("点击了删除模板");
    }

    public void open_serial(ActionEvent actionEvent) {
        System.out.println("点击了打开数据");
    }
}

目录结构

在这里插入图片描述

FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>
<GridPane prefHeight="262.0" style="-fx-pref-width: 400;-fx-font-family: SansSerif" xmlns="http://javafx.com/javafx/8.0.121"
          xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.company.Controller">
    <VBox alignment="TOP_LEFT" style="-fx-hgap: 10;-fx-hgap: 10;" GridPane.columnIndex="0" GridPane.columnSpan="2"
          GridPane.rowIndex="0">
        <MenuBar fx:id="menuBar" prefHeight="25.0" prefWidth="650">
            <!-- 设置action实际上是作用于它的MenuItem身上的 -->
            <Menu onAction="#action1" text="配置">
                <items>
                    <MenuItem text="标记"/>
                </items>
            </Menu>
            <Menu onAction="#action2" text="教程">
                <items>
                    <MenuItem text="别看了没有"/>
                </items>
            </Menu>
        </MenuBar>
    </VBox>
    <BorderPane fx:id="borderPane" style="-fx-pref-width: 400px;-fx-max-width: 400px;" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <center>
            <GridPane style="-fx-hgap: 5;-fx-vgap: 7;-fx-padding: 5;">
                <Label GridPane.columnIndex="0"  GridPane.rowIndex="0" text="模板" />
                <TextField fx:id="t_data_txt" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="0" />
                <Label GridPane.columnIndex="0"  GridPane.rowIndex="1" text="数据"/>
                <TextField fx:id="t_serial_txt" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="1" />
                <Label GridPane.columnIndex="0"  GridPane.rowIndex="2" text="背景"/>
                <TextField fx:id="t_pdf_path" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="2" />




                <Label   text="历史模板" GridPane.columnIndex="0"    GridPane.columnSpan="2" GridPane.rowIndex="3"/>


                <ChoiceBox fx:id="choiceBox" prefWidth="190.0" GridPane.columnIndex="2" GridPane.rowIndex="3">
                    <items>
                        <FXCollections fx:factory="observableArrayList">
                            <String fx:value="male" />
                            <String fx:value="female" />
                            <String fx:value="unknown" />
                        </FXCollections>
                    </items>
                </ChoiceBox>




                <Button fx:id="b_button" onAction="#work" prefWidth="120" text="生成" GridPane.columnIndex="3"
                        GridPane.rowIndex="3"/>



                <CheckBox fx:id="myCheckBox" onMouseClicked="#checkBoxClick" text="保存模板" GridPane.columnIndex="0"  GridPane.columnSpan="2" GridPane.rowIndex="4"/>
                <TextField fx:id="mubanTextField" onKeyReleased="#keyReleased" GridPane.columnIndex="2" GridPane.rowIndex="4"/>

                <Button fx:id="del_button" onAction="#del" prefWidth="120" text="删除模板" GridPane.columnIndex="3"
                        GridPane.rowIndex="4"/>

                <CheckBox fx:id="filedCheckBox" onMouseClicked="#checkBoxClick" text="按列名存"  GridPane.columnIndex="0"    GridPane.columnSpan="2" GridPane.rowIndex="5"/>

                <ChoiceBox fx:id="choiceBoxFiled" prefWidth="190.0" GridPane.columnIndex="2" GridPane.rowIndex="5">
                    <items>
                        <FXCollections fx:factory="observableArrayList">
                            <!--<String fx:value="male" />-->
                            <!--<String fx:value="female" />-->
                            <!--<String fx:value="unknown" />-->
                        </FXCollections>
                    </items>
                </ChoiceBox>


                <CheckBox fx:id="anshufuzhiCheckBox" onMouseClicked="#checkBoxClick" text="按数复制"  GridPane.columnIndex="0"    GridPane.columnSpan="2" GridPane.rowIndex="6"/>

                <ChoiceBox fx:id="anshufuzhiChoiceBox" prefWidth="190.0" GridPane.columnIndex="2" GridPane.rowIndex="6">
                    <items>
                        <FXCollections fx:factory="observableArrayList">
                            <!--<String fx:value="male" />-->
                            <!--<String fx:value="female" />-->
                            <!--<String fx:value="unknown" />-->
                        </FXCollections>
                    </items>
                </ChoiceBox>
                <Label GridPane.columnIndex="3"  GridPane.rowIndex="6" text="←选择数量字段"/>

                <Button fx:id="b_button2" onAction="#open_serial" prefWidth="120" text="打开数据" GridPane.columnIndex="3"
                        GridPane.rowIndex="5"/>

                <columnConstraints>
                    <ColumnConstraints/>
                    <ColumnConstraints/>
                    <ColumnConstraints/>
                    <ColumnConstraints/>
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                    <RowConstraints/>
                </rowConstraints>
            </GridPane>
        </center>
        <bottom>
            <FlowPane fx:id="zs_flowPane" style="max-height: 300; -fx-padding: 5px">
                <ImageView fx:id="imageView" fitHeight="650.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true" >
                    <image>
                        <Image url="@../img/instruction.png"/>
                    </image>
                </ImageView>
            </FlowPane>
        </bottom>

    </BorderPane>
    <columnConstraints>
        <ColumnConstraints/>
        <ColumnConstraints/>
    </columnConstraints>
    <rowConstraints>
        <RowConstraints/>
        <RowConstraints/>
    </rowConstraints>


</GridPane>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值