Kra*_*tos 3 java controller javafx
我正在尝试创建一个具有登录功能的库,并且我需要知道哪些用户在我的所有场景中登录。
我正在尝试通过 SceneController 将登录详细信息(来自数据库的响应)从 LoginController 发送到 MainController。
在 SceneController 中,我通过保存资源文件名和当前阶段的参数来更改场景。
登录控制器.java
public class LoginController implements Initializable {
@FXML
private Button cancelButton;
@FXML
private Button loginButton;
@FXML
private Label loginMessageLabel;
@FXML
private TextField usernameField;
@FXML
private TextField passwordField;
public void initialize(URL url, ResourceBundle resourceBundle){
}
public void loginButtonOnAction(ActionEvent event){
if(!usernameField.getText().isBlank() && !passwordField.getText().isBlank()){
validateLogin();
}else{
loginMessageLabel.setText("Datele introduse nu sunt corecte!");
}
}
public void cancelButtonOnAction(ActionEvent event){
Stage stage = (Stage) cancelButton.getScene().getWindow();
stage.close();
}
public void validateLogin(){
DatabaseConnection connectionNew = new DatabaseConnection();
Connection connectDB = connectionNew.getConnection();
String verifyLogin = "SELECT count(1), id, name FROM users WHERE Username= '"+usernameField.getText()+"' AND Password ='"+passwordField.getText()+"'";
System.out.println(verifyLogin);
try{
Statement statement = connectDB.createStatement();
ResultSet queryResult = statement.executeQuery(verifyLogin);
while(queryResult.next()){
if(queryResult.getInt(1) == 1){
Stage stage = (Stage) loginButton.getScene().getWindow();
SceneController sceneController = new SceneController();
sceneController.switchScene(stage, "main-view.fxml");
}else{
loginMessageLabel.setText("Datele introduse nu sunt corecte!");
}
}
}catch (Exception e){
throw new RuntimeException("unhandled", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
主控制器.java
public class MainController implements Initializable {
private static String paneName;
private static String userId;
private static String defaultImagePath = "C:\\Users\\costi\\Desktop\\LibraryImages\\";
private static String defaultImageName = "default.png";
public TableView<Book> tableViewSearch, tableViewHome;
public TableColumn<Book,String> colSearchTitle, colHomeTitle, colSearchPublicatedOn, colHomePublicatedOn, colSearchPublishingHouse, colHomePublishingHouse, colSearchSummary, colHomeSummary;
public TableColumn<Book,Integer> colSearchPrice, colHomePrice, colSearchId, colHomeId;
@FXML
private Button cancelButton, menuHomeButton, menuSearchButton, borrowBookButton;
@FXML
private Pane paneHome, paneSearch, paneBookDetails;
@FXML
private TextField searchField, titleField, publishingHouseField, priceField, bookIdField, userIdField;
@FXML
private DatePicker publishedOnField;
@FXML
private TextArea summaryTextArea;
@FXML
private FlowPane flowPaneCoverImage;
public void initialize(URL url, ResourceBundle resourceBundle) {
paneHome.toFront();
setPaneName("paneHome");
setMainData();
}
public void setMainData(){
colHomeId.setCellValueFactory(new PropertyValueFactory<>("Id"));
colHomeTitle.setCellValueFactory(new PropertyValueFactory<>("Title"));
colHomePublicatedOn.setCellValueFactory(new PropertyValueFactory<>("PublicatedOn"));
colHomePublishingHouse.setCellValueFactory(new PropertyValueFactory<>("PublishingHouse"));
colHomeSummary.setCellValueFactory(new PropertyValueFactory<>("Summary"));
colHomePrice.setCellValueFactory(new PropertyValueFactory<>("Price"));
HomeController homeController = new HomeController();
tableViewHome.setRowFactory( tv -> {
TableRow<Book> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() == 2 && (! row.isEmpty()) ) {
paneBookDetails.toFront();
Book rowData = row.getItem();
String date = rowData.getPublicatedOn();
var dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
var dateTime = LocalDate.parse(date, dateTimeFormatter);
bookIdField.setText(Integer.toString(rowData.getId()));
titleField.setText(rowData.getTitle());
publishingHouseField.setText(rowData.getPublishingHouse());
priceField.setText(Integer.toString(rowData.getPrice()));
publishedOnField.setValue(dateTime);
summaryTextArea.setText(rowData.getSummary());
InputStream stream = null;
try {
stream = new FileInputStream(defaultImagePath + rowData.getCover());
} catch (FileNotFoundException e) {
try {
stream = new FileInputStream(defaultImagePath + defaultImageName);
} catch (FileNotFoundException ex) {
}
}
Image image = new Image(stream);
ImageView imageView = new ImageView(image);
imageView.setFitHeight(flowPaneCoverImage.getHeight());
imageView.setFitWidth(flowPaneCoverImage.getWidth());
flowPaneCoverImage.getChildren().clear();
flowPaneCoverImage.getChildren().add(imageView);
}
});
return row ;
});
tableViewHome.setItems(homeController.getBooks());
}
public void searchButtonOnAction(ActionEvent event) {
colSearchId.setCellValueFactory(new PropertyValueFactory<>("Id"));
colSearchTitle.setCellValueFactory(new PropertyValueFactory<>("Title"));
colSearchPublicatedOn.setCellValueFactory(new PropertyValueFactory<>("PublicatedOn"));
colSearchPublishingHouse.setCellValueFactory(new PropertyValueFactory<>("PublishingHouse"));
colSearchSummary.setCellValueFactory(new PropertyValueFactory<>("Summary"));
colSearchPrice.setCellValueFactory(new PropertyValueFactory<>("Price"));
tableViewSearch.setRowFactory( tv -> {
TableRow<Book> row = new TableRow<>();
row.setOnMouseClicked(ev -> {
if (ev.getClickCount() == 2 && (! row.isEmpty()) ) {
paneBookDetails.toFront();
Book rowData = row.getItem();
String date = rowData.getPublicatedOn();
var dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
var dateTime = LocalDate.parse(date, dateTimeFormatter);
bookIdField.setText(Integer.toString(rowData.getId()));
titleField.setText(rowData.getTitle());
publishingHouseField.setText(rowData.getPublishingHouse());
priceField.setText(Integer.toString(rowData.getPrice()));
publishedOnField.setValue(dateTime);
summaryTextArea.setText(rowData.getSummary());
InputStream stream = null;
try {
stream = new FileInputStream(defaultImagePath + rowData.getCover());
} catch (FileNotFoundException e) {
try {
stream = new FileInputStream(defaultImagePath + defaultImageName);
} catch (FileNotFoundException ex) {
}
}
Image image = new Image(stream);
ImageView imageView = new ImageView(image);
imageView.setFitHeight(flowPaneCoverImage.getHeight());
imageView.setFitWidth(flowPaneCoverImage.getWidth());
flowPaneCoverImage.getChildren().clear();
flowPaneCoverImage.getChildren().add(imageView);
}
});
return row ;
});
SearchController searchController = new SearchController();
tableViewSearch.setItems(searchController.getBooks(searchField.getText()));
}
public void cancelButtonOnAction(ActionEvent event){
Stage stage = (Stage) cancelButton.getScene().getWindow();
stage.close();
}
public void menuButtonOnAction(ActionEvent event){
if(event.getSource() == menuHomeButton){
setPaneName("paneHome");
paneHome.toFront();
setMainData();
}else if(event.getSource() == menuSearchButton){
setPaneName("paneSearch");
paneSearch.toFront();
}
}
public void backButtonOnAction(ActionEvent event){
var lastPannelName = getPaneName();
System.out.println(lastPannelName);
if(lastPannelName == "paneHome"){
paneHome.toFront();
}else if(lastPannelName == "paneSearch"){
paneSearch.toFront();
}
}
public void borrowBookOnAction(ActionEvent event){
HomeController homeController = new HomeController();
var currentBookId = Integer.valueOf(bookIdField.getText());
try {
homeController.borrowBook(1, 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
System.out.println((currentBookId));
}
public static String getPaneName() {
return paneName;
}
public void display(String ceva){
System.out.println(ceva);
}
public static void setPaneName(String paneName) {
MainController.paneName = paneName;
}
}
Run Code Online (Sandbox Code Playgroud)
场景控制器.java
public class SceneController {
private double xOffset = 0;
private double yOffset = 0;
public void switchScene(Stage stage, String file) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource(file));
root.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
stage.setX(event.getScreenX() - xOffset);
stage.setY(event.getScreenY() - yOffset);
}
});
stage.setScene(new Scene(root));
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
使用方法:
在 中SceneController.switchScene,替换:
Parent root = FXMLLoader.load(getClass().getResource(file));
Run Code Online (Sandbox Code Playgroud)
和:
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
file
)
);
Parent root = loader.load();
Controller controller = loader.getController();
// do other stuff
return controller;
Run Code Online (Sandbox Code Playgroud)
返回 aController来自switchScene.
然后使用它:
MainController mainController = (MainController)
sceneController.switchScene(stage, "main-view.fxml");
mainController.setLoginDetails(loginDetails);
Run Code Online (Sandbox Code Playgroud)
备择方案
还有其他选项可以执行此操作(有些选项可以避免强制转换)。
例如,
| 归档时间: |
|
| 查看次数: |
203 次 |
| 最近记录: |