我的循环出了什么问题?

时间:2017-01-30 23:07:56

标签: java loops

所以我试图"优化"我的代码,通过使用大量循环减少行数。但它有问题。

我只是试图显示一堆单选按钮,只显示两个按钮而且都没有做任何事情。

只是想知道是否有人可以批评我的代码,并在他出错的地方显示这个新内容。

感谢任何帮助:)

import java.awt.event.*;
import javax.swing.*;  
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;


public class NewGame implements ActionListener {

    private final String[] premierLeagueClubs = {"Arsenal", "Bournemouth", "Burnley", "Chelsea", "Crystal Palace",
        "Everton", "Hull City", "Leicester City", "Liverpool", "Manchester United", "Manchester City", "Middlesborough",
        "Southampton", "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur", "Watford", "West Brom", "West Ham"};
    private final JRadioButton[] rb = new JRadioButton[20];
    JFrame f3;
    private final JButton b, quit;
    String teamName;
    JLabel label1;
    private static JPanel chooseClubPanel;

    public NewGame() {
        f3 = new JFrame("Ballon d'or");
        f3.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f3.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        chooseClubPanel = new JPanel();
        f3.add(chooseClubPanel, BorderLayout.CENTER);
        chooseClubPanel.setBackground(Color.GREEN);

        label1 = new JLabel("Please choose a team");
        chooseClubPanel.add(label1);

        ButtonGroup bg = new ButtonGroup();

        int startvalueBG;
        int endvalueBG = 19;
        for (startvalueBG = 0; startvalueBG <= endvalueBG; startvalueBG++) {
            bg.add(rb[startvalueBG]);
        }

        b = new JButton("OK");
        b.addActionListener(this);
        chooseClubPanel.add(b);

        quit = new JButton("Quit");
        quit.addActionListener(this);
        chooseClubPanel.add(quit);

        int startValueAddRB;
        int endValueAddRB = 19;
        for (startValueAddRB = 0; startValueAddRB <= endValueAddRB; startValueAddRB++) {
            f3.add(rb[startValueAddRB]);
        }

        int startRB;
        int endRB = 19;

        for (startRB = 0; endRB <= 19; startRB++) {
            for (int i = 0; i < premierLeagueClubs.length; i++) {
                rb[startRB] = new JRadioButton(premierLeagueClubs[i]);
                chooseClubPanel.add(rb[i]);
            }
        }
        f3.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(f3, "Are you sure ?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    f3.setVisible(false);
                    f3.dispose();
                } else {

                    f3.setVisible(true);
                }
            }
        });
    }

    public void setFrame(JFrame f3) {
        this.f3 = f3;
    }

    public JFrame getFrame() {

        return f3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        int rbNumber;
        int rbNumberMax = 19;

        for (rbNumber = 0; rbNumber < rbNumberMax; rbNumber++) {
            if (rb[rbNumber].isSelected()) {
                teamName = rb[rbNumber].getText();
                f3.dispose();
                JOptionPane.showMessageDialog(f3, "You chose : " + rb[rbNumber].getText());

            }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

所以,让我们来看看代码......

首先,您尝试将每个按钮添加到ButtonGroup,但是,您还没有实际创建任何按钮,可能NullPointerException

int startvalueBG;
int endvalueBG = 19;
for (startvalueBG = 0; startvalueBG <= endvalueBG; startvalueBG++) {
    bg.add(rb[startvalueBG]);
}

b = new JButton("OK");
b.addActionListener(this);
chooseClubPanel.add(b);

quit = new JButton("Quit");
quit.addActionListener(this);
chooseClubPanel.add(quit);

然后你将每个按钮添加到框架中,但框架使用BorderLayout,因此只有一个按钮可见,最后一个按钮...

int startValueAddRB;
int endValueAddRB = 19;
for (startValueAddRB = 0; startValueAddRB <= endValueAddRB; startValueAddRB++) {
    f3.add(rb[startValueAddRB]);
}

然后你尝试将按钮添加到chooseClubPanel,有效地从框架中移除,但索引全部搞砸了,你正在创建19x19按钮,每个俱乐部分配给所有19个并且有效地替换为下一个俱乐部......!?!?

int startRB;
int endRB = 19;

for (startRB = 0; endRB <= 19; startRB++) {
    for (int i = 0; i < premierLeagueClubs.length; i++) {
        rb[startRB] = new JRadioButton(premierLeagueClubs[i]);
        chooseClubPanel.add(rb[i]);
    }
}

我们要做的第一件事就是删除对chooseClubPanel框架的引用,它们只是妨碍了,并且该类没有业务生成框架,&#39不是它的责任。

接下来,我们将NewGame设为JPanel并允许其自行管理。

接下来,我们将把循环次数从3减少到1并完成我们需要同时完成的所有工作

Teams

public class NewGame extends JPanel implements ActionListener {

    private final String[] premierLeagueClubs = {"Arsenal", "Bournemouth", "Burnley", "Chelsea", "Crystal Palace",
                                                                                             "Everton", "Hull City", "Leicester City", "Liverpool", "Manchester United", "Manchester City", "Middlesborough",
                                                                                             "Southampton", "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur", "Watford", "West Brom", "West Ham"};
    private final JRadioButton[] rb = new JRadioButton[20];
    private final JButton b, quit;
    String teamName;
    JLabel label1;

    ButtonGroup bg = new ButtonGroup();

    public NewGame() {
        setBackground(Color.GREEN);

        label1 = new JLabel("Please choose a team");
        add(label1);

        for (int i = 0; i < premierLeagueClubs.length; i++) {
            rb[i] = new JRadioButton(premierLeagueClubs[i]);
            rb[i].setActionCommand(premierLeagueClubs[i]);
            bg.add(rb[i]);
            add(rb[i]);
        }

        b = new JButton("OK");
        b.addActionListener(this);
        add(b);

        quit = new JButton("Quit");
        quit.addActionListener(this);
        add(quit);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("You have selected " + bg.getSelection().getActionCommand());
    }

}

在某些时候,您想要显示,最简单的解决方案是将其添加到您想要的任何容器中,例如JFrame

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Title");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new NewGame());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

通常情况下,我建议您查看Laying Out Components Within a Container,但在您的情况下,我认为查看How to Use Lists

会更为实际