我正在设计一个JavaFX应用程序,我需要在另一个窗口的Controller中调用其中一个窗口的Application类。
MainController.java:
public class MainController {
@FXML
public Button buttonLogin;
@FXML
public Button buttonNeuAnmelden;
@FXML
public void handleButtonLoginAction(ActionEvent event) {
((Node) (event.getSource())).getScene().getWindow().hide();
System.out.println("LoginButton geclickt!");
}
@FXML
public void handleButtonNeuAnmeldenAction(ActionEvent event) {
((Node) (event.getSource())).getScene().getWindow().hide();
System.out.println("NeuAnmeldenButton Geclickt!");
}
}
LoginApp.java:
public class LoginApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/design/Login.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Benutzerverwaltung");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
我特别需要在LoginApp
方法中运行main(String[] args)
的所有方法,意味着start(Stage primaryStage)
和handleButtonLoginAction()
类,就像调用整个类一样正是在那一点上。
我该怎么做?
答案 0 :(得分:2)
如果我正确理解了这个问题,你需要对此进行重构。定义一个独立于LoginView
子类的Application
类:
public class LoginView {
private final Stage displayStage ;
private final Scene scene ;
public LoginView(Stage displayStage) throws IOException {
this.displayStage = displayStage ;
FXMLLoader loader = new FXMLLoader(
getClass().getResource("/design/Login.fxml"));
Parent root = loader.load();
scene = new Scene(root);
displayStage.setScene(scene);
displayStage.setTitle("Benutzerverwaltung");
}
public LoginView() throws IOException {
this(new Stage());
}
public void show() {
displayStage.show();
}
public void hide() {
displayStage.hide();
}
// ...
}
然后你的Application
课程如下:
public class LoginApp extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
LoginView loginView = new LoginView(primaryStage);
// ...
loginView.show();
}
}
您的问题未显示MainController
与应用程序的关系,但您需要做的就是将您创建的loginView
的引用传递给MainController
,然后从loginView.show()
。
MainController
答案 1 :(得分:0)
如果您不想重构您的软件架构,可以尝试Reflection,并执行以下操作:
<!--Search Bar-->
<form align="center" action="http://10.42.220.7/782/Groups/Group-O1/searchbar.php" method=get>
<input type="search" name="search1" placeholder="Search Database">
<input type="submit" value="Search">
</form> <?php
require_once("db.php");
$search_text = isset($_GET['search1']);
$content = "<tbody>";
$querysearch = "select *
from 782groupo1.property
WHERE city like ('search1')";
$results1 = mysqli_query($con, $querysearch);
while ($row = mysqli_fetch_array($results1)) {
$content .=
"{[$row['city']}
<td>{[$row['state']}</td>
<td>{[$row['property_id']}</td>
<td></tr>$";
}
$content .= "</tbody>";
echo $content;
mysqli_free_result($results1);
?>
}