*15.8(显示鼠标的位罝)
- 编写两个程序,一个当单击鼠标时显示鼠标的位置(参见图15-26a),而另一个当按下鼠标时显示鼠标的位置,当释放鼠标时停止显示
代码展示:编程练习题15_8DisplayMousePosition.java
package chapter_15;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class 编程练习题15_8DisplayMousePosition extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
Text text = new Text("");
pane.getChildren().add(text);
//方法一
/*pane.setOnMouseClicked(e ->{
double dx = e.getX();
double dy = e.getY();
text.setX(dx);
text.setY(dy);
text.setText("("+dx+","+dy+")");
});*/
//方法二
pane.setOnMousePressed(e ->{
double dx = e.getX();
double dy = e.getY();
text.setX(dx);
text.setY(dy);
text.setText("("+dx+","+dy+")");
});
pane.setOnMouseReleased(e ->{
text.setText("");
});
Scene scene = new Scene(pane, 300, 150);
primaryStage.setTitle("编程练习题15_8DisplayMousePosition");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
- 结果展示