显示包括六个标签的界面。
将标签背景色设置为白色。将标签前景色分别
设置为黑色、蓝色、青色、绿色、洋红色和橙色,如图1所示。
设置每个标签的边界为黄色的线边界。
设置每个标签的字体为Times Roman、加粗、20像素。
将每个标签的文本和工具
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class SixLable extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Six Lable");
FlowPane fp = new FlowPane();//保持水平布局,自动换行
Scene scene = new Scene(fp, 600, 200);
//创建
Label labelBlack = new Label("Black");
Label labelBlue = new Label("Blue");
Label labelCyan = new Label("Cyan");
Label labelGreen = new Label("Green");
Label labelMagenta = new Label("Magenta");
Label labelOrange = new Label("Orange");
//字体、大小、加粗
labelBlack.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
labelBlue.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
labelCyan.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
labelGreen.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
labelMagenta.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
labelOrange.setFont(Font.font("Times Roman", FontWeight.BOLD, 20));
//设置背景色
labelBlack.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
labelBlue.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
labelCyan.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
labelGreen.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
labelMagenta.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
labelOrange.setBackground(new Background(new BackgroundFill(Color.WHITE, null, null)));
//设置前景色
labelBlack.setTextFill(Color.BLACK);
labelBlue.setTextFill(Color.BLUE);
labelCyan.setTextFill(Color.CYAN);
labelGreen.setTextFill(Color.GREEN);
labelMagenta.setTextFill(Color.MAGENTA);
labelOrange.setTextFill(Color.ORANGE);
//边框
//三个参数:颜色、边框笔画样式、拐角圆弧半径、边框宽度
labelBlack.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
labelBlue.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
labelCyan.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
labelGreen.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
labelMagenta.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
labelOrange.setBorder(new Border(new BorderStroke(Color.YELLOW, BorderStrokeStyle.SOLID, new CornerRadii(10), new BorderWidths(3))));
//工具提示
Tooltip.install(labelBlack,new Tooltip("Black"));
Tooltip.install(labelBlue,new Tooltip("Blue"));
Tooltip.install(labelCyan,new Tooltip("Cyan"));
Tooltip.install(labelGreen,new Tooltip("Green"));
Tooltip.install(labelMagenta,new Tooltip("Magenta"));
Tooltip.install(labelOrange,new Tooltip("Orange"));
fp.getChildren().addAll(labelBlack,labelBlue,labelCyan,labelGreen,labelMagenta,labelOrange);
primaryStage.setScene(scene);
primaryStage.show();
}
}
提示文本都设置为它的前景色的名字。