Java程序界面设计

JavaFX实现图形用户界面,计算应交的税款。

Java程序界面设计_第1张图片

代码详情:

Mytax.java

 

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.geometry.HPos;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ComboBox;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

 

    /**

     * 计算贷款

     * @author

     *

     */

publicclass Mytax   extends Application{

       /**年收入文本框*/

       private TextField tfAnnualincome=new TextField();

       /**家庭状况*/

       private TextField tfstatus=new TextField();

       /**计算税按钮*/

       private Button btCal=new Button("计算税");

       /**应缴税款文本框*/

       private Label tftax=new Label();

       /**组合框*/

       private ComboBox cbStatus=new ComboBox<>();

 

       @Override

       publicvoid start(Stage primaryStage) throws Exception {

          

           GridPane gridPane=new GridPane();//创建网格面板

           gridPane.setHgap(10);//设置横向间隙

           gridPane.setVgap(4);//设置纵向间隙

          

           //依次添加标签和文本框

           gridPane.add(new Label("年收入:"), 0, 0);

           gridPane.add(tfAnnualincome,1,0);

           gridPane.add(new Label("家庭情况:"), 0, 1);

           gridPane.add(cbStatus, 1, 1);

           gridPane.add(btCal, 1, 2);

           gridPane.add(new Label("应缴税款:"),0, 3);

           gridPane.add(tftax, 1, 3);

          

           //设置居中对齐,靠右对齐

           gridPane.setAlignment(Pos.CENTER);

          

           this.tfAnnualincome.setAlignment(Pos.BOTTOM_LEFT);

           String[] status={"单身","已婚联合付税","已婚单独付税","户主"};

           ObservableList items=FXCollections.observableArrayList(status);

           cbStatus.getItems().addAll(items);

           cbStatus.setStyle("-fx-color:blue");

           cbStatus.setValue("单身");

           this.btCal.setOnAction(e ->calculateTax(items.indexOf(cbStatus.getValue())) );

           this.tftax.setAlignment(Pos.CENTER);

           //设置按钮的水平位置

           GridPane.setHalignment(btCal, HPos.LEFT); 

           //显示出面板

           Scene scene=new Scene(gridPane,500,300);

           primaryStage.setTitle("计算税");

           primaryStage.setScene(scene);

           primaryStage.show();

          

       }

      

       /**

        * 计算贷款数据

        */

       privatevoid calculateTax(intindex){

 

              //从文本框中获得利率

              doubleincome =

              Double.parseDouble(this.tfAnnualincome.getText());

             

             

              //创建Tax对象

              Tax tax = new Tax(index,income);

              doublet=tax.computeTax();

              tftax.setText(String.format("$%.2f", t));

             

           }

   

      

       publicstaticvoid main(String[] args){

           launch();

       }

 

    }

 

Tax.java

publicclass Tax {

   

    privateintstatus ;//家庭状态

    privatedoubleincome;//收入

    privatedoubletax;//应缴的税款

    //用数组rates保存每个档次对应的税率

    privatedouble[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};

    //用二维数组保存税率表,左下标记录家庭状况,右下标记录收入档次

    privateint[][] brackets = {

             {8350, 33950,82250, 171550, 372950}, // Single filer

             {16700, 67900,137050, 208850, 372950}, // Marriedjointly

             {8350, 33950,68525, 104425, 186475}, // Marriedseparately

             {11950, 45500,117450, 190200, 372950} // Head ofhousehold

      };

   

   

   

    publicstaticfinalintSINGLE_FILER=0;

    publicstaticfinalintMARRIED_JOINTLY=1;

    publicstaticfinalintQUALIFIED_WIDOER=1;

    publicstaticfinalintMARRIED_SEPARATELY=2;

    publicstaticfinalintHEAD_OF_HOUSEHOLD=3;

   

   

   

    /**

     * 无参构造方法

     */

    public Tax() {   

       this(0,0);

    }

   

    /**

     * 用家庭状态和收入构建一个tax对象

     * @param status 家庭状态

     * @param income 收入

     */

    public Tax(intstatus, doubleincome) {

      

       this.status = status;

       this.income = income;

    }

 

 

    /**

     * 计算税款

     * @param status 家庭状况

     * @param income 收入

     * @return tax 应交税款

     */

    publicdouble computeTax(){

        

           // 计算第一个区域里的税

           if (income <= brackets[status][0])

             returntax = income * rates[0]; // 得出结果

           else

             tax = brackets[status][0] * rates[0];//得出第一区域的税

 

           // 计算剩余区域的税

           for (inti = 1; i < brackets[0].length; i++) {

             if (income > brackets[status][i])//如果收入大于区域界限

               tax += (brackets[status][i] - brackets[status][i - 1]) *

                 rates[i];//计算这个区域的税

             else {//如果收入小于这个区域的界限

               tax += (income - brackets[status][i - 1]) * rates[i];

               returntax; // 计算还没有计算的税

             }

           }

 

           // 此时还没有返回,计算超出上界的税

           return  tax += (income - brackets[status][4]) * rates[5];        

      }

 

    /**

     * @return the tax

     */

    publicdouble getTax() {

       returntax;

    }

 

    /**

     * @param tax the tax toset

     */

    publicvoid setTax(doubletax) {

       this.tax = tax;

    }

 

    /**

     * @return the rates

     */

    publicdouble[] getRates() {

       returnrates;

    }

 

    /**

     * @param rates the ratesto set

     */

    publicvoid setRates(double[] rates) {

       this.rates = rates;

    }

 

    /**

     * @return the brackets

     */

    publicint[][] getBrackets() {

       returnbrackets;

    }

 

    /**

     * @param brackets thebrackets to set

     */

    publicvoid setBrackets(int[][] brackets) {

       this.brackets = brackets;

    }

 

 

 

 

    /**

     * @return the status

     */

    publicint getStatus() {

       returnstatus;

    }

 

 

    /**

     * @param status thestatus to set

     */

    publicvoid setStatus(intstatus) {

       this.status = status;

    }

 

 

 

 

    /**

     * @return the income

     */

    publicdouble getIncome() {

       returnincome;

    }

 

    /**

     * @param income theincome to set

     */

    publicvoid setIncome(doubleincome) {

       this.income = income;

    }

 

   

   

 

}

 

