我在注册事件处理程序时遇到问题

时间:2019-05-03 18:47:03

标签: java javafx eventhandler actionevent

我在注册一个事件处理程序时遇到麻烦,该事件处理程序应该用于计算用户输入的餐费,所需的小费百分比和营业税的总额。我不断收到一条错误消息,内容为“缺少方法主体,或声明了抽象”。似乎没有语法错误,所以我不确定是什么问题。

老实说,我被困在这一点上,还没有发现问题出在哪里。

public class TipTaxTotal extends Application
{
    //Fields
    private TextField mealCostTextField;
    private TextField tipPercentageTextField;
    private TextField salesTaxTextField;
    private Label totalLabel;

    public static void main (String[] args)
    {

        launch(args);
    }


    @Override
    public void start (Stage primaryStage)
    {
    //Meal cost, Tip percentage, and Sales tax labels & TextFields

        Label mealCostLabel = new Label ("Enter the cost of your meal");

        mealCostTextField = new TextField ();

        Label tipPercentageLabel = new Label ("Enter the desired tip percentage");

                tipPercentageTextField = new TextField ();

        Label salesTaxLabel = new Label ("Enter the sales tax percentage");

                salesTaxTextField = new TextField ();

        //This button will perform the calculation
        Button calcButton = new Button("Calculate");

        //Register the event handler
        calcButton.setOnAction(new CalcButtonHandler());

        //This label will display the total
        totalLabel = new Label ();

        //Put all the Labels and Text Fields in the Hbox spaced by 10 px
        HBox hbox = new HBox(10, mealCostLabel, mealCostTextField,
                tipPercentageLabel, tipPercentageTextField, salesTaxLabel,
                salesTaxTextField);

        //Put Hbox, CalcButton, and total Label in the Vbox
        VBox vbox = new VBox(10, hbox, calcButton, totalLabel);

        //align vbox to the center of the stage
        vbox.setAlignment(Pos.CENTER);

        //set vbox padding to 10 px
        vbox.setPadding(new Insets(10));

        //create a scene
        Scene scene = new Scene(vbox);

        //add the scene to the stage
        primaryStage.setScene(scene);

        //give the stage a title
        primaryStage.setTitle("Tip% and Sales Tax Calculator");

        primaryStage.show();

    }
    //This is where my trouble is
    //event handler
    class CalcButtonHandler implements EventHandler<ActionEvent>
    {

        @Override
        public void handle(ActionEvent event);
        {
            //get the meal cost, tip percentage, and sales tax

            double MealCost =
                    Double.parseDouble(mealCostTextField.getText());

            double TipPercentage =
                    Double.parseDouble(tipPercentageTextField.getText());

            double SalesTax =
                    Double.parseDouble(salesTaxTextField.getText());
            //calculate
            double totalTaxes = SalesTax + TipPercentage;
            double total = MealCost * totalTaxes;

            //display total

            totalLabel.setText(String.format("Total: $%,.2f ", total));
        }
    }
}

应该计算calcButton来获得用户用餐的总费用,用户所需的小费以及用户的营业税。

1 个答案:

答案 0 :(得分:-4)

实现接口时,您根本不需要 @Override ,因为接口与抽象类并不相等。您无法覆盖接口方法,因为该接口方法未定义且未标记为抽象。

相关问题