纪念一下第一次学习Javafx
public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Button button = new Button("OK");
StackPane pane = new StackPane();
pane.getChildren().add(button);
Scene scene = new Scene(pane,200,200);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是第一个简单的加入面板组件(StackPane),这个地方一定要添加到场景中去,要不然出现nvocationtargetexception异常。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Circle circle = new Circle();
circle.setCenterX(100);
circle.setCenterY(100);
circle.setRadius(50);
circle.setStroke(Color.BLACK);//这是圆的边界
circle.setFill(Color.RED);//设置成null就是无色
//Button button = new Button("OK");
Pane pane = new Pane();
pane.getChildren().add(circle);
Scene scene = new Scene(pane,200,200);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这个就是在面板中央显示一个圆了
接下来就是绑定属性
就是圆的位置随着面板大小而变化
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new Pane();
Circle circle = new Circle();
//没有绑定属性
//circle.setCenterX(100);
//circle.setCenterY(100);
//绑定属性circle.centerXProperty()返回的是一个数值绑定属性类的一个对象
circle.centerXProperty().bind(pane.widthProperty().divide(2));
circle.centerYProperty().bind(pane.heightProperty().divide(2));
circle.setRadius(50);
circle.setStroke(Color.BLACK);//这是圆的边界
circle.setFill(Color.RED);
//Button button = new Button("OK");
pane.getChildren().add(circle);
Scene scene = new Scene(pane,500,500);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
javafx样式
circle.setStyle("-fx-stroke:black;-fx-fill:red;");这就是一个设置样式的简单格式,他以-fx-开头,多个样式以;间隔
rotate属性可以设定一个单位旋转角度。
button.setRotate(80),这表示顺时针旋转80度
button.setStyle("-fx-border-color:blue;");
pane.setRotate(45);
pane.setStyle("-fx-border-color:red;-fx-background-color:lightgray;");
效果如下
Font类
Label label = new Label("javafx");
label.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC,20));
Image类和ImageView类
Image image = new Image("2020561604218663.jpg");
ImageView imageView = new ImageView(image);
imageView.fitHeightProperty().bind(pane.heightProperty().divide(2));
//将图像与面板动态绑定
imageView.fitWidthProperty().bind(pane.widthProperty().divide(2));
//将图像与面板动态绑定
pane.getChildren().add(imageView);