如何使用GridPane的fx:id通过controller.java文件向JavaFX gui添加按钮

时间:2016-10-17 23:14:15

标签: java javafx

对JavaFX来说还是比较新的,我在添加按钮添加到我设置的GUI时遇到了一些麻烦。

我有3个文件:Main.java,Controller.java和sample.fxml(每个位于下面)

从我通过教程和文档阅读的内容,由于“fx:controller”部分,Controller.java文件被加载并链接到fxml文件 - 但除此之外我无法正确地运行该程序方法

我已经尝试将Main.java设置为控制器并以这种方式融合代码(被轰炸)。甚至主动反对该表单并尝试在Main.java文件中创建一个新的Controller.java实例(这实际上只是在程序运行时创建了另一个实例)并且太轰炸了。

任何指针?

Main.java

package soundboardEoZ;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {

    int sound_index = 1;
    int target = 10;

    @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();


        while (sound_index < target){
            // This is where I want to call the addButton() method from Controller.java
            sound_index++;
        }
    }

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

Controller.java

package soundboardEoZ;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class Controller {
    int index = 2;
    int target = 10;

    @FXML
    GridPane button_grid;

    void test(){
        System.out.println("Testing");
    }

    void addButton(){
        Button sound_button = new Button("Button_" + index);
        button_grid.add(sound_button, index,2);
    }

}

我们的FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<!--VBox is a single column panel-->

<?import javafx.scene.text.Font?>
<VBox id="vbox" prefHeight="400" prefWidth="1000" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="soundboardEoZ.Controller">
    <MenuBar fx:id="menuBar"> <!--Literally a MenuBar-->
        <menus> <!--Holds the menu concept for the bar-->
            <Menu text="File"> <!--Menu holds Items-->
                <items>
                    <MenuItem text="New" />
                    <MenuItem text="Open" />
                    <SeparatorMenuItem />
                    <MenuItem text="Save Settings" />
                    <SeparatorMenuItem />
                    <MenuItem text="Settings" />
                </items>
            </Menu>
            <Menu text="Edit">
                <items>
                    <MenuItem text="Copy" />
                </items>
            </Menu>
            <Menu text="About">
                <items>
                    <MenuItem text="Team" />
                </items>
            </Menu>
            <Menu text="Help">
                <items>
                    <MenuItem text="Guide" />
                    <MenuItem text="Forums" />
                </items>
            </Menu>
        </menus>
    </MenuBar>

    <Pane>
        <GridPane>
            <Button GridPane.columnIndex="0">I'm another Test</Button>
            <Text fill="RED" stroke="BLACK" strokeWidth="2.0" GridPane.columnIndex="1">
                <font><Font size="25"/></font>
                Testing
            </Text>
        </GridPane>
    </Pane>

    <GridPane fx:id="button_grid" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml">

        <!--<Button GridPane.columnIndex="0" GridPane.rowIndex="1">Hello!</Button>-->

        <!--<Button GridPane.columnIndex="1">Hiya!</Button>-->

        <!--<Text text="Clickity!" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2" />-->

        <!--<Label text="Configuration Name:" GridPane.columnIndex="0" GridPane.rowIndex="1" />-->
        <!--<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />-->

        <!--<Button fx:id="test_button" text="Hello"></Button>-->

    </GridPane>
</VBox>

1 个答案:

答案 0 :(得分:1)

示例中的Application子类 - Main - 实际上应该只是启动应用程序。即它应该加载FXML,将其放在窗口中,并显示窗口。 FXML定义的UI的初始化和其他配置以及事件处理应由控制器完成。因此,您并没有真正尝试从正确的位置执行此代码。您应该从控制器执行此操作:

package soundboardEoZ;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class Controller {
    int index ;
    int target = 10;

    @FXML
    GridPane button_grid;

    public void initialize() {
        for (index = 2; index < target ; index++) {
            addButton();
        }
    }

    public void test(){
        System.out.println("Testing");
    }

    public void addButton(){
        Button sound_button = new Button("Button_" + index);
        button_grid.add(sound_button, index,2);
    }

}

然后从Main中删除所有功能:

package soundboardEoZ;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
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);
    }
}

如果你真的想从Main调用该方法(但同样,我不能过分强调这实际上是以与设计模式相反的方式使用工具包),创建一个FXMLLoader实例并从中获取控制器。您需要控制器正确跟踪索引:

package soundboardEoZ;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class Controller {
    int index = 2;

    @FXML
    GridPane button_grid;

    public void test(){
        System.out.println("Testing");
    }

    public void addButton(){
        Button sound_button = new Button("Button_" + index);
        button_grid.add(sound_button, index,2);
        index++;
    }

}

然后你可以这样做:

package soundboardEoZ;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {

    int sound_index = 1;
    int target = 10;

    @Override
    public void start(Stage primaryStage) throws Exception{

        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
        Parent root = loader.load();

        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        Controller controller = loader.getController();

        while (sound_index < target){
            controller.addButton();
            sound_index++;
        }
    }

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