[英]Switch between fxml files
我在 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 控制器
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
實例化控制器,因為實例化控制器的工作是由 FXMLLoader 完成的。 在實例化控制器的同時,它還創建 FXML 中節點的實例並將它們注入控制器。
如果您沒有從 FMXLLoader 獲取控制器實例,則控制器內所有用@FXML
注釋的節點都為空。 因此,您必須始終從 fxml 中取出 Controller。
在你的情況下,你應該使用
bpic = fxmlLoader.getController();
代替
bpic = new BottomPanelIncomingController();
更新
單擊按鈕更改 FXML
讓我們考慮在Button點擊時調用以下方法
@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並應用您的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.