如何用另一个组合框中的选择填充组合框?JavaFX

我已经开始为March Madness 括号生成器创建一个 GUI,方法是显示第 1 轮的所有 64 支球队,Labels现在我正在尝试ComboBox为每场比赛创建一个下拉菜单。

我已经ComboBox为 2 个匹配项创建了一个,现在我想创建一个新的ComboBox,它从它之前的其他两个匹配项中提取它的选项ComboBox。所以在下面的示例图中,newComboBox应该有Duke和VCU选项供用户选择。

           (2 combo boxes)        (new combo box)


Duke------

               Duke ---   

ND St. ---


                                        X


VCU -----

               VCU ---

UCF -----  

我该怎么做?


public class ControlPanel extends Application

{


    @Override

    public void start(Stage primaryStage) {


        primaryStage.setTitle("March Madness 2019 Generator");


        BorderPane componentLayout = new BorderPane();

        componentLayout.setPadding(new Insets(20,0,20,20));


        final FlowPane choicePane = new FlowPane();

        choicePane.setHgap(100);

        Label choiceLbl = new Label("Match1");


        ArrayList<Team> round1 = new ArrayList<Team>();


        round1.add(new Team("Duke", 0.670, 1));                    //0

        round1.add(new Team("North Dakota St", 0.495, 16));

        round1.add(new Team("VCU", 0.609, 8));

        round1.add(new Team("UCF", 0.606, 9));



        //The choicebox is populated from an observableArrayList

        ChoiceBox r2Match1 = new ChoiceBox(FXCollections.observableArrayList(  match(round1, 0, 1)   ));


        //Add the label and choicebox to the flowpane

        choicePane.getChildren().add(choiceLbl);

        choicePane.getChildren().add(r2Match1);


        //put the flowpane in the top area of the BorderPane

        componentLayout.setTop(choicePane);


        //Add the BorderPane to the Scene

        Scene appScene = new Scene(componentLayout,500,500);

        //Add the Scene to the Stage

        primaryStage.setScene(appScene);

        primaryStage.show();

    }


    private ArrayList<Team> match(ArrayList<Team> roundPullFrom, int team1, int team2) {

        ArrayList<Team> temp = new ArrayList<Team>();

        temp.add(roundPullFrom.get(team1));

        temp.add(roundPullFrom.get(team2));

        return temp;

    }


}


HUX布斯
浏览 69回答 2
2回答

慕尼黑8549860

