我正在尝试使用 JavaFX 2 和 Spring Boot 创建新应用程序,但到目前为止,我的简单应用程序(如 hello world)尚未运行,因为MainPaneController.
MainPaneController 类:
public class MainPaneController implements Initializable {
public static final String VIEW = "/fxml/Scene.fxml";
@FXML
private Node root;
@FXML
private Label label;
@PostConstruct
public void init() {
}
public Node getRoot() {
return root;
}
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Run Code Online (Sandbox Code Playgroud)
主类FxBootApplication:
@SpringBootApplication
public class FxBootApplication extends Application {
private …Run Code Online (Sandbox Code Playgroud) 我有两个场景Login.fxml和MainView.fxml以及两个不同的控制器LoginController.java和MainViewControler.java
在LoginController中,我执行整个登录过程并获取JSessionID的值并将其存储在对象中,如下所示:
loginGateway = loginGateway(gateway);
Run Code Online (Sandbox Code Playgroud)
现在在MainViewController中我需要使用this对象(loginGateway)来获取getJSessionID并向服务器发出其他请求.但是如何在另一个Controller类(MainViewController.java)中访问此对象?
可以将两个FXML(JavaFX)文件连接到一个控制器吗?我不能通过在每个FXML文件中更改"fx:controller"来做到这一点......
有任何想法吗?
我想提前道歉,正如前面在“将参数直接从调用者传递给控制器”中讨论的那样,但是我遵循了发现的所有可能的解决方案,但仍然无法使它起作用。
我很难将参数从一个控制器传递到另一个控制器。
特别:
LoginController传递username给MainController。
当我单击登录按钮LoginController时,将其username设置为MainController。但是,加载Main.fxml时username为NULL。
在试图解决这个问题时,我想问一下:
initialize()方法?我想在LoginController调用的时候st.show();?username为NULL,因为我们已经使用Login在LoginController中设置了它的值mainController.setUsername(username)?任何帮助,将不胜感激。
这是我的代码。
LoginController.java
public class LoginController implements Initializable {
...
@FXML TextField username;
@FXML public void actionLoginButton() {
...
Stage st = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
Region root = (Region) loader.load();
Scene scene = new Scene(root);
st.setScene(scene);
MainController …Run Code Online (Sandbox Code Playgroud) 在我的程序中,控制器只是用一个函数挂钩按键。那么应该 ikeep 引用它吗?例如保持参考
Model model = new Model();
View view = new View(model);
Controller controller = new Controller(model,view);
Run Code Online (Sandbox Code Playgroud)
或没有
Model model = new Model();
View view = new View(model);
new Controller(model,view);
Run Code Online (Sandbox Code Playgroud)
内部控制器
public Controller(Model model, View view)
{
this.model = model;
this.view = view;
view.setOnKeyPressed(this::doSomething);
}
public void doSomething(KeyEvent event)
{
System.out.println("key pressed");
}
Run Code Online (Sandbox Code Playgroud)
也许我错误地实现了 Controller 类并且误解了 mvc 模式。但是到目前为止我写的内容没有理由让我保留对控制器对象的引用。
我在 JavaFX 中的 TableView 控件方面遇到了很大的问题。这是我第一次使用它,我不知道我做错了什么。
我的代码:
主要类别:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器类:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
public class Controller { …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个具有登录功能的库,并且我需要知道哪些用户在我的所有场景中登录。
我正在尝试通过 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 …Run Code Online (Sandbox Code Playgroud) 我的javaFx应用程序出了问题.每次我调用Label的.setText()方法时,都会出现NullPointerException.这是我的代码:这是我的控制器的一个小部件:
public class HomenizerController implements Initializable{
//ArrayList zum abspeichern der Termine in Listen Form
private ArrayList<Date> dateList = new ArrayList();
//ArrayList zum ab speichern der Aufgaben in Listen Form
private ArrayList<ToDo> toDoList = new ArrayList();
private Properties prop = null;
@FXML private Label username;
private void setWelcomeTab(){
username.setText("A");
}
private void loadProperties(String path){
FileInputStream fis = null;
try {
prop = new Properties();
fis = new FileInputStream("src/homenizer/profiles/"+path+"/config.properties");
prop.load(fis);
} catch (FileNotFoundException ex) {
Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) …Run Code Online (Sandbox Code Playgroud) 有可能从Node获取Controller实例吗?例如Tab上的AnchorPane?
我有一些AnchorPanes,我加载不同的控制器,我想验证哪个控制器已经加载
我想创建一个基于控制器的 JavaFX GUI,由多个控制器组成。
我无法完成的任务是将参数从一个场景传递到另一个场景并返回。
或者换句话说:MainController 加载 SubController 的 fxml,将一个对象传递给 SubController,切换场景。不应有两个敞开的窗户。完成工作后,SubController 应将场景切换回 MainController 并将一些对象传回。这是我失败的地方。
这个问题与这个问题非常相似,但仍然没有答案。传递参数 JavaFX FXML 评论中也提到了:
“当您将参数从第一个控制器传递到第二个控制器但如何将参数从第二个控制器传递到第一个控制器时,这项工作是在加载 first.fxml 之后。
– Xlint Xms 2017 年 9 月 18 日 23:15"
我在该线程的最佳答案中使用了第一种方法。
有没有人知道如何在没有外部库的情况下实现这一目标?
javafx ×8
java ×5
controller ×3
fxml ×3
javafx-2 ×3
controllers ×1
label ×1
null ×1
spring ×1
spring-boot ×1
tableview ×1