JAVAFX学习心得01----Label
package helloworld;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
//GUI类需继承继承Application类
public class Demo01 extends Application {
// 重写start(Stage primaryStage)方法
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
// 创建label
Label label1 = new Label();
Label label2 = new Label("我是label2");
Image image = new Image(getClass().getResourceAsStream("timg.jpg"));
Label label3 = new Label ("我是label3",new ImageView(image));
// 设置文本属性
// 设置文本换行
label1.setWrapText(true);
label2.setGraphic(new ImageView(image));
label2.setTextFill(Color.web("#888888"));
label2.setFont(new Font("宋体", 20));
// 设置label顺时针旋转度数
label2.setRotate(360);
// label标签垂直下移value
label2.setTranslateY(20);
//设置鼠标聚焦事件
label2.setOnMouseEntered((MouseEvent e) -> {
//设置label放大1.5倍
label2.setScaleX(1.5);
label2.setScaleY(1.5);
});
//设置鼠标移动事件
label2.setOnMouseExited((MouseEvent e) -> {
//设置label放大1.0倍
label2.setScaleX(1.0);
label2.setScaleY(1.0);
});
// 设置图标与文字的间距
label2.setGraphicTextGap(50);
// 感觉没什么用
// label2.setTextAlignment(TextAlignment.LEFT);
// 设置图片与文本的相对位置
label2.setContentDisplay(ContentDisplay.TOP);
// 设置窗口标题
primaryStage.setTitle("第一个Label");
// 添加label至窗口内
// root.getChildren().add(label1);
root.getChildren().add(label2);
// root.getChildren().add(label3);
// 初始化窗口大小
primaryStage.setScene(new Scene(root,800,600));
// 显示窗口
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}