我必须将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;
}
}
答案 0 :(得分:2)
如果IPView
是HBox
的子类,则需要将按钮添加到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
的访问权限:< / p>
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);
一般来说,我更喜欢第二种方法,但这只是个人偏好的问题。