java 如何从数据文件读取的数组中删除球队名称(字符串)?

oknrviil  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(76)

我正在尝试创建一个输入对话框,提示用户选择与第一支球队比赛的第二支球队。用户之前选择的球队无法再次显示,如果用户输入了不是球队选项的内容,则需要进行错误检查。我正在使用一个名为Team的类从数据文件中获取球队名称。我已经创建了选择第一支球队的方法。这是所需的输出:The message for the user to select two teams
我创建了一个while循环,如果用户再次输入第一个团队或其他非团队选项,该循环将继续运行。我为每个团队创建了不同的JOptionPane输入对话框,并认为用户可以输入任何内容,代码将检查其是否有效。**但是,当程序运行时,它完全跳过了这个方法。***联赛 * 是一系列有输赢的球队和他们的花名册这个方法有两个形式参数,分别接受联赛数组和用户选择的第一支球队team 1,这个方法应该返回第二支球队的选择。
这是我目前的代码:

for (int i = 0; i < league.length; i++) {
            while (!team2.equalsIgnoreCase(league[i].getName()) && team2.equals(team1)) {
                if (team1.equals(league[0].getName())) {
                    display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
                            + "-" + league[1].getName() + "\n"
                            + "-" + league[2].getName() + "\n"
                            + "-" + league[3].getName() + "\n\n");
                } else if (team1.equals(league[1].getName())) {
                    display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
                            + "-" + league[0].getName() + "\n"
                            + "-" + league[2].getName() + "\n"
                            + "-" + league[3].getName() + "\n\n");
                } else if (team1.equals(league[2].getName())) {
                    display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
                            + "-" + league[0].getName() + "\n"
                            + "-" + league[1].getName() + "\n"
                            + "-" + league[3].getName() + "\n\n");
                } else if (team1.equals(league[3].getName())) {
                    display = JOptionPane.showInputDialog("Select a team to play the " + team1 + "\n\n"
                            + "-" + league[0].getName() + "\n"
                            + "-" + league[1].getName() + "\n"
                            + "-" + league[2].getName() + "\n\n");
                } else {
                    popup("Sorry, I couldn't find " + team2 + ", please try again...");
                }
            }
        }

任何帮助都很好!

v6ylcynt

v6ylcynt1#

您可能想尝试一些不同的东西。也许使用两个JComboBox来保存团队名称。当团队名称从第一个ComboBox中选定后,将该名称从第二个ComboBox中删除。第二个ComboBox甚至在第一个ComboBox具有选定名称之前不会变为活动状态。“确定”按钮在第二个ComboBox具有选定名称之前不会变为启用状态。这在下面的GIF中进行了演示:

下面是可运行的代码。请务必阅读代码中的所有注解:

public class PlayTeamsDemo {

    // Class Member Variables:
    /* List of Team Names to be used in JComboBoxes. List<String> is
       used due to its flexability:        */
    private java.util.List<String> teamNames = java.util.Arrays.asList(
            "Toronto Raptors", "Chicago Cows", "Orlando Tricks", "Boston Gaelics"
            );
    
    String team1 = "";  // Will hold the first selected team (default null string).
    String team2 = "";  // Will hold the second selected team (default null string).
    
    // Swing component that requires class global access:
    javax.swing.JDialog dialog;
    javax.swing.JLabel topTitle;
    javax.swing.JComboBox teamNamesCombo;
    javax.swing.JLabel bottomTitle;
    javax.swing.JComboBox otherTeamNamesCombo;
    javax.swing.JButton okButton;
    javax.swing.JButton cancelButton;
    
    // Constructor:
    public PlayTeamsDemo() {
        initForm();  // Create the dialog and display:
    }

