繁体   English   中英

Javafx中GridPane内的HBox

[英]HBox inside GridPane in Javafx

我必须将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();       

    }   

}

-

public class IPview extends HBox {  

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

    public IPview(){

        HBox hbox=addHBox();

    }

    public HBox addHBox() {
        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);

        return hbox;
    }   

}

如果IPViewHBox的子类,则需要将按钮添加到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);

    }   

}

如果希望HBox成为成员变量,则不会使IPView成为HBox的子类,而只提供对HBox访问:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM