gpt4 book ai didi

java - 在 fxml 文件之间切换

转载 作者:行者123 更新时间:2023-12-01 12:31:40 25 4
gpt4 key购买 nike

我在 swing 组件内使用 jfxPanel 创建了一个应用程序。我面临的问题是我无法更改 fxml 文件。当单击 fxml 的按钮时,我想处理该 fxml 并在那里加载另一个 fxml 文件。这就是我到目前为止所做的

public class NonResponsiveButtons extends JFrame {
NonResponsiveButtons nrb;
BottomPanelIncomingController bpic;
JPanel panel;
JPanel bPanel;

private int applicationWidth_600 = 600;
private int applicationHeight_600 = 600;

private int upperPanelHeight_535 = 535;
private int bottomPanelHeight_65 = (applicationHeight_600-upperPanelHeight_535);


public static void main(String[] args) {
new NonResponsiveButtons();
}



public NonResponsiveButtons(){
final JFXPanel fxPanel = new JFXPanel();

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try{
new JFXPanel();
add(getJPanel(), BorderLayout.CENTER);
add(getJBottomPanel(), BorderLayout.PAGE_END);
bPanel.add(fxPanel, BorderLayout.CENTER);

Platform.runLater((new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
initFX(fxPanel);
}
})) ;

}
catch(Exception e){
System.out.println("Error in swing utilities thread :" + e.getMessage());
}

}
});

this.setSize(applicationWidth_600, applicationHeight_600);
setLocationRelativeTo(null);
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}


private void initFX(JFXPanel jfxPanel) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxmlDesign.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root, 600, 65);
jfxPanel.setScene(scene);

fxmlLoader.getController();
bpic = new BottomPanelIncomingController();
bpic.setNonResoinsiveButtons(this);

} catch (IOException exc) {
exc.printStackTrace();
System.exit(1);
}
}

public void loadSecondFxml(){
System.out.println("loading second fxml");
}

private JPanel getJPanel(){
if(panel == null){
panel = new JPanel();
panel.setSize(applicationWidth_600,upperPanelHeight_535);
panel.setBackground(Color.gray);
}
return panel;
}

private JPanel getJBottomPanel(){
if(bPanel == null){
bPanel = new JPanel();
bPanel.setSize(applicationWidth_600, bottomPanelHeight_65);
bPanel.setBackground(new Color(8, 16, 19));
}
return bPanel;
}

}

BottomPanelIncommingController 类。 fxml Controller

public class BottomPanelIncomingController implements Initializable {
NonResponsiveButtons nrb;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
System.out.println("HEllo");
}

public void setNonResoinsiveButtons(NonResponsiveButtons nrb){
this.nrb = nrb;
}

@FXML
public void callAccepted(ActionEvent event){
System.out.println("From controller");
nrb.loadSecondFxml();
}
}

最佳答案

当您使用 FXML 时,切勿使用 new 实例化 Controller ,因为实例化 Controller 的工作是由 FXMLLoader 完成的。在实例化 Controller 时,它还会创建 FXML 中的节点实例并将它们注入(inject)到 Controller 中。

如果您没有从 FMXLLoader 获取 Controller 实例,则 Controller 内所有用 @FXML 注解的节点都将为 null。因此,您必须始终从 fxml 中获取 Controller 。

根据您的情况,您应该使用

bpic = fxmlLoader.getController();

而不是

bpic = new BottomPanelIncomingController();

更新

单击按钮即可更改 FXML

让我们考虑在单击按钮时调用以下方法

@FXML
public void callAccepted(ActionEvent event){
System.out.println("From controller");
nrb.loadSecondFxml();
}

您可以加载 FXML 并将其设置在场景上,然后设置到 JFXPanel

public void loadSecondFxml(){
//Load new FXML and assign it to scene
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newFXML.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root, 600, 65);
jfxPanel.setScene(scene);
}

注意:我不确定您想在这里实现什么,请将此视为一个示例,单击按钮即可加载 FXML 并应用您的逻辑。

关于java - 在 fxml 文件之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com