我正在寻找一种简单的方法来做到这一点。我无法在任何地方找到它,我已经尝试过API。
所以我有一句话:
没有帐户?点击这里
我想将“here”字设为蓝色并可点击 - 进入按钮。怎么能实现这个?我只是在文本位置后面隐藏一个按钮吗?
答案 0 :(得分:24)
<强>解决方案强>
TextFlow flow = new TextFlow(
new Text("Don't have an account? "), new Hyperlink("Click here")
);
使用FlowPane(Java 7):
FlowPane flow = new FlowPane();
flow.getChildren().addAll(
new Text("Don't have an account? "), new Hyperlink("Click here")
);
<强>示例强>
这是一个完整的可执行示例(Java 8):
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.text.*;
import javafx.stage.Modality;
import javafx.stage.*;
public class TextLink extends Application {
@Override
public void start(final Stage primaryStage) throws Exception {
Stage accountCreation = buildAccountCreationStage(primaryStage);
Hyperlink createAccount = buildCreateAccountLink(primaryStage, accountCreation);
TextFlow flow = new TextFlow(
new Text("Don't have an account? "), createAccount
);
flow.setPadding(new Insets(10));
primaryStage.setScene(new Scene(new Group(flow)));
primaryStage.show();
}
private Hyperlink buildCreateAccountLink(Stage primaryStage, Stage accountCreation) {
Hyperlink createAccount = new Hyperlink("Click here");
createAccount.setOnAction(event -> {
accountCreation.setX(primaryStage.getX());
accountCreation.setY(primaryStage.getY() + primaryStage.getHeight());
accountCreation.show();
});
return createAccount;
}
private Stage buildAccountCreationStage(Stage primaryStage) {
Stage accountCreation = new Stage(StageStyle.UTILITY);
accountCreation.initModality(Modality.WINDOW_MODAL);
accountCreation.initOwner(primaryStage);
accountCreation.setTitle("Create Account");
accountCreation.setScene(new Scene(new Label("<Account Creation Form Goes Here>"), 250, 50));
return accountCreation;
}
public static void main(String[] args) { launch(args); }
}
默认链接突出显示有点奇怪,周围带有虚线边框(如果你希望你可以用css设置样式以获得更好的东西,更类似于网络上的链接;即只是不同颜色的文字来表示访问和未访问链路)。
<强>除了强>
对于您的特定消息,您应该将“没有帐户”文本设为超链接,并删除“单击此处”文本(as recommended by the w3c web standards body)。
相关强>