当前位置: 首页 > 知识库问答 >
问题:

如何确定javafx vbox父类

魏航
2023-03-14

我是javaFX的新手,我正在开发一个javaFX应用程序。我得到的错误是我无法从fxml文件中提取子节点,fxml文件如下所示

<children>
    <VBox fx:id="sideMenuVBox" alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="600.0" minWidth="125.0">
        <children>
        <Button fx:id="headingButton" maxWidth="1.7976931348623157E308" onAction="#helpButtonOnAction" text="Masters" />
            <Button fx:id="openCompanyButton" maxWidth="1.7976931348623157E308" onAction="#openCompanyButtonOnAction" text="Open Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="newCompanyButton" maxWidth="1.7976931348623157E308" onAction="#newCompanyButtonOnAction" text="New Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="closeCompanyButton" maxWidth="1.7976931348623157E308" onAction="#closeCompanyButtonOnAction" text="Close Company" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="chartOfAccountsButton" maxWidth="1.7976931348623157E308" onAction="#chartOfAccountsButtonOnAction" text="Chart of Accounts" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="newUserButton" maxWidth="1.7976931348623157E308" onAction="#newUserButtonOnAction" text="New User" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="userSetUpButton" maxWidth="1.7976931348623157E308" onAction="#userSetUpButtonOnAction" text="User SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="unitMasterButton" maxWidth="1.7976931348623157E308" onAction="#unitMasterButtonOnAction" text="Unit Master" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="itemMasterButton" maxWidth="1.7976931348623157E308" onAction="#itemMasterButtonOnAction" text="Item Master" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="narrationSetUpButton" maxWidth="1.7976931348623157E308" onAction="#narrationSetUpButtonOnAction" text="Narration SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="miscellaneousSetUpButton" maxWidth="1.7976931348623157E308" onAction="#miscellaneousSetUpButtonOnAction" text="Miscellaneous SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="numberingSetUpButton" maxWidth="1.7976931348623157E308" onAction="#numberingSetUpButtonOnAction" text="Numbering SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="sundrySetUpButton" maxWidth="1.7976931348623157E308" onAction="#sundrySetUpButtonOnAction" text="Sundry SetUp" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="inventoryFlowButton" maxWidth="1.7976931348623157E308" onAction="#inventoryFlowButtonOnAction" text="Inventory Flow" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="challanTypeButton" maxWidth="1.7976931348623157E308" onAction="#challanTypeButtonOnAction" text="Challan Type" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="mergeLedgersButton" maxWidth="1.7976931348623157E308" onAction="#mergeLedgersButtonOnAction" text="Merge Ledgers" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="transferOpeningBalanceButton" maxWidth="1.7976931348623157E308" onAction="#transferOpeningBalanceButtonOnAction" text="Transfer Opening Balance" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="backUpRestoreButton" maxWidth="1.7976931348623157E308" onAction="#backUpRestoreButtonOnAction" text="Back Up &amp; Restore" textAlignment="CENTER" wrapText="true" />
            <Button fx:id="helpButton" maxWidth="1.7976931348623157E308" onAction="#helpButtonOnAction" text="Help" textAlignment="CENTER" wrapText="true" />
        </children>
    </VBox>
</children>
</AnchorPane>
@FXML
private Label titleOfPage;
@FXML
private ComboBox<String> combo;
@FXML
private VBox vbox;
@FXML
private AnchorPane anchor;

private List<String> mastersList;
private List<String> addressBookList;
private List<String> vouchersList;
private List<String> salesPurchaseList;
private List<String> financialRegistersList;
private List<String> inventoryRegistersList;
private List<String> finalReportsList;
private List<String> misReportsList;
@FXML
private AnchorPane content;


