亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用另一個組合框中的選擇填充組合框?JavaFX

如何用另一個組合框中的選擇填充組合框?JavaFX

HUX布斯 2023-01-05 16:56:06
我已經開始為March Madness 括號生成器創建一個 GUI,方法是顯示第 1 輪的所有 64 支球隊,Labels現在我正在嘗試ComboBox為每場比賽創建一個下拉菜單。我已經ComboBox為 2 個匹配項創建了一個,現在我想創建一個新的ComboBox,它從它之前的其他兩個匹配項中提取它的選項ComboBox。所以在下面的示例圖中,newComboBox應該有Duke和VCU選項供用戶選擇。           (2 combo boxes)        (new combo box)Duke------               Duke ---   ND St. ---                                        XVCU -----               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;    }}
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

使用我之前的回答ComboBox中發布的方法成對組合es,直到只剩下一個.ComboBox

下面的代碼也以類似于樹結構的方式布置節點,但是您可以通過將每一輪保留在數據結構中而不是覆蓋單個數組的值來輕松解耦布局。(由于您需要訪問數據,因此無論如何您都應該將組合存儲在適當的數據結構中。)

private static ComboBox<String> createCombo(double x, double y, double width) {

    ComboBox<String> comboBox = new ComboBox<>();

    comboBox.setLayoutX(x);

    comboBox.setLayoutY(y);

    comboBox.setMaxWidth(Region.USE_PREF_SIZE);

    comboBox.setMinWidth(Region.USE_PREF_SIZE);

    comboBox.setPrefWidth(width);


    return comboBox;

}


private static Label createLabel(String text, double maxWidth) {

    Label label = new Label(text);

    label.setMaxWidth(maxWidth);

    return label;

}


@Override

public void start(Stage primaryStage) {

    String[] teams = new String[64];

    for (int i = 0; i < teams.length; i++) {

        teams[i] = Integer.toString(i);

    }

    final double offsetY = 30;

    final double offsetX = 100;

    final double width = 90;


    Pane root = new Pane();


    // array storing the comboboxes

    // combos for previous round are at the lowest indices

    ComboBox<String>[] combos = new ComboBox[teams.length / 2];


    // create initial team labels & comboboxes

    for (int i = 0, offsetTeams = 0; i < combos.length; i++, offsetTeams += 2) {

        Label label = createLabel(teams[offsetTeams], width);

        double y = offsetTeams * offsetY;

        label.setLayoutY(y);

        root.getChildren().add(label);


        label = createLabel(teams[offsetTeams+1], width);

        label.setLayoutY(y+offsetY);


        ComboBox<String> comboBox = createCombo(offsetX, y + offsetY / 2, width);

        comboBox.getItems().addAll(teams[offsetTeams], teams[offsetTeams+1]);

        combos[i] = comboBox;


        root.getChildren().addAll(label, comboBox);

    }


    double x = 2 * offsetX;

    int count = combos.length / 2; // combos still left for the next round


    for (; count > 0; count /= 2, x += offsetX) { // for each round

        // create comboboxes combining the combos from previous round pairwise

        for (int i = 0, ci = 0; i < count; i++, ci+=2) {

            // get combos pairwise

            ComboBox<String> c1 = combos[ci];

            ComboBox<String> c2 = combos[ci+1];


            ComboBox<String> combo = createCombo(x, (c1.getLayoutY() + c2.getLayoutY()) / 2, width) ;


            // combine data from previous round

            ChangeListener<String> listener = (o, oldValue, newValue) -> {

                final List<String> items = combo.getItems();

                int index = items.indexOf(oldValue);

                if (index >= 0) {

                    if (newValue == null) {

                        items.remove(index);

                    } else {

                        items.set(index, newValue);

                    }

                } else if (newValue != null) {

                    items.add(newValue);

                }

            };

            c1.valueProperty().addListener(listener);

            c2.valueProperty().addListener(listener);


            root.getChildren().add(combo);

            combos[i] = combo;

        }

    }


    primaryStage.setScene(new Scene(new ScrollPane(root), 600, 400));

    primaryStage.show(); 

}


查看完整回答
反對 回復 2023-01-05
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

你的問題的結構是一棵樹。所以您可能希望您的解決方案支持該結構。您可以使用二叉樹數據結構來模擬錦標賽,或者您可以通過以下類創建這樣的結構:

class Team {

   String name;

}


class Match {

   Team teamA;

   Team teamB;

   String where;

   Date when;


   public Team selectWinner() { 

     ...

   }

}


class Tournament {

   List<Team> teams;

   List<Match> getMatches(int round,List<Team> teams) {

     List<Match> matches=new ArrayList<Match>)();

     if (round==1) {

       for (teamIndex=1;teamIndex<=teams.size();teamIndex+=2) {

         Match match=new Match(teams[teamIndex-1],teams(teamIndex)];

         matches.add(match);

       }

     } else { 

       List<Team> winners=new ArrayList<Team>();

       for (Match match:getMatches(round-1)) {

         winners.add(match.selectWinner());

       }

       return getMatches(1,winners);

     }

   }

}

從這個結構中,您可以派生必要的 gui 組件,使選擇動態化,并讓 GUI 組件從 Tournament、Match 和 Team 類中獲取它們的值。



查看完整回答
反對 回復 2023-01-05
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號