    private void initForm() {
        // Dialog Frame...
        dialog = new javax.swing.JDialog();
        dialog.setTitle("Team Play:");
        dialog.setResizable(false);
        dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setSize(300, 210);
        dialog.setAlwaysOnTop(true);
        dialog.setModal(true);
        dialog.setLocationRelativeTo(null);

        // Dialog Main JPanel...
        javax.swing.JPanel mainPanel = new javax.swing.JPanel();

        // Upper Panel...To hold Top Title JLabel and first JComboBox:
        javax.swing.JPanel firstTeamPanel = new javax.swing.JPanel(new java.awt.GridLayout(2, 1));
        topTitle = new javax.swing.JLabel("Select a team:");
        firstTeamPanel.add(topTitle);

        teamNamesCombo = new javax.swing.JComboBox();
        teamNamesCombo.setModel(new javax.swing.DefaultComboBoxModel<String>(
                teamNames.toArray(new String[0])));
        teamNamesCombo.setPreferredSize(new java.awt.Dimension(200, 25));
        teamNamesCombo.setSelectedIndex(-1);
        
        // Top Combo ItemListener
        teamNamesCombo.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                // If a team name was actually selected:
                if (!teamNamesCombo.getSelectedItem().toString().isEmpty()) {
                    // Load the second JComboBox with Team Names...
                    otherTeamNamesCombo.setModel(new javax.swing.DefaultComboBoxModel<String>(
                                                 teamNames.toArray(new String[0])));
                    // Make sure nothing is selected in second JComboBox:
                    otherTeamNamesCombo.setSelectedIndex(-1);
                    // Apply the selected team name from fist combobox to team1 variable:
                    team1 = teamNamesCombo.getSelectedItem().toString();
                    // Get the selected index value from first ComboBox
                    int selectedIndex = teamNamesCombo.getSelectedIndex();
                    /* Remove the selected team name in first combo from
                       the second combobox so the same name can't be selected: */
                    otherTeamNamesCombo.removeItemAt(selectedIndex);
                    // Enable bottom combobox title JLabel:
                    bottomTitle.setEnabled(true);
                    // Enable the second team name combobox:
                    otherTeamNamesCombo.setEnabled(true);
                }
            }
        });
        firstTeamPanel.add(teamNamesCombo);

        // Lower Panel...To hold Bottom Title JLabel and second JComboBox:
        javax.swing.JPanel secondTeamPanel = new javax.swing.JPanel(new java.awt.GridLayout(2, 1));
        bottomTitle = new javax.swing.JLabel("Select a team to play the above: ");
        bottomTitle.setEnabled(false); // JLabel is Disabled at startup
        secondTeamPanel.add(bottomTitle);
        
        // Second team names Combobox:
        otherTeamNamesCombo = new javax.swing.JComboBox();
        otherTeamNamesCombo.setPreferredSize(new java.awt.Dimension(200, 25));
        otherTeamNamesCombo.setEnabled(false); // Combobox is Disabled at startup
        // Bottom Combo ItemListener
        otherTeamNamesCombo.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                // If second combobox has an actual selected team name...
                if (otherTeamNamesCombo.getSelectedItem() != null && 
                           !otherTeamNamesCombo.getSelectedItem().toString().isEmpty()) {
                    // Apply the second team name to the team2 variable:
                    team2 = otherTeamNamesCombo.getSelectedItem().toString();
                    // Enable the OK button:
                    okButton.setEnabled(true);
                }
            }
        });
        secondTeamPanel.add(otherTeamNamesCombo);
        
        // JPanel to hold JButtons:
        javax.swing.JPanel buttonsPanel = new javax.swing.JPanel();
        // The OK Button:
        okButton = new javax.swing.JButton("OK");
        okButton.setEnabled(false);  // JButton is Disabled at startup
        // OK Button Action Listener:
        okButton.addActionListener(new java.awt.event.ActionListener() { 
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                /* If the OK button is selected then display a message box
                   indicating the teams to be in play:             */
                javax.swing.JOptionPane.showMessageDialog(dialog, team1 + " vs " + team2, 
                            "Teams in play...", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                dialog.dispose();  // Dispose the dialog window:
            } 
        });
        buttonsPanel.add(okButton);
        
        // The Cancel button:
        cancelButton = new javax.swing.JButton("Cancel");
        // The Cancel button ActionListener:
        cancelButton.addActionListener(new java.awt.event.ActionListener() { 
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) { 
                // Indicate that the process has been Csnceled:
                javax.swing.JOptionPane.showMessageDialog(dialog, "Operation Canceled!", 
                            "Canceled!", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                team1 = ""; // Empty the team1 variable:
                team2 = ""; // Empty the team2 variable:
                dialog.dispose();  // Dispose the dialog window:
            } 
        });
        buttonsPanel.add(cancelButton);
        
        mainPanel.add(firstTeamPanel);
        mainPanel.add(secondTeamPanel);
        mainPanel.add(buttonsPanel);
        dialog.add(mainPanel);
    }

    public static void main(String[] args) {
        // App started this way to avoid the need for statics:
        new PlayTeamsDemo().startApp(args);
    }

    private void startApp(String[] args) {
        /* Display the Dialog Window */
        java.awt.EventQueue.invokeLater(() -> {
            dialog.setVisible(true);
        });
    }
}

相关问题