云图网
  • 业界资讯
  • 技术专区
    • 云计算
    • 人工智能
    • 信息安全
    • 大数据
    • 研发管理
    • 大前端
    • 开源
    • 智能运维
    • 编程笔记
    • WordPress
  • 企业战略规划
  • 下载专区
  • 江湖史
  • 随笔记录
登录 注册
投稿
  1. 云图网首页
  2. 技术专区
  3. 编程笔记

关于java:JavaFX登录隐藏按钮

2022年6月18日 19:37 • 编程笔记

JavaFX Login hide buttons

我在我的程序中创建了一个函数,允许员工登录我创建的应用程序。代码如下所示:

登录控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Login for employee
@FXML
private TextField textUsername;

@FXML
private PasswordField textPassword;

@FXML
private ChoiceBox<String> EmpSelect;

Stage dialogStage = new Stage();
Scene scene;

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public LoginController() {
    connection = sqlDatabaseConnection.connectdb();
}

//Login for employee
@FXML
private void handleButtonAction(ActionEvent event) {
    String username = textUsername.getText().toString();
    String password = textPassword.getText().toString();
    String function = EmpSelect.getValue().toString();
    String sql ="SELECT * FROM Employee WHERE username = ? and password = ? and function = ?";

    try {
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        preparedStatement.setString(3, function);
        resultSet = preparedStatement.executeQuery();

        if (!resultSet.next()) {
            infoBox("Enter Correct Username and Password","Failed", null);
        } else {

            if ("Employee".equals(function)) {
                infoBox("Login Successfull","Success", null);
                FXMLDocumentController controller = new FXMLDocumentController();
                controller.newAnchorpane("WorkerHomescreen", paneLogin);
            } else if ("Manager".equals(function)) {
                infoBox("Login Successfull","Success", null);
                FXMLDocumentController controller = new FXMLDocumentController();
                controller.newAnchorpane("WorkerHomescreen", paneLogin);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void infoBox(String infoMessage, String titleBar, String headerMessage) {
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setTitle(titleBar);
    alert.setHeaderText(headerMessage);
    alert.setContentText(infoMessage);
    alert.showAndWait();
}

员工可以通过这段代码登录。
这一切都很好,花花公子。
正如您在此代码中看到的那样,如果您想以员工或经理身份登录,它会在选择框中检查,然后将您重定向到正确的页面。

问题是,如果您以管理员身份登录,您将能够看到所有按钮和标签。
当您以员工身份登录时,您将只能看到几个按钮和标签。

我希望在登录后页面加载时发生这种情况。看到我想登录到名为 WorkerHomescreen.fxml 的页面,我认为在此类中放置一些代码会很方便。

WorkerHomescreen 类:

1
2
3
4
5
6
7
8
9
10
Connection connection = null;
public WorkerHomescreenController() {
    connection = sqlDatabaseConnection.connectdb();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    String Check ="SELECT * FROM Employee WHERE function = ’employee’";
    but4.setVisible(false);
}

所以基本上我想用这段代码来检查你是否以员工身份登录。如果是这种情况,名为”but4″的按钮将变得不可见。
我试过这个,它不工作。即使您以管理员身份登录,它也会隐藏按钮。

所以我想知道我在这里做错了什么,是否有人可以帮助我。

如果我错过了什么,请告诉我,我会在下面的评论中发布。

  • 我希望您在 initialize 方法中遗漏了一些代码!?!
  • 看看这个问题的答案,它提供了有关 JavaFX 的基于角色的基本可见性实现的信息。


根据用户的角色隐藏和显示控件是一种解决方案,但可能不是最合适的。
您可以选择利用面向对象编程的原则。

制作两种不同的视图,一种用于工作人员,一种用于管理人员,使用通用标识符进行常规控制。做两个不同的控制器,比如一个用于管理器的控制器,作为工人控制器的继任者。这将使您在逻辑上分离每个用户组的视图和允许的功能。

这是一个示例实现。

这是两个控制器:worker_homescreen.fxml

1
2
3
4
5
6
7
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.WorkerController">
    <children>
        <Label text="Worker"/>
        <Button text="Different Action Button" onAction="#handleDifferentActionButton"/>
        <Button text="General Action Button" onAction="#handleGeneralActionButton"/>
    </children>
</VBox>

和 manager_homescreen.fxml

1
2
3
4
5
6
7
8
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.ManagerController">
    <children>
        <Label text="Manager"/>
        <Button text="Manager Only Button" onAction="#handleManagerButton"/>
        <Button text="Different Action Button" onAction="#handleDifferentActionButton"/>
        <Button text="General Action Button" onAction="#handleGeneralActionButton"/>
    </children>
</VBox>

常规操作按钮是两个视图中都可用的控件,它执行相同的功能。两个视图中也提供了不同的操作按钮,但根据加载的视图执行不同的操作。当然,Manager Only Button 仅在 Mannard 视图中可用,并执行仅对 Managers 可用的特定功能。

所有这些”魔法”都来自所用控制器的继承。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class WorkerController {

    @FXML
    protected void initialize() {

    }

    @FXML
    protected void handleDifferentActionButton(ActionEvent event) {

    }

    @FXML
    protected void handleGeneralActionButton(ActionEvent event) {

    }
}

public class ManagerController extends WorkerController {

    @FXML
    @Override
    protected void initialize() {
        super.initialize();
    }

    @FXML
    protected void handleManagerButton(ActionEvent event) {

    }

    @FXML
    @Override
    protected void handleDifferentActionButton(ActionEvent event) {

    }
}

如您所见,逻辑已被清除,您不必经常隐藏和显示控件。您可以专注于实现业务逻辑,因为您不必经常检查用户的角色。这在登录时完成一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Controller {
    private static final String WORKER ="/sample/worker_homescreen.fxml";
    private static final String MANAGER ="/sample/manager_homescreen.fxml";

    @FXML
    private void handleLoginButton() {
        …

        if (!resultSet.next()) {
            infoBox("Enter Correct Username and Password","Failed", null);
        } else {
            if ("Employee".equals(function)) {
                infoBox("Login Successfull","Success", null);
                showMainView(getClass().getResource(WORKER));

            } else if ("Manager".equals(function)) {
                infoBox("Login Successfull","Success", null);
                showMainView(getClass().getResource(MANAGER));
            }
        }
    }

    private void showMainView(URL url) {
        try {
            Parent parent = FXMLLoader.load(url);

            Stage stage = new Stage();
            stage.setScene(new Scene(parent, 800, 600));
            stage.show();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这种方法的最大优点是您可以轻松添加角色而不会影响现有逻辑。


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/267481.html

javafxloginmysqlSQL
赞 (0)
0 0
生成海报
关于改变kNN算法中k的值:改变kNN算法中k的值-Java
上一篇 2022年6月18日 19:37
关于 java:Spring Security 身份验证管理器不会在自定义过滤器上被选中
下一篇 2022年6月18日 19:38

相关推荐

  • Percona Xtrabackup快速备份MySQL 2021年8月4日
  • SQL Server分隔函数实例详解 2022年1月23日
  • 'mysql' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 2022年7月19日
  • MySQL多字段/列,联合唯一索引/约束 2022年4月17日
  • [MySQL]快速解决”is marked as crashed and should be repaired”故障 2021年8月29日
  • 分析《人民的名义》的观众评论及总结 2022年5月18日
  • MongoDB 中的 insert into select 和 select into from 2022年5月4日
  • Mysql编译安装参数优化 2021年8月4日
  • mysql登录验证插件失败Authentication method 'caching_sha2_password' not supported by any of the ava 2022年7月25日
  • MySQL8.0数据库安装 2022年6月15日

发表回复

请登录后评论...
登录后才能评论
  • 欢迎投稿
  • 隐私政策
  • 使用协议
  • 服务条款
  • 版权声明

Copyright © 2006-2025 YTSO.COM 版权所有 鲁ICP备15002310号-3 Powered by WordPress

免责声明:本站信息来自互联网收集分享,版权归原创者所有,如果侵犯了您的权益,请发邮件给39941211@qq.com通知我们删除.