使用我之前的回答ComboBox中发布的方法成对组合es,直到只剩下一个.ComboBox下面的代码也以类似于树结构的方式布置节点,但是您可以通过将每一轮保留在数据结构中而不是覆盖单个数组的值来轻松解耦布局。(由于您需要访问数据,因此无论如何您都应该将组合存储在适当的数据结构中。)private static ComboBox<String> createCombo(double x, double y, double width) {&nbsp; &nbsp; ComboBox<String> comboBox = new ComboBox<>();&nbsp; &nbsp; comboBox.setLayoutX(x);&nbsp; &nbsp; comboBox.setLayoutY(y);&nbsp; &nbsp; comboBox.setMaxWidth(Region.USE_PREF_SIZE);&nbsp; &nbsp; comboBox.setMinWidth(Region.USE_PREF_SIZE);&nbsp; &nbsp; comboBox.setPrefWidth(width);&nbsp; &nbsp; return comboBox;}private static Label createLabel(String text, double maxWidth) {&nbsp; &nbsp; Label label = new Label(text);&nbsp; &nbsp; label.setMaxWidth(maxWidth);&nbsp; &nbsp; return label;}@Overridepublic void start(Stage primaryStage) {&nbsp; &nbsp; String[] teams = new String[64];&nbsp; &nbsp; for (int i = 0; i < teams.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; teams[i] = Integer.toString(i);&nbsp; &nbsp; }&nbsp; &nbsp; final double offsetY = 30;&nbsp; &nbsp; final double offsetX = 100;&nbsp; &nbsp; final double width = 90;&nbsp; &nbsp; Pane root = new Pane();&nbsp; &nbsp; // array storing the comboboxes&nbsp; &nbsp; // combos for previous round are at the lowest indices&nbsp; &nbsp; ComboBox<String>[] combos = new ComboBox[teams.length / 2];&nbsp; &nbsp; // create initial team labels & comboboxes&nbsp; &nbsp; for (int i = 0, offsetTeams = 0; i < combos.length; i++, offsetTeams += 2) {&nbsp; &nbsp; &nbsp; &nbsp; Label label = createLabel(teams[offsetTeams], width);&nbsp; &nbsp; &nbsp; &nbsp; double y = offsetTeams * offsetY;&nbsp; &nbsp; &nbsp; &nbsp; label.setLayoutY(y);&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(label);&nbsp; &nbsp; &nbsp; &nbsp; label = createLabel(teams[offsetTeams+1], width);&nbsp; &nbsp; &nbsp; &nbsp; label.setLayoutY(y+offsetY);&nbsp; &nbsp; &nbsp; &nbsp; ComboBox<String> comboBox = createCombo(offsetX, y + offsetY / 2, width);&nbsp; &nbsp; &nbsp; &nbsp; comboBox.getItems().addAll(teams[offsetTeams], teams[offsetTeams+1]);&nbsp; &nbsp; &nbsp; &nbsp; combos[i] = comboBox;&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().addAll(label, comboBox);&nbsp; &nbsp; }&nbsp; &nbsp; double x = 2 * offsetX;&nbsp; &nbsp; int count = combos.length / 2; // combos still left for the next round&nbsp; &nbsp; for (; count > 0; count /= 2, x += offsetX) { // for each round&nbsp; &nbsp; &nbsp; &nbsp; // create comboboxes combining the combos from previous round pairwise&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, ci = 0; i < count; i++, ci+=2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get combos pairwise&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComboBox<String> c1 = combos[ci];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComboBox<String> c2 = combos[ci+1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ComboBox<String> combo = createCombo(x, (c1.getLayoutY() + c2.getLayoutY()) / 2, width) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // combine data from previous round&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChangeListener<String> listener = (o, oldValue, newValue) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final List<String> items = combo.getItems();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = items.indexOf(oldValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (newValue == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.remove(index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.set(index, newValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (newValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.add(newValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c1.valueProperty().addListener(listener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2.valueProperty().addListener(listener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(combo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; combos[i] = combo;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; primaryStage.setScene(new Scene(new ScrollPane(root), 600, 400));&nbsp; &nbsp; primaryStage.show();&nbsp;}

忽然笑

你的问题的结构是一棵树。所以您可能希望您的解决方案支持该结构。您可以使用二叉树数据结构来模拟锦标赛,或者您可以通过以下类创建这样的结构:class Team {&nbsp; &nbsp;String name;}class Match {&nbsp; &nbsp;Team teamA;&nbsp; &nbsp;Team teamB;&nbsp; &nbsp;String where;&nbsp; &nbsp;Date when;&nbsp; &nbsp;public Team selectWinner() {&nbsp;&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp;}}class Tournament {&nbsp; &nbsp;List<Team> teams;&nbsp; &nbsp;List<Match> getMatches(int round,List<Team> teams) {&nbsp; &nbsp; &nbsp;List<Match> matches=new ArrayList<Match>)();&nbsp; &nbsp; &nbsp;if (round==1) {&nbsp; &nbsp; &nbsp; &nbsp;for (teamIndex=1;teamIndex<=teams.size();teamIndex+=2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Match match=new Match(teams[teamIndex-1],teams(teamIndex)];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matches.add(match);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;} else {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;List<Team> winners=new ArrayList<Team>();&nbsp; &nbsp; &nbsp; &nbsp;for (Match match:getMatches(round-1)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winners.add(match.selectWinner());&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return getMatches(1,winners);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}从这个结构中,您可以派生必要的 gui 组件,使选择动态化,并让 GUI 组件从 Tournament、Match 和 Team 类中获取它们的值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java