Mytax.java

 

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.geometry.HPos;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ComboBox;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

 

    /**

     * 计算贷款

     * @author

     *

     */

publicclass Mytax   extends Application{

       /**年收入文本框*/

       private TextField tfAnnualincome=new TextField();

       /**家庭状况*/

       private TextField tfstatus=new TextField();

       /**计算税按钮*/

       private Button btCal=new Button("计算税");

       /**应缴税款文本框*/

       private Label tftax=new Label();

       /**组合框*/

       private ComboBox cbStatus=new ComboBox<>();

 

       @Override

       publicvoid start(Stage primaryStage) throws Exception {

          

           GridPane gridPane=new GridPane();//创建网格面板

           gridPane.setHgap(10);//设置横向间隙

           gridPane.setVgap(4);//设置纵向间隙

          

           //依次添加标签和文本框

           gridPane.add(new Label("年收入:"), 0, 0);

           gridPane.add(tfAnnualincome,1,0);

           gridPane.add(new Label("家庭情况:"), 0, 1);

           gridPane.add(cbStatus, 1, 1);

           gridPane.add(btCal, 1, 2);

           gridPane.add(new Label("应缴税款:"),0, 3);

           gridPane.add(tftax, 1, 3);

          

           //设置居中对齐,靠右对齐

           gridPane.setAlignment(Pos.CENTER);

          

           this.tfAnnualincome.setAlignment(Pos.BOTTOM_LEFT);

           String[] status={"单身","已婚联合付税","已婚单独付税","户主"};

           ObservableList items=FXCollections.observableArrayList(status);

           cbStatus.getItems().addAll(items);

           cbStatus.setStyle("-fx-color:blue");

           cbStatus.setValue("单身");

           this.btCal.setOnAction(e ->calculateTax(items.indexOf(cbStatus.getValue())) );

           this.tftax.setAlignment(Pos.CENTER);

           //设置按钮的水平位置

           GridPane.setHalignment(btCal, HPos.LEFT); 

           //显示出面板

           Scene scene=new Scene(gridPane,500,300);

           primaryStage.setTitle("计算税");

           primaryStage.setScene(scene);

           primaryStage.show();

          

       }

      

       /**

        * 计算贷款数据

        */

       privatevoid calculateTax(intindex){

 

              //从文本框中获得利率

              doubleincome =

              Double.parseDouble(this.tfAnnualincome.getText());

             

             

              //创建Tax对象

              Tax tax = new Tax(index,income);

              doublet=tax.computeTax();

              tftax.setText(String.format("$%.2f", t));

             

           }

   

      

       publicstaticvoid main(String[] args){

           launch();

       }

 

    }

 

Tax.java

