javafX8初探(提示)

52 篇文章 0 订阅
47 篇文章 4 订阅

本节我们学习提示信息,这个空间可以在任何UI控件上使用,当鼠标移动到UI上时就会出现提示框。

ToolTip类代表了公共的UI,它一般被用来表现UI控件的一些附加信息。提示信息可以通过调用setTooltip方法设置到任何一个UI控件上。

提示有两个状态,激活和显示。当鼠标移动到一个控件的时候,提示就会被激活。当提示信息显示出来后,就是显示状态。显示状态也是激活状态。激活和显示状态之间可能会存在一些延时。

下图是一个有提示信息的密码域:

 

创建提示信息

使用下面的代码创建一个带有提示信息的密码域。

final PasswordField pf = new PasswordField();
final Tooltip tooltip = new Tooltip();
tooltip.setText(
    "\nYour password must be\n" +
    "at least 8 characters in length\n"  +
);
pf.setTooltip(tooltip);


 

任何UI控件都可以使用setToolTip方法设置提示。Tooltip使用setText方法设置文本内容。

因为Tooltip是扩展Labeled类。所以不仅仅只能添加文本内容,也可以添加图标。下面的代码为密码域添加了一个带图标的提示。

Image image = new Image(
    getClass().getResourceAsStream("warn.png")
);
tooltip.setGraphic(new ImageView(image));


 

添加了这写代码后,重新编译运行,得到下面的结果。

在提示中显示应用数据

下面的例子在提示中显示住酒店总消费。

每一个多选框都有一个提示框,每个提示框内都显示具体的价格,如果用户选择了一个多选框,那么这个多选框对应的值就会加入total上,如果取消一个选择,就会从total中减去相应的值。代码如下:

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
 
 
public class TooltipSample extends Application {
 
    final static String[] rooms = new String[]{
        "Accommodation (BB)",
        "Half Board",
        "Late Check-out",
        "Extra Bed"
    };
    final static Integer[] rates = new Integer[]{
        100, 20, 10, 30
    };
    final CheckBox[] cbs = new CheckBox[rooms.length];
    final Label total = new Label("Total: $0");
    Integer sum = 0;
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Tooltip Sample");
        stage.setWidth(330);
        stage.setHeight(150);
 
        total.setFont(new Font("Arial", 20));
        
        for (int i = 0; i < rooms.length; i++) {
            final CheckBox cb = cbs[i] = new CheckBox(rooms[i]);
            final Integer rate = rates[i];
            final Tooltip tooltip = new Tooltip("$" + rates[i].toString());
            tooltip.setFont(new Font("Arial", 16));
            cb.setTooltip(tooltip);

        cb.selectedProperty().addListener(
                (ObservableValue<? extends Boolean> ov, Boolean old_val, 
                Boolean new_val) -> {
                    if (cb.isSelected()) {
                        sum = sum + rate;
                    } else {
                        sum = sum - rate;
                    }
                    total.setText("Total: $" + sum.toString());
                }
            );
        }
 
        VBox vbox = new VBox();
        vbox.getChildren().addAll(cbs);
        vbox.setSpacing(5);
        HBox root = new HBox();
        root.getChildren().add(vbox);
        root.getChildren().add(total);
        root.setSpacing(40);
        root.setPadding(new Insets(20, 10, 10, 20));
 
        ((Group) scene.getRoot()).getChildren().add(root);
 
        stage.setScene(scene);
        stage.show();
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值