15.7(使用鼠标改变颜色)
- 编写一个程序,显示一个圆的颜色,当按下鼠标键时颜色为黑色,释放鼠标时颜色为白色
代码展示:编程练习题15_7CircleColor.java
package chapter_15;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class 编程练习题15_7CircleColor extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
Circle circle = new Circle(30);
stackPane.getChildren().add(circle);
stackPane.setOnMousePressed(e ->{
circle.setFill(Color.BLACK);
});
stackPane.setOnMouseReleased(e ->{
circle.setFill(Color.WHITE);
});
Scene scene = new Scene(stackPane, 300, 150);
primaryStage.setTitle("编程练习题15_7CircleColor");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
结果展示