publicclass Tax {

   

    privateintstatus ;//家庭状态

    privatedoubleincome;//收入

    privatedoubletax;//应缴的税款

    //用数组rates保存每个档次对应的税率

    privatedouble[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};

    //用二维数组保存税率表,左下标记录家庭状况,右下标记录收入档次

    privateint[][] brackets = {

             {8350, 33950,82250, 171550, 372950}, // Single filer

             {16700, 67900,137050, 208850, 372950}, // Marriedjointly

             {8350, 33950,68525, 104425, 186475}, // Marriedseparately

             {11950, 45500,117450, 190200, 372950} // Head ofhousehold

      };

   

   

   

    publicstaticfinalintSINGLE_FILER=0;

    publicstaticfinalintMARRIED_JOINTLY=1;

    publicstaticfinalintQUALIFIED_WIDOER=1;

    publicstaticfinalintMARRIED_SEPARATELY=2;

    publicstaticfinalintHEAD_OF_HOUSEHOLD=3;

   

   

   

    /**

     * 无参构造方法

     */

    public Tax() {   

       this(0,0);

    }

   

    /**

     * 用家庭状态和收入构建一个tax对象

     * @param status 家庭状态

     * @param income 收入

     */

    public Tax(intstatus, doubleincome) {

      

       this.status = status;

       this.income = income;

    }

 

 

    /**

     * 计算税款

     * @param status 家庭状况

     * @param income 收入

     * @return tax 应交税款

     */

    publicdouble computeTax(){

        

           // 计算第一个区域里的税

           if (income <= brackets[status][0])

             returntax = income * rates[0]; // 得出结果

           else

             tax = brackets[status][0] * rates[0];//得出第一区域的税

 

           // 计算剩余区域的税

           for (inti = 1; i < brackets[0].length; i++) {

             if (income > brackets[status][i])//如果收入大于区域界限

               tax += (brackets[status][i] - brackets[status][i - 1]) *

                 rates[i];//计算这个区域的税

             else {//如果收入小于这个区域的界限

               tax += (income - brackets[status][i - 1]) * rates[i];

               returntax; // 计算还没有计算的税

             }

           }

 

           // 此时还没有返回,计算超出上界的税

           return  tax += (income - brackets[status][4]) * rates[5];        

      }

 

    /**

     * @return the tax

     */

    publicdouble getTax() {

       returntax;

    }

 

    /**

     * @param tax the tax toset

     */

    publicvoid setTax(doubletax) {

       this.tax = tax;

    }

 

    /**

     * @return the rates

     */

    publicdouble[] getRates() {

       returnrates;

    }

 

    /**

     * @param rates the ratesto set

     */

    publicvoid setRates(double[] rates) {

       this.rates = rates;

    }

 

    /**

     * @return the brackets

     */

    publicint[][] getBrackets() {

       returnbrackets;

    }

 

    /**

     * @param brackets thebrackets to set

     */

    publicvoid setBrackets(int[][] brackets) {

       this.brackets = brackets;

    }

 

 

 

 

    /**

     * @return the status

     */

    publicint getStatus() {

       returnstatus;

    }

 

 

    /**

     * @param status thestatus to set

     */

    publicvoid setStatus(intstatus) {

       this.status = status;

    }

 

 

 

 

    /**

     * @return the income

     */

    publicdouble getIncome() {

       returnincome;

    }

 

    /**

     * @param income theincome to set

     */

    publicvoid setIncome(doubleincome) {

       this.income = income;

    }

 }

总结:通过本实例,

Mytax.java

 

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.geometry.HPos;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ComboBox;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

 

    /**

     * 计算贷款

     * @author

     *

     */

publicclass Mytax   extends Application{

       /**年收入文本框*/

       private TextField tfAnnualincome=new TextField();

       /**家庭状况*/

       private TextField tfstatus=new TextField();

       /**计算税按钮*/

       private Button btCal=new Button("计算税");

       /**应缴税款文本框*/

       private Label tftax=new Label();

       /**组合框*/

       private ComboBox cbStatus=new ComboBox<>();

 

       @Override

       publicvoid start(Stage primaryStage) throws Exception {

          

           GridPane gridPane=new GridPane();//创建网格面板

           gridPane.setHgap(10);//设置横向间隙

           gridPane.setVgap(4);//设置纵向间隙

          

           //依次添加标签和文本框

           gridPane.add(new Label("年收入:"), 0, 0);

           gridPane.add(tfAnnualincome,1,0);

           gridPane.add(new Label("家庭情况:"), 0, 1);

           gridPane.add(cbStatus, 1, 1);

           gridPane.add(btCal, 1, 2);

           gridPane.add(new Label("应缴税款:"),0, 3);

           gridPane.add(tftax, 1, 3);

          

           //设置居中对齐,靠右对齐

           gridPane.setAlignment(Pos.CENTER);

          

           this.tfAnnualincome.setAlignment(Pos.BOTTOM_LEFT);

           String[] status={"单身","已婚联合付税","已婚单独付税","户主"};

           ObservableList items=FXCollections.observableArrayList(status);

           cbStatus.getItems().addAll(items);

           cbStatus.setStyle("-fx-color:blue");

           cbStatus.setValue("单身");

           this.btCal.setOnAction(e ->calculateTax(items.indexOf(cbStatus.getValue())) );

           this.tftax.setAlignment(Pos.CENTER);

           //设置按钮的水平位置

           GridPane.setHalignment(btCal, HPos.LEFT); 

           //显示出面板

           Scene scene=new Scene(gridPane,500,300);

           primaryStage.setTitle("计算税");

           primaryStage.setScene(scene);

           primaryStage.show();

          

       }

      

       /**

        * 计算贷款数据

        */

       privatevoid calculateTax(intindex){

 

              //从文本框中获得利率

              doubleincome =

              Double.parseDouble(this.tfAnnualincome.getText());

             

             

              //创建Tax对象

              Tax tax = new Tax(index,income);

              doublet=tax.computeTax();

              tftax.setText(String.format("$%.2f", t));

             

           }

   

      

       publicstaticvoid main(String[] args){

           launch();

       }

 

    }

 

