在javafx中布置我的复选框的最佳方法是什么?

时间:2015-04-04 23:18:05

标签: java css3 checkbox javafx

enter image description here

我正在尝试使用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);

  }




   }

1 个答案:

答案 0 :(得分:1)

您没有初始化peClassesGridPane。只需添加一行

即可

peClassesGridPane = new GridPane();

使用之前。此外,请完全删除代码中的多余peVBox1peVBox2

相关问题