没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
FX 教程—UI 控件
标签
JavaFX API 的 javafx.scene.control 包中的 Label 类显示一个文本元素。
我们可以包装文本元素以适应特定空间,添加图形图像或使用 JavaFX Label 控件应用
视觉效果。
以下代码显示如何使用 Label 显示文本。
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 300, 130, Color.WHITE);
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);
Label label = new Label("Label");
GridPane.setHalignment(label, HPos.CENTER);
gridpane.add(label, 0, 0);
root.getChildren().add(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
创建标签
JavaFX API 提供了用于创建标签的 Label 类的三个构造函数。
//An empty label
Label label1 = new Label();
//A label with the text element
Label label2 = new Label("Name");
//A label with the text element and graphical icon
Image image = new Image(getClass().getResourceAsStream("labels.jpg"));
Label label3 = new Label("Name", new ImageView(image));
标签内容
创建标签后,我们可以使用 Label 类中的以下方法添加文本和图形内容。
� setText(String text) - 设置标签的文本标题
� setGraphic(Node graphic)- 设置图形图标
setGraphicTextGap 方法设置文本和图标之间的差距。
setTextFill 方法设置标签文本的颜色。
以下代码创建文本标签,向其添加图标,并为文本设置填充颜色。
Label label1 = new Label("Name");
Image image = new Image(getClass().getResourceAsStream("icon.jpg"));
label1.setGraphic(new ImageView(image));
label1.setTextFill(Color.web("#FF76a3"));
以下代码显示如何设置 Label Text 颜色。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Label Sample");
stage.setWidth(400);
stage.setHeight(180);
HBox hbox = new HBox();
Label label1 = new Label("Search");
label1.setTextFill(Color.web("#0076a3"));
hbox.setSpacing(10);
hbox.getChildren().add((label1));
((Group) scene.getRoot()).getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
}
setTextAlignment 方法可以在其布局区域内设置标签内容的对齐方式。
setContentDisplay 方法设置图形相对于文本的位置。该方法接受以下
ContentDisplay 常数中的一个:LFFT,RIGHT,CENTER,TOP,BOTTOM。
标签字体
如果未设置 Label 控件的字体,则使用默认字体大小进行渲染。
要设置字体文本大小,请使用 Label 类中的 setFont 方法。
以下代码将 label1 文本的大小设置为 30 点,将字体名称设置为 Arial。
label.setFont(new Font("Arial", 30));
将文本大小设置为 32 磅,字体名称设置为 Cambria。
label.setFont(Font.font("Cambria", 32));
以下代码显示如何设置标签的字体。
import javafx.application.Application;
import javafx.scene.Group;
剩余167页未读,继续阅读
资源评论
Andy&lin
- 粉丝: 98
- 资源: 214
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现夜间车牌识别程序(1).rar
- 基于matlab实现无线传感器网络无需测距定位算法matlab源代码 包括apit,dv-hop,amorphous在内的共7个
- 基于python的yolov5实现的旋转目标检测
- 基于matlab实现无线传感器网络 CAB定位仿真程序 这是无线传感器节点定位CAB算法的仿真程序,由matlab完成.rar
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- springcodespringcodespringcodespringcode
- 基于matlab实现权值的MAXDEV无线传感器网络定位算法研究 MAXDEV 无线传感器 定位 算法.rar
- sdk.config
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功