RadioButtons无法从javafx子类加载

时间:2016-11-17 10:32:09

标签: java javafx

尝试将另一个类的新场景加载到主javafx页面中,这是主要的javafx页面

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.layout.GridPane;
    import javafx.scene.control.Button;
    import javafx.scene.control.Hyperlink;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.FlowPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;

    public class Cbt extends Application {
      Stage stageCbt;
      Scene staffMainPage;
      BorderPane border;
      FlowPane addFlowPane;
      Hyperlink SQuestions, setParameters;

      public void start(Stage theMainStage){

            stageCbt = theMainStage;

            border = new BorderPane();

            SQuestions.setOnAction(ae -> createQuestionsLinkClicked(ae));
      }
      private FlowPane addFlowPaneLO() {

            addFlowPane = new FlowPane();
            addFlowPane.getStyleClass().addAll("pane", "vbox");

            Hyperlink options[] = new Hyperlink[]{
                setParameters = new Hyperlink("set Questions Parameters"),
                SQuestions = new Hyperlink("Select past jamb questions")
            };

            for (int i = 0; i < 2; i++) {
                addFlowPane.setMargin(options[i], new Insets(0, 0, 0, 8));
                addFlowPane.getChildren().add(options[i]);
            }
            addFlowPane.getStyleClass().addAll("pane", "flow");
            addFlowPane.setPrefWrapLength(170);

            return addFlowPane;
         }

         public void createQuestionsLinkClicked(ActionEvent ae) {
        if (ae.getSource() == SQuestions) {
            //Questions ques = new Questions();
            stageCbt.setScene(staffMainPage);
            borderpane.setCenter(new Questions().getAnchor());
        }
    }


         public static void main(String[] args) {
            launch(args);
         }

}

这是子类(Questions.java)

import javafx.beans.property.StringProperty;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;


public class Questions {
    TextArea putQues;
    Button end, edit, review, next;
    BorderPane bpane;
    GridPane gdQues;
    AnchorPane anchor;
    ToggleGroup oneSelected;
    Text msg, option;

    public static String lvo[] = new String[]{
        "Option A", 
        "Option B",
        "Option C",
        "Option D",
        "Option E"
    };

    public static String rbo[] = new String[]{
        "A",
        "B",
        "C",
        "D",
        "E"
    };

    RadioButton rbs[] = new RadioButton[rbo.length];
    ListView lvs [] = new ListView[lvo.length];

    public Questions(){
       gdQues = new GridPane();
       gdQues.setHgap(10);
       gdQues.setVgap(10);;
       gdQues.setAlignment(Pos.TOP_LEFT);
       gdQues.getStyleClass().add("grid");

       msg = new Text("Questions should be put below");
       msg.setFont(Font.font("Arial", FontWeight.BOLD, 20));
       gdQues.add(msg, 3, 3);

       putQues = new TextArea();
       putQues.setPromptText("Type question here"); 
       gdQues.add(putQues, 4, 0);

       option = new Text("Options");
       option.setFont(Font.font("Monotype Corsiva", FontWeight.BOLD, 10));
       gdQues.add(option, 6, 0);

       //options = new ListView();
       //ListView optionAll = new ListView();
       for(int i = 0; i < rbo.length; i++){
           RadioButton rb = rbs[i] = new RadioButton(rbo[i]);
           ListView lv = lvs[i];
           rbs[i].setToggleGroup(oneSelected);
       }
       //gdQues.getChildren().addAll(rbs);
       //gdQues.getChildren().addAll(lvs);

       anchor = new AnchorPane();
       anchor.getStyleClass().add("pane");

       end = new Button("End");
       edit = new Button("Edit");
       review = new Button("Review");
       next = new Button("Next");

       HBox hb = new HBox();
       hb.getStyleClass().add("hb");
       hb.getChildren().addAll(end, edit, review, next);
       anchor.getChildren().addAll(gdQues, hb);

    }
    public Parent getGdQues(){
       return gdQues; 
    }
    public Parent getAnchor(/*GridPane gdQues*/){
        return anchor;
    }
    public Parent getPutQues(){
        return putQues;
    }
    public Text getMsg(){
        return msg;
    }
    /*public Parent  getRbs(){
        return rbo;
    }
    public Parent getLvs(){
        return lvs;
    }*/
}

除了radiobuttons之外的所有其他控件显示(我猜是因为函数没有进入构造函数)。

我现在的问题是如何显示无线电按钮?

1 个答案:

答案 0 :(得分:0)

我在实例化单选按钮时不知道您正在使用的格式,请参阅:

RadioButton rb = rbs[i] = new RadioButton(rbo[i]);

另请参阅此问题:initialize multiple variables in a same line in java

在你想到的时候,也许是另一个实例化的实例化。或者你只是忘了删除fisrt部分。无论是什么,它接缝,你都不使用rb变量,所以更喜欢:

rbs[i] = new RadioButton(rbo[i]);

或将其分为两行。