我有一个程序,我试图在JavaFX中使用事件处理程序来使按钮执行操作,例如标记为Exit的按钮以结束程序。我认为我的代码是正确的,但它不起作用。有什么想法吗?这是代码:
import edu.seminolestate.studentanalysis.ActionEvent;
import edu.seminolestate.studentanalysis.EventHandler;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class ProcessPayrollApplication extends Application {
HourlyEmployee hE = new HourlyEmployee(newName, hoursWorked, hourlyRate);
@Override
public void start(Stage primaryStage) throws Exception {
Label lblName = new Label("Employee name: ");
Label lblHours = new Label("Hours worked: ");
Label lblHourlyPay = new Label("Hourly pay rate: ");
Label lblGrossPay = new Label("Gross pay:" + hE.computeGrossPay());
Label lblTaxes = new Label("Taxes: " + hE.computeTaxAmount());
Label lblNetPay = new Label("Net pay: " + hE.computeNetPay());
TextField txtName = new TextField();
TextField txtHours = new TextField();
TextField txtPay = new TextField();
Button btnCalculate = new Button ("Calculate");
Button btnClear = new Button ("Clear");
Button btnExit = new Button ("Exit");
BorderPane bp = new BorderPane();
GridPane gp = new GridPane();
gp.setVgap(15);
gp.setHgap(15);
FlowPane fp = new FlowPane();
fp.setHgap(10);
fp.setAlignment(Pos.CENTER);
gp.add(lblName, 0, 0);
gp.add(lblHours, 0, 1);
gp.add(lblHourlyPay, 0, 2);
gp.add(lblGrossPay, 0, 3);
gp.add(lblTaxes, 0, 4);
gp.add(lblNetPay, 0, 5);
gp.add(txtName, 1, 0, 2, 1);
gp.add(txtHours, 1, 1, 2, 1);
gp.add(txtPay, 1, 2, 2, 1);
BorderPane.setAlignment(gp, Pos.CENTER);
bp.setCenter(gp);
BorderPane.setAlignment(fp, Pos.CENTER);
bp.setBottom(fp);
//Add event handlers
btnExit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
Scene scene = new Scene(bp, 350, 250);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setTitle("Payroll");
}
public static void main(String[] args) {
Application.launch(args);
}
}
答案 0 :(得分:2)
替换
import edu.seminolestate.studentanalysis.ActionEvent;
import edu.seminolestate.studentanalysis.EventHandler;
带
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
然后不要忘记将按钮添加到窗格。