我在程序中创建了一个功能,允许员工登录我制作的应用程序。其代码如下所示:
登录 Controller :
//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 类:
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”的按钮将变得不可见。 我尝试了这个,但它不起作用。即使您以管理员身份登录,它也会隐藏该按钮。
所以我想知道我在这里做错了什么以及是否有人可以帮助我。
如果我错过了什么,请告诉我,我会在下面的评论中发布。
最佳答案
根据用户的角色隐藏和显示控件是一种解决方案,但可能不是最合适的。 您可以选择利用面向对象编程的原则。
使用常规控件的通用标识符创建两种不同的 View ,一种用于 worker ,一种用于经理。做两个不同的控制者,比如经理的控制者,作为 worker 的控制者的继任者。这将为您提供每个用户组的 View 和允许的功能的逻辑分离。
这是一个示例实现。
这是两个 Controller :worker_homescreen.fxml
<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
<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>
常规操作按钮是两个 View 中都可用的控件,它执行相同的功能。 不同的操作按钮在两个 View 中也可用,但根据加载的 View 执行不同的操作。当然,仅限经理按钮仅在 Mannard View 中可用,并执行仅适用于经理的特定功能。
所有这些“魔力”都来自于所使用的 Controller 的继承。
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) {
}
}
如您所见,逻辑清晰,您不必不断隐藏和显示控件。您可以专注于实现业务逻辑,因为您不必不断检查用户的角色。此操作在登录时完成一次。
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();
}
}
}
这种方法的最大优点是您可以轻松添加角色,而不会影响现有逻辑。
关于JavaFX 登录隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47661531/