事件是从一个事件源上产生的对象
触发一个事件意味着产生一个事件并委派处理器处理该事件
事件驱动编程:当运行一个Java GUI程序的时候,程序和用户进行交互,并且事件驱动它的执行
一个事件可以被定义为一个通知程序某件事发生的信号
事件源对象:产生并触发一个事件的组件,或者简单称为源对象或源组件
一个事件是一个事件类的实例
Java事件类的根类是java.util.EventObject
JavaFX的事件类的根类是javafx.event.Event
一个事件对象包含与该事件相关的所有属性
可以通过EventObject
类中的getSourse()
实例方法来确定一个事件的源对象
如果一个组件可以触发一个事件,那么这个组件的任何子类都可以触发同样类型的事件
内部类,或者称为嵌套类,是定义在另外一个类范围中的类
通常,在一个类只被它的外部类所使用的时候,才将它定义为内部类
一个内部类具有以下特征:
1,一个内部类被编译成一个名为OuterClassName$InnerClassName
的类
2,一个内部类可以引用定义在它所在的外部类中的数据和方法
3,一个内部类可以使用可见性修饰符所定义,和应用在一个类中成员的可见性规则一样
4,一个内部类可以被定义为static,一个static的内部类可以使用外部类的名字所访问,一个static的内部类不能访问外部类中非静态的成员
5,内部类对象通常在外部类中创建
内部类的用途:
1,将相互依赖的类结合到一个主类中
2,避免类名的冲突
OuterClassName$n.calss
package chapter15; //listing 15-6 import chapter10.Loan; import javafx.application.Application; import javafx.geometry.Pos; import javafx.geometry.HPos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class LoanCalculator extends Application { private TextField tfAnnualInterestRate = new TextField(); private TextField tfNumberOfYears = new TextField(); private TextField tfLoanAmount = new TextField(); private TextField tfMonthlyPayment = new TextField(); private TextField tfTotalPayment = new TextField(); private Button btCalculate = new Button("Calculate"); @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create UI GridPane gridPane = new GridPane(); gridPane.setHgap(5); gridPane.setVgap(5); gridPane.add(new Label("Annual Interest Rate:"), 0, 0); gridPane.add(tfAnnualInterestRate, 1, 0); gridPane.add(new Label("Number of Years:"), 0, 1); gridPane.add(tfNumberOfYears, 1, 1); gridPane.add(new Label("Loan Amount:"), 0, 2); gridPane.add(tfLoanAmount, 1, 2); gridPane.add(new Label("Monthly Payment:"), 0, 3); gridPane.add(tfMonthlyPayment, 1, 3); gridPane.add(new Label("Total Payment:"), 0, 4); gridPane.add(tfTotalPayment, 1, 4); gridPane.add(btCalculate, 1, 5); // Set properties for UI gridPane.setAlignment(Pos.CENTER); tfAnnualInterestRate.setAlignment(Pos.BOTTOM_RIGHT); tfNumberOfYears.setAlignment(Pos.BOTTOM_RIGHT); tfLoanAmount.setAlignment(Pos.BOTTOM_RIGHT); tfMonthlyPayment.setAlignment(Pos.BOTTOM_RIGHT); tfTotalPayment.setAlignment(Pos.BOTTOM_RIGHT); tfMonthlyPayment.setEditable(false); tfTotalPayment.setEditable(false); GridPane.setHalignment(btCalculate, HPos.RIGHT); // Process events btCalculate.setOnAction(e -> calculateLoanPayment()); // Create a scene and place it in the stage Scene scene = new Scene(gridPane, 400, 250); primaryStage.setTitle("LoanCalculator"); // Set title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } private void calculateLoanPayment() { // Get values from text fields double interest = Double.parseDouble(tfAnnualInterestRate.getText()); int year = Integer.parseInt(tfNumberOfYears.getText()); double loanAmount = Double.parseDouble(tfLoanAmount.getText()); // Create a loan object. Loan defined in Listing 10.2 Loan loan = new Loan(interest, year, loanAmount); // Display monthly payment and total payment tfMonthlyPayment.setText(String.format("$%.2f", loan.getMonthlyPayment())); tfTotalPayment.setText(String.format("$%.2f", loan.getTotalPayment())); } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }