1.要对事件源进行注册事件,然后处理事件,才能对事件做出相应的反应。下面程序写出来通过按钮使圆形扩大或者缩小。
package practice;
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.layout.HBox;
public class demo extends Application{
public void start(Stage primaryStage){
Pane pane = new Pane();
Scene scene = new Scene(pane,300,300);
HBox hb = new HBox();
Circle circle = new Circle(100);
circle.centerXProperty().bind(scene.widthProperty().divide(2));
circle.centerYProperty().bind(scene.heightProperty().divide(2));
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLUE);
Button btl = new Button("Enlarge");
Button bts = new Button("Shrink");
hb.getChildren().add(btl);
hb.getChildren().add(bts);
pane.getChildren().addAll(circle,hb);
btl.setOnAction(e->circle.setRadius(circle.getRadius()+10));
bts.setOnAction(e->circle.setRadius(circle.getRadius()-10));
primaryStage.setTitle("controlcircle");
primaryStage.setScene(scene);
primaryStage.show();
}
}
其中是用来注册事件和处理事件的,这里用到了lamabda的表达方法,lamabda的表达方式使对匿名内部类的省略,匿名内部类是对内部类的省略。这样可以使语法更加清晰。
btl.setOnAction(e->circle.setRadius(circle.getRadius()+10));
2.lamabda的应用,实现了一个简单的加法程序。
package practice;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
public class demo extends Application{
public void start(Stage primaryStage){
GridPane pane = new GridPane();
Scene scene = new Scene(pane,300,300);
pane.add(new Text("number1"), 0, 0);
pane.add(new Text("number2"), 0, 1);
pane.add(new Text("resulut"), 0, 2);
pane.hgapProperty().bind(scene.widthProperty().divide(8));
pane.vgapProperty().bind(scene.heightProperty().divide(8));
TextField number1 = new TextField("0");
TextField number2 = new TextField("0");
TextField result = new TextField("0");
result.setEditable(false);
pane.add(number1, 1, 0);
pane.add(number2, 1, 1);
pane.add(result, 1, 2);
Button bt = new Button("calculate!");
pane.add(bt, 1, 3);
bt.setOnAction(e->{
double value1 = Double.parseDouble(number1.getText());
double value2 = Double.parseDouble(number1.getText());
Double value = value1+value2;
String re = value.toString();
System.out.println(value1+value2);
result.setText(re);
}
);
pane.setPadding(new Insets(20,20,20,20));
primaryStage.setTitle("calculate");
primaryStage.setScene(scene);
primaryStage.show();
}
}
其中
bt.setOnAction(e->{
double value1 = Double.parseDouble(number1.getText());
double value2 = Double.parseDouble(number1.getText());
Double value = value1+value2;
String re = value.toString();
System.out.println(value1+value2);
result.setText(re);
}
);
是lamabda的完整应用。另外以下实现了属性的绑定。
pane.hgapProperty().bind(scene.widthProperty().divide(8));
3.鼠标响应,该程序实现了一个鼠标拖动文本框的程序。
package practice;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
public class demo extends Application{
public void start(Stage primaryStage){
Pane pane = new Pane();
Scene scene = new Scene(pane,300,300);
Text text = new Text(50,50,"program is fun");
pane.getChildren().add(text);
text.setOnMouseDragged(e->{
text.setX(e.getX());
text.setY(e.getY());
});
primaryStage.setTitle("calculate");
primaryStage.setScene(scene);
primaryStage.show();
}
}
其中
text.setOnMouseDragged(e->{
text.setX(e.getX());
text.setY(e.getY());
});
是对鼠标拖拽事件的注册与实现。