简体   繁体   English

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

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

在此处输入图片说明

I am trying to achieve this design using css and javafx and so far i think im on the right track, but where i am stuck at is the check boxes.I cant seem to get them to layout correctly and functioning the way i want them to. 我正在尝试使用css和javafx来实现此设计,到目前为止,我认为即时消息正处于正确的轨道上,但我所坚持的是复选框。我似乎无法正确地布局它们并按我希望它们的方式运行。 I put 4 check Boxes in each of the two vboxs and put them both in the same cell to be able fit in one border that i set in the style sheet,but when i do this only the second column of check boxes work. 我在两个vbox的每一个中放了4个复选框,并将它们都放在同一个单元格中,以适合样式表中设置的一个边框,但是当我这样做时,复选框的第二列才起作用。 I have tried other options such as using a second gridPane and laying out the check boxes in there then putting the second gridPane in the gridPane that i set for the scene, but that wouldn't even run so my question is what is the best way to layout my check boxes so they look like this and are functioning properly? 我尝试了其他选择,例如使用第二个gridPane并在其中放置复选框,然后将第二个gridPane放到我为场景设置的gridPane中,但是它甚至无法运行,所以我的问题是最好的方法是什么布置我的复选框,使它们看起来像这样并正常运行? Here is what i have so far 这是我到目前为止所拥有的

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

  }




   }

You don't initialize peClassesGridPane . 您不初始化peClassesGridPane Just add in the line 只需添加行

peClassesGridPane = new GridPane();

before you use it. 在使用之前。 Additionally, remove the redundant peVBox1 and peVBox2 from the code entirely. 另外,从代码中完全删除冗余peVBox1peVBox2

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

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