【发布时间】:2016-12-15 19:08:01
【问题描述】:
我正在尝试将 ActionListener 添加到标签中,每当用户输入错误的密码或登录名时就会弹出。 这是我的登录控制器
public class LoginController implements Initializable {
@FXML
private Label label;
@FXML
private TextField LoginField;
@FXML
private PasswordField PasswdField;
@FXML
private Button LogInButton;
@FXML
private Label IncorrectDataLabel;
//private String uri = "http://google.com";
@FXML
private void LogIn(ActionEvent event) throws IOException {
if(LoginField.getText().equals("MKARK")&&PasswdField.getText().equals("KACZOR1"))
{
Parent parent = FXMLLoader.load(getClass().getResource("/fxmlFiles/MainScreen.fxml"));
Scene MainScene = new Scene(parent);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(MainScene);
stage.show();
}
else
{
IncorrectDataLabel.setVisible(true);
// <------------------- Here I want to bind hyperlink to a label with would open the google site, whenever user clicks it.
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
我该如何解决这个问题?我已经尝试了很多次(setOnAction、addMouseListener)但没有任何效果:(。
如果您不介意,我还会询问公共 void 初始化函数。它是干什么用的?当我创建类时它会自动弹出。
提前致谢
【问题讨论】:
-
标签不会触发动作事件,因此不清楚您的意思。你想让这个方法运行,用户实际上做了什么? (
initialize()方法在@FXML-annotated 字段注入后调用,用于初始化控制器。) -
我的意思是在这个 IncorrectDataLabel 弹出后,用户将能够点击它并重定向到我想要的网站(就像超链接一样)。
-
为什么不使用
Hyperlink而不是标签? -
坦率地说,我不知道 =D。我只是在尝试一切:P。我会尝试的!谢谢
-
啊,这是一个广泛的问题,实际上是一个见仁见智的问题。 SceneBuilder 非常适合快速制作原型,也非常适合启动复杂的 UI。我经常发现我需要做的事情不受 SceneBuilder 支持,例如
<fx:include>或<fx:define>元素,因此在中大型项目中,我可能会使用 SceneBuilder 启动各种 UI 屏幕,但有时最终会在稍后编辑 FXML 代码。将视图 (FXML) 与控制器分离通常是一件好事。