gpt4 book ai didi

javafx - 如何将 actionListener 添加到 JavaFx 中的标签

转载 作者:行者123 更新时间:2023-12-04 12:50:52 24 4
gpt4 key购买 nike

我正在尝试将 ActionListener 添加到一个标签,当用户输入错误的密码或登录时,该标签就会弹出。这是我的登录 Controller

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)但没有任何效果:(。

如果您不介意,我还会询问有关 public void initialize 函数的问题。它是做什么用的?它会在我创建类(class)时自动弹出。

提前致谢

最佳答案

标签不会触发 Action 事件。您可以为鼠标单击事件使用监听器,例如:

@FXML
private void gotoGoogle() {
// open url etc
}

在 FXML 文件中

<Label fx:id="IncorrectDataLabel" onMouseClicked="#gotoGoogle" ... />

但是,使用 Hyperlink 可能更有意义为此,这将为用户提供更好的视觉反馈,即他们希望点击的内容。只需将标签替换为

<Hyperlink fx:id="IncorrectDataLabel" onAction="#gotoGoogle" text="..." ... />

并相应地更新 Controller 中的类型:

@FXML
private Hyperlink IncorrectDataLabel ;

您需要在 FXML 文件和 Controller 中对 javafx.control.Hyperlink 进行适当的导入。

题外话:使用proper Java naming conventions用于您的变量和方法名称。

关于javafx - 如何将 actionListener 添加到 JavaFx 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com