Javafx中GridPane内的HBox

Javafx中GridPane内的HBox,java,javafx,Java,Javafx,我必须将HBox添加到GridPane。如果我在同一个类中将HBox添加到GridPane,则系统显示正确。但是当我尝试使用两个类时,只会显示空窗口。我是javafx新手。我该怎么做请帮助我谢谢 public class IpCamMainWindow extends Application{ private static ArrayList<IpCamViewer> ipCameraList = new ArrayList<IpCamViewer>();

我必须将HBox添加到GridPane。如果我在同一个类中将HBox添加到GridPane,则系统显示正确。但是当我尝试使用两个类时,只会显示空窗口。我是javafx新手。我该怎么做请帮助我谢谢

public class IpCamMainWindow  extends Application{

    private static ArrayList<IpCamViewer> ipCameraList = new ArrayList<IpCamViewer>();
    private static ArrayList<String> urls= new ArrayList<String>();
    GridPane grid =null;

    private ImageView imgWebCamCapturedImage;
    private BufferedImage grabbedImage;
    private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();
    private Webcam webCam = null;
    private boolean stopCamera = false;
    IPview ipCamViewer=null;

    public static void main(String[] args) {


        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(0, 10, 0, 10));      

        for(int i=0;i<4;i++){
            ipCamViewer = new IPview();     

            grid.add(ipCamViewer, i%2, i/2);
            System.out.println("column: " + i%2 + ", row: " + i/2);

        }

        Scene scene = new Scene(grid);
        stage.setScene(scene);
        stage.setTitle("IP Camera Solution");
        stage.show();       

    }   

}
公共类IpCamMainWindow扩展应用程序{
私有静态ArrayList ipCameraList=新ArrayList();
私有静态ArrayList URL=新ArrayList();
GridPane grid=null;
私有图像查看imgWebCamCapturedImage;
私有缓存图像抓取图像;
private ObjectProperty imageProperty=新的SimpleObject属性();
私人网络摄像头=空;
私有布尔stopCamera=false;
IPview ipCamViewer=null;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage)引发异常{
grid=新的GridPane();
网格。setHgap(10);
网格设置间隙(10);
设置填充(新的插入(0,10,0,10));

对于(int i=0;i如果
IPView
HBox
的子类,则需要将按钮添加到
IPView
实例中,而不是创建另一个
HBox
作为其成员变量

然后在你的应用程序课上

        ipCamViewer = new IPview();     
        grid.add(ipCamViewer.getView(), i%2, i/2);

一般来说,我更喜欢第二种方法,但这只是个人偏好的问题。

您需要确定
IPView
是否是
HBox
(继承:
public class IPView扩展HBox
),或者
IPView
是否有
HBox
(聚合:
HBox
)。现在您拥有这两个按钮,您将按钮添加到聚合的
HBox
,但将
IPView
本身(不包含按钮)添加到
GridPane
。我需要使用继承(公共类IPView扩展HBox)然后要将HBox添加到Gridpane。因此不要在
IPView
中创建另一个
HBox
作为字段
public class IPview extends HBox {  

    private ImageView imgWebCamCapturedImage;
    private BufferedImage grabbedImage;
    private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();

    public IPview(){

        this.setPadding(new Insets(15, 12, 15, 12));
        this.setSpacing(10);
        this.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);
        this.getChildren().addAll(buttonCurrent, buttonProjected);

    }   

}
public class IPview {  

    private ImageView imgWebCamCapturedImage;
    private BufferedImage grabbedImage;
    private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();
    private HBox hbox;

    public IPview(){

        hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);
        hbox.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);
        hbox.getChildren().addAll(buttonCurrent, buttonProjected);

    }   

    public Node getView() {
        return hbox ; 
    }

}
        ipCamViewer = new IPview();     
        grid.add(ipCamViewer.getView(), i%2, i/2);