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

关于java:JavaFX登录隐藏按钮

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

文章目录[隐藏]

  • JavaFX Login hide buttons

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

相关推荐

  • 利用 SQL Server 过滤索引提高查询语句的性能分析 2022年1月23日
  • oracle和mysql批量合并对比详解数据库 2021年7月16日
  • PICE(1):Programming In Clustered Environment - 集群环境内编程模式详解编程语言 2021年7月19日
  • 分页语句详解编程语言 2021年7月19日
  • MySQL查询 语句 2022年4月17日
  • MySQL8.0数据库安装 2022年6月15日
  • SuperSocket 信息: (SpnRegister) : Error 1355。解决方法 2022年1月23日
  • 在sql查询中使用表变量 2022年1月23日
  • 基于SQL Server OS的任务调度机制详解 2022年1月23日
  • MySQL安装配置教程(超级详细) 2022年10月18日

发表回复

请登录后评论...
登录后才能评论

热门标签

AI (11871) aliyun (95062) Android (10391) c (14594) go (7786) google (20654) html (8304) https (10802) iPhone (21436) jd (7444) linux (18686) MD (8346) microsoft (7561) Phone (21786) python (7226) windows (18482) 人工智能 (9889) 安全 (28526) 开源 (16950) 微软 (13275) 手机 (21138) 游戏 (13254) 百度 (7189) 硬件 (14378) 美国 (20456) 苹果 (10017) 观察 (7533) 谷歌 (8726) 车 (28069) 通信 (7496)
  • 欢迎投稿
  • 隐私政策
  • 使用协议
  • 服务条款
  • 版权声明

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

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