Tax.java

publicclass Tax {

   

    privateintstatus ;//家庭状态

    privatedoubleincome;//收入

    privatedoubletax;//应缴的税款

    //用数组rates保存每个档次对应的税率

    privatedouble[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};

    //用二维数组保存税率表,左下标记录家庭状况,右下标记录收入档次

    privateint[][] brackets = {

             {8350, 33950,82250, 171550, 372950}, // Single filer

             {16700, 67900,137050, 208850, 372950}, // Marriedjointly

             {8350, 33950,68525, 104425, 186475}, // Marriedseparately

             {11950, 45500,117450, 190200, 372950} // Head ofhousehold

      };

   

   

   

    publicstaticfinalintSINGLE_FILER=0;

    publicstaticfinalintMARRIED_JOINTLY=1;

    publicstaticfinalintQUALIFIED_WIDOER=1;

    publicstaticfinalintMARRIED_SEPARATELY=2;

    publicstaticfinalintHEAD_OF_HOUSEHOLD=3;

   

   

   

    /**

     * 无参构造方法

     */

    public Tax() {   

       this(0,0);

    }

   

    /**

     * 用家庭状态和收入构建一个tax对象

     * @param status 家庭状态

     * @param income 收入

     */

    public Tax(intstatus, doubleincome) {

      

       this.status = status;

       this.income = income;

    }

 

 

    /**

     * 计算税款

     * @param status 家庭状况

     * @param income 收入

     * @return tax 应交税款

     */

    publicdouble computeTax(){

        

           // 计算第一个区域里的税

           if (income <= brackets[status][0])

             returntax = income * rates[0]; // 得出结果

           else

             tax = brackets[status][0] * rates[0];//得出第一区域的税

 

           // 计算剩余区域的税

           for (inti = 1; i < brackets[0].length; i++) {

             if (income > brackets[status][i])//如果收入大于区域界限

               tax += (brackets[status][i] - brackets[status][i - 1]) *

                 rates[i];//计算这个区域的税

             else {//如果收入小于这个区域的界限

               tax += (income - brackets[status][i - 1]) * rates[i];

               returntax; // 计算还没有计算的税

             }

           }

 

           // 此时还没有返回,计算超出上界的税

           return  tax += (income - brackets[status][4]) * rates[5];        

      }

 

    /**

     * @return the tax

     */

    publicdouble getTax() {

       returntax;

    }

 

    /**

     * @param tax the tax toset

     */

    publicvoid setTax(doubletax) {

       this.tax = tax;

    }

 

    /**

     * @return the rates

     */

    publicdouble[] getRates() {

       returnrates;

    }

 

    /**

     * @param rates the ratesto set

     */

    publicvoid setRates(double[] rates) {

       this.rates = rates;

    }

 

    /**

     * @return the brackets

     */

    publicint[][] getBrackets() {

       returnbrackets;

    }

 

    /**

     * @param brackets thebrackets to set

     */

    publicvoid setBrackets(int[][] brackets) {

       this.brackets = brackets;

    }

 

 

 

 

    /**

     * @return the status

     */

    publicint getStatus() {

       returnstatus;

    }

 

 

    /**

     * @param status thestatus to set

     */

    publicvoid setStatus(intstatus) {

       this.status = status;

    }

 

 

 

 

    /**

     * @return the income

     */

    publicdouble getIncome() {

       returnincome;

    }

 

    /**

     * @param income theincome to set

     */

    publicvoid setIncome(doubleincome) {

       this.income = income;

    }

 

   

   

 

}

 


   

你可能感兴趣的:(Java学习)