关于java:JavaFX登录隐藏按钮 | 珊瑚贝

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();
        }
    }
}


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


来源:https://www.codenong.com/47661531/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8629.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?