gpt4 book ai didi

java - Javafx 中 GridPane 内的 HBox

转载 作者:行者123 更新时间:2023-11-30 03:37:20 26 4
gpt4 key购买 nike

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

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

关于java - Javafx 中 GridPane 内的 HBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com