javafx简单入门

纪念一下第一次学习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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值