簡體   English   中英

在javafx中布局復選框的最佳方法是什么?

[英]what is the best way to layout my checkboxes in javafx?

在此處輸入圖片說明

我正在嘗試使用css和javafx來實現此設計,到目前為止,我認為即時消息正處於正確的軌道上,但我所堅持的是復選框。我似乎無法正確地布局它們並按我希望它們的方式運行。 我在兩個vbox的每一個中放了4個復選框,並將它們都放在同一個單元格中,以適合樣式表中設置的一個邊框,但是當我這樣做時,復選框的第二列才起作用。 我嘗試了其他選擇,例如使用第二個gridPane並在其中放置復選框,然后將第二個gridPane放到我為場景設置的gridPane中,但是它甚至無法運行,所以我的問題是最好的方法是什么布置我的復選框,使它們看起來像這樣並正常運行? 這是我到目前為止所擁有的

 import javafx.scene.*;
 import javafx.stage.*;
 import javafx.geometry.*;
 import javafx.application.*;
 import javafx.scene.control.*;
 import javafx.scene.layout.*;
 import javafx.collections.*;
 import javafx.event.*;
 import javafx.scene.text.TextAlignment;

   public class ClassRegistrationForm extends Application{

    Scene scene1;

  Button createRequestButton,clearButton;

   Label peLabel,mathLabel,electivesLabel,englishLabel,spaceLabel;

   RadioButton english12,english11,english10,english9;
   ToggleGroup group;

 ComboBox<String>electivesComboBox;

 CheckBox healthBox,sportsBox,liftingBox,aerobicsBox,
 archeryBox,swimmingBox,yogaBox,bowlingBox;

    HBox buttonHbox;
    VBox englishVbox,mathVbox,electivesVbox,peVbox1,peVbox2;

    GridPane peClassesGridPane;

     ListView<String> mathClassesListView;
     ObservableList<String> mathClassesList;


public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Class Registration Application");
    primaryStage.setResizable(false);
    primaryStage.sizeToScene();




        GridPane gridPane = new GridPane();
        peClassesGridPane = new GridPane();
        scene1 = new Scene(gridPane);






       buttonHbox = new HBox();

       createRequestButton = new Button("Create Request");
       clearButton = new Button("Clear");
       buttonHbox.getChildren().addAll(createRequestButton,clearButton);
       gridPane.setConstraints(buttonHbox,0,2);


       peLabel = new Label("Pe"); 
       peClassesGridPane.setConstraints(peLabel,0,0);
       healthBox = new CheckBox("Health");
       peClassesGridPane.setConstraints(healthBox,0,1);
       yogaBox = new CheckBox("Yoga");
       peClassesGridPane.setConstraints(yogaBox,0,2);
       sportsBox = new CheckBox("Sports");
       peClassesGridPane.setConstraints(sportsBox,0,3);
       archeryBox = new CheckBox("Archery");
       peClassesGridPane.setConstraints(archeryBox,0,4);

       spaceLabel = new Label("");
       peClassesGridPane.setConstraints(spaceLabel,1,0);
       liftingBox = new CheckBox("Lift");
       peClassesGridPane.setConstraints(liftingBox,1,1);
       swimmingBox = new CheckBox("Swim");
       peClassesGridPane.setConstraints(swimmingBox,1,2);
       aerobicsBox = new CheckBox("Aero");
       peClassesGridPane.setConstraints(aerobicsBox,1,3);
       bowlingBox = new CheckBox("Bowl");
       peClassesGridPane.setConstraints(bowlingBox,1,4);
       peClassesGridPane.getChildren().addAll(
       peLabel,healthBox,yogaBox,sportsBox,archeryBox,spaceLabel,
        liftingBox,swimmingBox,aerobicsBox,bowlingBox
       );  

       gridPane.setConstraints(peClassesGridPane,0,1);



       mathVbox = new VBox();
       mathVbox.setAlignment(Pos.CENTER);
       mathVbox.setPrefSize(150,100);
       mathClassesListView = new ListView();
      mathClassesListView.setPrefSize(100,50);
       mathClassesList = FXCollections.observableArrayList(
                "Algebra 1-2",
                "Algebra 3-4",
                "Geometry",
                "Pre-Calculus",
                "Calculus"
         );
        mathClassesListView.setItems(mathClassesList);



       mathLabel = new Label("Math Classes");
       mathVbox.getChildren().addAll(mathLabel,mathClassesListView);
       gridPane.setConstraints(mathVbox,1,1);




       englishVbox = new VBox();
      englishVbox.setPrefSize(200, 200);
       englishVbox.setAlignment(Pos.CENTER);
       group  = new ToggleGroup();

    englishLabel = new Label("English Classes");
        englishVbox.getChildren().add(englishLabel);
    english12 = new RadioButton("English12");
    english12.setToggleGroup(group);
        englishVbox.getChildren().add(english12);
    english11 = new RadioButton("English11");
    english11.setToggleGroup(group);
        englishVbox.getChildren().add(english11);
    english10 = new RadioButton("English12");
    english10.setToggleGroup(group);
    englishVbox.getChildren().add(english10);
    english9 = new RadioButton("English9");
        english9.setToggleGroup(group);
    englishVbox.getChildren().add(english9);
    gridPane.setConstraints(englishVbox,0,0);

        electivesVbox = new VBox();
        electivesVbox.setAlignment(Pos.CENTER);
    electivesLabel = new Label("Electives");


    ObservableList<String> data = FXCollections.observableArrayList("Java"
            , "Web Design"
            , "Welding"
            , "Woods"
            , "Art"
            , "Band"
            , "GameDesign"
            , "Graphic Arts");
    electivesComboBox = new ComboBox<String>();
    electivesComboBox.setItems(data);

           electivesVbox.getChildren().addAll(
  electivesLabel,electivesComboBox
 );

       gridPane.setConstraints(electivesVbox,0,1);   



          gridPane.getChildren().addAll(
  englishVbox,electivesVbox,peVbox1,peVbox2,mathVbox,
  buttonHbox,peClassesGridPane
 );

    primaryStage.setScene(scene1);
    primaryStage.show();
}



public static void main(String[] args) {

    launch(args);

  }




   }

您不初始化peClassesGridPane 只需添加行

peClassesGridPane = new GridPane();

在使用之前。 另外,從代碼中完全刪除冗余peVBox1peVBox2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM