用JavaFX编写桌面应用程序

我决定编写一个具有方便接口的可执行jar文件,以计算我的日常工作任务。我从最简单的开始。



图片



必须根据固定类型和矩形板的纵横比来确定临界剪应力。



让我们从设置界面开始,我们将使用Scene Builder来提供帮助。



让我们创建一个大小为900x600的工作区。



图片



让我们创建几个具有大小和边距的容器(起初,我只是在一张纸上做过标记,但是那一块已经丢失了)。



图片



接下来,让我们看一下容器的内部。



  1. Button Button

    让我们为按钮分配一个唯一的名称,然后为我们将在其中实现代码的方法的名称



    图片

  2. TextField

    让我们为文本字段指定一个唯一的名称。输入/输出数值时需要该字段。我们还将在该字段中添加其他名称,当用符号输入时该名称将消失



    图片



    图片

  3. 标签

    字段我使用标签字段来表示单元格。

  4. 图像字段

    在这里,我们指定位于项目中的现有图像的名称



    图片

  5. CheckBox字段

    在这里我们指定一个唯一的名称。这对于确定紧固类型是必要的



    图片



sample.fxml文件的最终代码



列出sample.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<Pane prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">

   <children>
      <Button fx:id="button" layoutX="40.0" layoutY="200.0" mnemonicParsing="false" onAction="#showDateTime" prefHeight="71.0" prefWidth="528.0" text="=" />

      <VBox layoutX="312.0" layoutY="20.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="280.0" GridPane.columnIndex="1">
         <children>
            <TextField fx:id="myTextField1" promptText=" " />
            <TextField fx:id="myTextField2" promptText=" b ( )" />
            <TextField fx:id="myTextField3" promptText="" />
            <TextField fx:id="myTextField4" prefHeight="25.0" prefWidth="280.0" promptText="  " />
            <TextField fx:id="myTextField6" prefHeight="25.0" prefWidth="280.0" promptText=" " />
         </children>
      </VBox>
      <VBox layoutX="611.0" prefHeight="562.0" prefWidth="271.0" GridPane.columnIndex="21">
         <children>
            <ImageView fx:id="image" fitHeight="116.0" fitWidth="174.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@Zadelka1.PNG" />
               </image></ImageView>
            <CheckBox fx:id="checkbox1" mnemonicParsing="false" text="  " />
            <ImageView fitHeight="122.0" fitWidth="237.0" layoutX="30.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@Zadelka2.PNG" />
               </image></ImageView>
            <CheckBox fx:id="checkbox2" mnemonicParsing="false" text="      " />
            <ImageView fitHeight="127.0" fitWidth="272.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@Zadelka3.PNG" />
               </image></ImageView>
            <CheckBox fx:id="checkbox3" mnemonicParsing="false" text="  " />
            <ImageView fitHeight="129.0" fitWidth="392.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@Zadelka4.PNG" />
               </image></ImageView>
            <CheckBox fx:id="checkbox4" mnemonicParsing="false" text=" " />
         </children>
      </VBox>

            <VBox layoutX="20.0" layoutY="20.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="262.0">
         <children>
            <Label prefHeight="25.0" prefWidth="185.0" text=" a" />
            <Label prefHeight="25.0" prefWidth="262.0" text=" b ( )" />
            <Label prefHeight="25.0" prefWidth="188.0" text="" />
            <Label prefHeight="25.0" prefWidth="186.0" text="  E" />
            <Label prefHeight="25.0" prefWidth="188.0" text=" " />
         </children>
      </VBox>
      <VBox layoutX="312.0" layoutY="311.0" prefHeight="200.0" prefWidth="280.0">
         <children>
            <TextField fx:id="myTextField5" prefHeight="25.0" prefWidth="280.0" promptText=" ()" />
            <TextField fx:id="myTextField7" prefHeight="25.0" prefWidth="280.0" promptText=" " />
         </children>
      </VBox>
      <VBox layoutX="20.0" layoutY="310.0" prefHeight="200.0" prefWidth="262.0">
         <children>
            <Label prefHeight="25.0" prefWidth="260.0" text="  " />
            <Label prefHeight="25.0" prefWidth="187.0" text=" " />
         </children>
      </VBox>
   </children>
  