public void comboOnAction()
{   

    String selectedItem=this.combo.getSelectionModel().getSelectedItem();
    List<String> resultantList;
    if(selectedItem.equals("Masters"))
    {

        resultantList=this.mastersList;
        this.vbox.getChildren().clear();

        show(resultantList);

    }

    else if(selectedItem.equals("Address Book"))
    {   
        resultantList=this.addressBookList;
        this.vbox.getChildren().clear();

        show(resultantList);
    }

    else if(selectedItem.equals("Vouchers"))
    {
        resultantList=this.vouchersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Sales Purchase"))
    {
        resultantList=this.salesPurchaseList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Financial Registers"))
    {
        resultantList=this.financialRegistersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Inventory Registors"))
    {
        resultantList=this.inventoryRegistersList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }

    else if(selectedItem.equals("Final Reports"))
    {
        resultantList=this.finalReportsList;
        this.vbox.getChildren().clear();
        show(resultantList);

    }

    else if(selectedItem.equals("MIS Reports"))
    {
        resultantList=this.misReportsList;
        this.vbox.getChildren().clear();
        show(resultantList);
    }


}

public void show(List<String> menuTypeInString)
{
    ObservableList menuObservaleArrayList=
            FXCollections.observableArrayList(menuTypeInString);
    for(int i=0;i<menuObservaleArrayList.size();i++)
    {
        HBox hbox=new HBox();
        Label dynamicListLabel=new Label();
    //  dynamicListLabel.setMaxSize(100,10);
        dynamicListLabel.setText(menuObservaleArrayList.get(i).toString());
        TextField newNameTextField=new TextField();
    //  newNameTextField.setMaxSize(100,10);
        Button makeChangeButton=new Button();
        makeChangeButton.setText("Change Name");
        makeChangeButton.setOnAction(new EventHandler<ActionEvent>() 
                {
                    @Override
                    public void handle(ActionEvent event)
                    {   
                        String ButtonName=dynamicListLabel.toString();
                        String inputButtonName = newNameTextField.getText();
                        System.out.println(getClass().getResource(""));

                        try {
                            content=FXMLLoader.load(getClass().getResource
                                    ("/com/openlx/kalculate/views/masters/SideMenu.fxml"));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                //  AnchorPane  childHolder;
                //  childHolder.getChildren().addAll(content);

                //content.getChildrenUnmodifiable().listIterator(i).toString();
                    System.out.println(content.getChildren().toString());   
            //      ((Pane) content.getChildren()).getChildren();
                    System.out.println(((Pane) content.getChildren()).getChildren().toString());

                /*  content.getChildren().clear();
                    content.getChildren().addAll();
                    Button btn=(Button) content.lookup(ButtonName);
                    hbox.getChildren().add(dynamicListLabel);
                    hbox.getChildren().add(newNameTextField);
                    hbox.getChildren().add(makeChangeButton);
                    hbox.getChildren().add(btn);

                    */
                    }
                });
        hbox.getChildren().add(dynamicListLabel);
        hbox.getChildren().add(newNameTextField);
        hbox.getChildren().add(makeChangeButton);
        vbox.getChildren().add(hbox);

        if(newNameTextField.getText()!=null)
        {

        }
    }
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

List<String> mainList=new ArrayList<String>();
    mainList.add("Masters");
    mainList.add("Address Book");
    mainList.add("Vouchers");
    mainList.add("Sales Purchase");
    mainList.add("Financial Registers");
    mainList.add("Inventory Registers");
    mainList.add("Final Reports");
    mainList.add("MIS Reports");

ObservableList menuObservaleArrayList=
    FXCollections.observableArrayList(mainList);
combo.setItems(menuObservaleArrayList);

    mastersList=new ArrayList<String>();
    mastersList.add("Open Company");
    mastersList.add("New Company");
    mastersList.add("Close Company");
    mastersList.add("Charts Of Accounts");
    mastersList.add("New User");
    mastersList.add("User Setup");
    mastersList.add("Unit Master");
    mastersList.add("Item Master");
    mastersList.add("Narration Setup");
    mastersList.add("Miscellaneous Setup");
    mastersList.add("Numbering Setup");
    mastersList.add("Sundry Setup");
    mastersList.add("Inventory Flow");
    mastersList.add("Challan Type");
    mastersList.add("Merge Ledgers");
    mastersList.add("Transfering Opening Balance");
    mastersList.add("Back Up & Restore");
    mastersList.add("Help");

addressBookList=new ArrayList<String>();
    addressBookList.add("Show Address Book");
    addressBookList.add("Supplier Entry");
    addressBookList.add("Customer Entry");
    addressBookList.add("Help");

vouchersList=new ArrayList<String>();
    vouchersList.add("Cash Reciept");
    vouchersList.add("Cash Payement");
    vouchersList.add("Bank Reciept");
    vouchersList.add("Bank Payement");
    vouchersList.add("Journal Voucher");
    vouchersList.add("Journal Sales");
    vouchersList.add("Journal Purchase");
    vouchersList.add("Debit Note");
    vouchersList.add("Credit Note");
    vouchersList.add("Contra Entry");
    vouchersList.add("Help");

salesPurchaseList=new ArrayList<String>();
    salesPurchaseList.add("Quotation");
    salesPurchaseList.add("Order Acknowledgement");
    salesPurchaseList.add("Challan Cum Sales Invoice");
    salesPurchaseList.add("Challan");
    salesPurchaseList.add("Sales Invoice");
    salesPurchaseList.add("Sales Return");
    salesPurchaseList.add("Enquiry");
    salesPurchaseList.add("Purchase Order");
    salesPurchaseList.add("Purchase Invoice");
    salesPurchaseList.add("Purchase Return");
    salesPurchaseList.add("GRN Cum Purchase Invoice");
    salesPurchaseList.add("Goods Reciept Note");
    salesPurchaseList.add("Process Existing Voucher");
    salesPurchaseList.add("Stock Adjustment Voucher(Add)");
    salesPurchaseList.add("Stock Adjustment Voucher(Deduct)");
    salesPurchaseList.add("Help");

financialRegistersList=new ArrayList<String>();
    financialRegistersList.add("Cash book");
    financialRegistersList.add("Cash Reciept Book");
    financialRegistersList.add("Cash Payement Book");
    financialRegistersList.add("Bank Book");
    financialRegistersList.add("Bank Payement Book");
    financialRegistersList.add("Bank Reciept Book");
    financialRegistersList.add("Day Book");
    financialRegistersList.add("Ledger");
    financialRegistersList.add("Journal Book");
    financialRegistersList.add("Debit Note Book");
    financialRegistersList.add("Credit Note Book");
    financialRegistersList.add("Cheque Book");
    financialRegistersList.add("Customer Book");
    financialRegistersList.add("Supplier Book");
    financialRegistersList.add("Help");

inventoryRegistersList=new ArrayList<String>();
    inventoryRegistersList.add("Quotation Registers");
    inventoryRegistersList.add("Order Acknowledgement Registers");
    inventoryRegistersList.add("Challan Registers");
    inventoryRegistersList.add("Invoice Registers");
    inventoryRegistersList.add("Sales Return Registers");
    inventoryRegistersList.add("Sales Registers");
    inventoryRegistersList.add("Sales Summary");
    inventoryRegistersList.add("Enquiry Registers");
    inventoryRegistersList.add("Purchase Order Registers");
    inventoryRegistersList.add("Purchase Invoice  Registers");
    inventoryRegistersList.add("Purchase Return Registers");
    inventoryRegistersList.add("Goods Reciept Note Registers");
    inventoryRegistersList.add("Purchase Registers");
    inventoryRegistersList.add("Purchase Summary");
    inventoryRegistersList.add("Stock Registers");
    inventoryRegistersList.add("Stock Movement");
    inventoryRegistersList.add("Stock Adjustment Details");
    inventoryRegistersList.add("Custom Stock BAlance");
    inventoryRegistersList.add("Help");

finalReportsList=new ArrayList<String>();
    finalReportsList.add("Trial Balance");
    finalReportsList.add("Income And Expenditure A/C");
    finalReportsList.add("Profit And Loss A/C");
    finalReportsList.add("Balance Sheet A/C");
    finalReportsList.add("Help");

misReportsList=new ArrayList<String>();
    misReportsList.add("Group Summary");
    misReportsList.add("Statement Of Debtors With Ageing");
    misReportsList.add("Statement Of Creditors With AGeing");
    misReportsList.add("Sundry Ledgers");
    misReportsList.add("Account Status Confirmation");
    misReportsList.add("Payement Reminders");
    misReportsList.add("Negative Cash Warning");
    misReportsList.add("Depraction Statement");
    misReportsList.add("Item Ladger");
    misReportsList.add("Re-Order Level Report");
    misReportsList.add("Rate List");
    misReportsList.add("Customer Ledger");
    misReportsList.add("Supplier Ledger");
    misReportsList.add("Bank Reconciliation");
    misReportsList.add("Help");

}

}

这个部分基本上是错误的,

                       @Override
                        public void handle(ActionEvent event)
                        {   
                            String ButtonName=dynamicListLabel.toString();
                            String inputButtonName = newNameTextField.getText();
                            System.out.println(getClass().getResource(""));

                            try {
                                content=FXMLLoader.load(getClass().getResource
                                        ("/com/openlx/kalculate/views/masters/SideMenu.fxml"));
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    //  AnchorPane  childHolder;
                    //  childHolder.getChildren().addAll(content);

                    //content.getChildrenUnmodifiable().listIterator(i).toString();
                        System.out.println(content.getChildren().toString());   
                //      ((Pane) content.getChildren()).getChildren();
                        System.out.println(((Pane) content.getChildren()).getChildren().toString());

                    /*  content.getChildren().clear();
                        content.getChildren().addAll();
                        Button btn=(Button) content.lookup(ButtonName);
                        hbox.getChildren().add(dynamicListLabel);
                        hbox.getChildren().add(newNameTextField);
                        hbox.getChildren().add(makeChangeButton);
                        hbox.getChildren().add(btn);

                        */
                        }
                    });
            hbox.getChildren().add(dynamicListLabel);
            hbox.getChildren().add(newNameTextField);
            hbox.getChildren().add(makeChangeButton);
            vbox.getChildren().add(hbox);

我尝试了很多方法,但它只是说不能将父级$2转换为javafx.scene(any)。

共有1个答案

慕容渊
2023-03-14

您的content变量是一个anchorpane,因此

((Pane) content.getChildren()).getChildren();

是错误的,因为content.getChildren()返回子列表(observableList ),并且不能将其强制转换为pane

带有fx:id=“SideMenuvBox”的VBox是AnchorPane的唯一子级。您可以将其作为

VBox vb = (VBox) content.getChildren().get(0);

那么您可以通过以下方法获取vbox的按钮子级

vb.getChildren();

但是,FXML文件中的所有控件都定义了fx:id属性。您可以使用这些ID直接注入和访问它们。有关更多信息,请研究fx:id的用途。

 类似资料:
  • 高尔夫球场示例:我们有类,,和。 这里有很多为人父母的事情。 必须由18条赛道组成,这在高尔夫球场是很正常的 但是我遇到了一个问题。当我尝试调用的构造函数时... ... 我必须同时通过和的实例,但是。。。 ... 这些函数的构造函数要求我传递一个的实例。我陷入了一个递归问题。 我可以通过不再让每个成为这些类的成员来“解决”这一问题,但这让人感觉很不舒服,就像糟糕的做法一样-没有理由不让它们成为f

  • 一个原则:尽量精度高,但不能溢出。 若变量的最大值为 |max|,2^{n-1}< |max| < 2^{n}。我们 dsp 是 32bit ,因此最大的Q = 31 - n。 要求对输入的数据集的范围做到心中有数。

  • 一个原则:尽量精度高,但不能溢出。 若变量的最大值为 |max|,2^{n-1}< |max| < 2^{n}。我们 dsp 是 32bit ,因此最大的Q = 31 - n。 要求对输入的数据集的范围做到心中有数。

  • 问题内容: 如果class 和class 扩展了class 并且我有一个对象类型或,我如何确定它是实例的哪种类型? 问题答案:

  • 问题内容: 我正在创建一个通用类,并且需要使用一种方法来了解当前使用的通用类型的Class。原因是我调用的方法之一期望将此作为参数。 例: 显然,上面的示例不起作用,并导致以下错误:类型参数T的非法类文字。 我的问题是:有人知道这方面有很好的选择或解决方法吗? 问题答案: 仍然存在相同的问题:常规信息在运行时被删除,无法恢复。一种解决方法是在静态方法的参数中传递T类: 很难看,但是可以用。

  • 问题内容: 为什么Java不需要导入诸如Integer,String等的类,而又需要导入其他类呢? 问题答案: 存在的隐式导入。 根据Java规范: 编译单元可以自动访问其程序包中声明的所有类型,还可以自动导入预定义程序包中声明的所有公共类型。