</Pane>




创建Main类的main方法



主班
package sample;

import javafx.application.Application;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.stage.Stage;



public class Main extends Application  {

    @Override

    public void start(Stage primaryStage) {
        try {
//  sample.fxml( ).      
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            primaryStage.setTitle(" ");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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




创建Controller类时,最重要的部分带有注释



控制器类
ackage sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

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

import static java.lang.Double.NaN;
import static java.lang.Double.parseDouble;
import static java.lang.String.valueOf;


public class Controller extends TextField implements Initializable  {


//      
    @FXML
    private TextField myTextField4;
    @FXML
    private TextField myTextField1;
    @FXML
    private TextField myTextField2;
    @FXML
    private TextField myTextField3;
    @FXML
    private TextField myTextField5;
    @FXML
    private TextField myTextField6;
    @FXML
    private TextField myTextField7;
    @FXML
    private CheckBox checkbox1;
    @FXML
    private CheckBox checkbox2;
    @FXML
    private CheckBox checkbox3;
    @FXML
    private CheckBox checkbox4;

    private double k;
    private double krit;
    private double zapas;



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        
        //    
        myTextField1.setTextFormatter(new AlphaNumericTextFormatter());
        myTextField2.setTextFormatter(new AlphaNumericTextFormatter());
        myTextField3.setTextFormatter(new AlphaNumericTextFormatter());
        myTextField4.setTextFormatter(new AlphaNumericTextFormatter());
        //myTextField6.setTextFormatter(new AlphaNumericTextFormatter());
    }

    public void showDateTime() throws Exception {


//   
        Double a = parseDouble(myTextField1.getText());
        Double b = parseDouble(myTextField2.getText());
        Double t = parseDouble(myTextField3.getText());
        Double e = parseDouble(myTextField4.getText());
        Double c = b / a;
//  CheckBox   
        if (checkbox1.isSelected()) {
            if (c <= 0.1) {
                k = 8;
            } else if ((c > 0.1) && (c <= 0.15)) {
                k = 8.5;
            } else if ((c > 0.15) && (c <= 0.2)) {
                k = 8.8;
            } else if ((c > 0.2) && (c <= 0.25)) {
                k = 9;
            } else if ((c > 0.25) && (c <= 0.30)) {
                k = 9.2;
            } else if ((c > 0.3) && (c <= 0.35)) {
                k = 9.5;
            } else if ((c > 0.35) && (c <= 0.40)) {
                k = 9.8;
            } else if ((c > 0.40) && (c <= 0.45)) {
                k = 10.3;
            } else if ((c > 0.45) && (c <= 0.50)) {
                k = 10.5;
            } else if ((c > 0.5) && (c <= 0.55)) {
                k = 10.8;
            } else if ((c > 0.55) && (c <= 0.6)) {
                k = 11;
            } else if ((c > 0.6) && (c <= 0.65)) {
                k = 11.5;
            } else if ((c > 0.65) && (c <= 0.7)) {
                k = 11.8;
            } else if ((c > 0.7) && (c <= 0.75)) {
                k = 12;
            } else if ((c > 0.75) && (c <= 0.8)) {
                k = 12.5;
            } else if ((c > 0.8) && (c <= 0.85)) {
                k = 12.8;
            } else if ((c > 0.85) && (c <= 0.9)) {
                k = 13;
            } else if ((c > 0.9) && (c <= 0.95)) {
                k = 13.5;
            } else if ((c > 0.95) && (c <= 1)) {
                k = 14;
            }
            krit = (k * e) / (Math.pow((b / t), 2));

            String d = valueOf(krit);
            //   
            myTextField5.setText(d);
            }
            else if (checkbox2.isSelected()) {
                if (c <= 0.1) {
                    k = 8;
                } else if ((c > 0.1) && (c <= 0.15)) {
                    k = 8.2;
                } else if ((c > 0.15) && (c <= 0.2)) {
                    k = 8.4;
                } else if ((c > 0.2) && (c <= 0.25)) {
                    k = 8.5;
                } else if ((c > 0.25) && (c <= 0.30)) {
                    k = 8.6;
                } else if ((c > 0.3) && (c <= 0.35)) {
                    k = 8.7;
                } else if ((c > 0.35) && (c <= 0.40)) {
                    k = 8.9;
                } else if ((c > 0.40) && (c <= 0.45)) {
                    k = 9;
                } else if ((c > 0.45) && (c <= 0.50)) {
                    k = 9.2;
                } else if ((c > 0.5) && (c <= 0.55)) {
                    k = 9.4;
                } else if ((c > 0.55) && (c <= 0.6)) {
                    k = 9.5;
                } else if ((c > 0.6) && (c <= 0.65)) {
                    k = 9.7;
                } else if ((c > 0.65) && (c <= 0.7)) {
                    k = 10;
                } else if ((c > 0.7) && (c <= 0.75)) {
                    k = 10;
                } else if ((c > 0.75) && (c <= 0.8)) {
                    k = 10.2;
                } else if ((c > 0.8) && (c <= 0.85)) {
                    k = 10.5;
                } else if ((c > 0.85) && (c <= 0.9)) {
                    k = 10.6;
                } else if ((c > 0.9) && (c <= 0.95)) {
                    k = 11;
                } else if ((c > 0.95) && (c <= 1)) {
                    k = 11;
                }
            krit = (k * e) / (Math.pow((b / t), 2));

            String d = valueOf(krit);
            myTextField5.setText(d);
            }

        else if (checkbox3.isSelected()) {
            if (c <= 0.1) {
                k = 4.9;
            } else if ((c > 0.1) && (c <= 0.15)) {
                k = 5.1;
            } else if ((c > 0.15) && (c <= 0.2)) {
                k = 5.2;
            } else if ((c > 0.2) && (c <= 0.25)) {
                k = 5.5;
            } else if ((c > 0.25) && (c <= 0.30)) {
                k = 5.8;
            } else if ((c > 0.3) && (c <= 0.35)) {
                k = 6;
            } else if ((c > 0.35) && (c <= 0.40)) {
                k = 6.2;
            } else if ((c > 0.40) && (c <= 0.45)) {
                k = 6.5;
            } else if ((c > 0.45) && (c <= 0.50)) {
                k = 6.9;
            } else if ((c > 0.5) && (c <= 0.55)) {
                k = 7;
            } else if ((c > 0.55) && (c <= 0.6)) {
                k = 7.5;
            } else if ((c > 0.6) && (c <= 0.65)) {
                k = 8;
            } else if ((c > 0.65) && (c <= 0.7)) {
                k = 8.2;
            } else if ((c > 0.7) && (c <= 0.75)) {
                k = 8.5;
            } else if ((c > 0.75) && (c <= 0.8)) {
                k = 9.1;
            } else if ((c > 0.8) && (c <= 0.85)) {
                k = 9.5;
            } else if ((c > 0.85) && (c <= 0.9)) {
                k = 10;
            } else if ((c > 0.9) && (c <= 0.95)) {
                k = 10.5;
            } else if ((c > 0.95) && (c <= 1)) {
                k = 11;
            }
            krit = (k * e) / (Math.pow((b / t), 2));

            String d = valueOf(krit);
            myTextField5.setText(d);
        }
        else if (checkbox4.isSelected()) {
            if (c <= 0.1) {
                k = 4.8;
            } else if ((c > 0.1) && (c <= 0.15)) {
                k = 5;
            } else if ((c > 0.15) && (c <= 0.2)) {
                k = 5;
            } else if ((c > 0.2) && (c <= 0.25)) {
                k = 5.1;
            } else if ((c > 0.25) && (c <= 0.30)) {
                k = 5.2;
            } else if ((c > 0.3) && (c <= 0.35)) {
                k = 5.4;
            } else if ((c > 0.35) && (c <= 0.40)) {
                k = 5.5;
            } else if ((c > 0.40) && (c <= 0.45)) {
                k = 5.6;
            } else if ((c > 0.45) && (c <= 0.50)) {
                k = 5.9;
            } else if ((c > 0.5) && (c <= 0.55)) {
                k = 6;
            } else if ((c > 0.55) && (c <= 0.6)) {
                k = 6.2;
            } else if ((c > 0.6) && (c <= 0.65)) {
                k = 6.5;
            } else if ((c > 0.65) && (c <= 0.7)) {
                k = 6.7;
            } else if ((c > 0.7) && (c <= 0.75)) {
                k = 7;
            } else if ((c > 0.75) && (c <= 0.8)) {
                k = 7.1;
            } else if ((c > 0.8) && (c <= 0.85)) {
                k = 7.4;
            } else if ((c > 0.85) && (c <= 0.9)) {
                k = 7.8;
            } else if ((c > 0.9) && (c <= 0.95)) {
                k = 8;
            } else if ((c > 0.95) && (c <= 1)) {
                k = 8.3;
            }
            krit = (k * e) / (Math.pow((b / t), 2));
            String d = valueOf(krit);
            myTextField5.setText(d);

        }
        Double f = parseDouble(myTextField6.getText());
            zapas = krit/f;
            String g = valueOf(zapas);
            myTextField7.setText(g);
        }
        }




创建两个用于应用数据输入过滤器的类(不包括字母,以及限制输入/输出数量的能力)。解决方案不是我的,而是我在stackoverflow上发现的,在我提出的所有解决方案中,这似乎是最简单,最容易理解的。



AlphaNumericTextFormatter类



AlphaNumericTextFormatter类
public class AlphaNumericTextFormatter extends TextFormatter<String> {

    /** The Constant ALPHA_NUMERIC_ONLY. */
    //private static final String ALPHA_NUMERIC_ONLY = "^[a-zA-Z0-9]*$";
    /** MAKE NUMERIC ONLY **/
    private static final String DIGITS_ONLY = "^[0-9.]*$";

    /**
     * Instantiates a new alpha numeric text formatter.
     */
    public AlphaNumericTextFormatter() {
        super(applyFilter(null));
    }

    /**
     * Instantiates a new alpha numeric text formatter.
     *
     * @param maxLength
     *            the max length
     */
    public AlphaNumericTextFormatter(int maxLength) {
        super(applyFilter(new MaxLengthTextFormatter(maxLength).getFilter()));
    }

    /**
     * Apply filter.
     *
     * @param filter
     *            the filter
     * @return the unary operator
     */
    private static UnaryOperator<Change> applyFilter(UnaryOperator<Change> filter) {
        return change -> {
            if (change.getControlNewText() != null && change.getControlNewText().matches(DIGITS_ONLY)) {
                if (filter != null) {
                    filter.apply(change);
                }
                return change;
            }
            return null;
        };
    }

}




MaxLengthTextFormatter类



代码清单

package sample;

import java.util.function.UnaryOperator;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;

public class MaxLengthTextFormatter extends TextFormatter<String> {
    private int maxLength;

    public MaxLengthTextFormatter(final int maxLength) {
        super(new UnaryOperator<Change>() {
            public Change apply(Change change) {
                if (change.isDeleted()) {
                    if (change.getControlNewText().length() > maxLength) {
                        change.setText(change.getText().substring(0, maxLength));
                    }
                } else if (change.getControlText().length() + change.getText().length() >= maxLength) {
                    int maxPos = maxLength - change.getControlText().length();
                    change.setText(change.getText().substring(0, maxPos));
                }

                return change;
            }
        });
        this.maxLength = maxLength;
    }

    public int getMaxLength() {
        return this.maxLength;
    }
}




好了,在创建jar文件的最后



图片



图片



就这样。现在该文件已准备就绪,可以在装有Java的任何计算机上运行。



All Articles