user3712626
user3712626

Reputation: 143

Issues setting my JPanel to have a layout manager

I think it is just late and my brain stopped working, I have been staring at this for a good hour now and can't seem to figure out my mistake. My program in a nutshell will display all of the states voting districts with a radiobutton for each party underneath it (Dem,Rep,Ind) after clicking a button for each state, it will then log a counter for each and return a winner when a party reaches 270 votes.

I am attempting to use a cardLayout to display state name and the buttons as a single component, then I plan to use GridLayout to display all of the states on one frame, but im getting ahead of myself.

Right now my issue lies in the line JPanel cards = new JPanel(new CardLayout()); and the error is telling me the constructor is undefined. I have attempted googling the error with not a lot of clear information as to how to fix it.

public class ElectoralCollegeGUI extends JFrame 
{
    private static final long serialVersionUID = 1L;

    public ElectoralCollegeGUI()
    {
    super("Cast Your Votes");
    //JFrame frame = new JFrame();
    //setLayout(new GridLayout(14, ))
    JPanel mainPanel = new JPanel();
    setLayout(new FlowLayout());
    JPanel buttonPanel = new JPanel();
    JPanel stateNames = new JPanel();

    JRadioButton Democrat,Republican,Independent ;
    ButtonGroup party;
    party = new ButtonGroup();

    Democrat = new JRadioButton("Democrat");
    Republican = new JRadioButton("Republican");
    Independent = new JRadioButton("Independent");

    add(buttonPanel,BorderLayout.CENTER);

    //adds buttons to party button group
    party.add(Democrat);
    party.add(Republican);
    party.add(Independent);

    //adds buttons to button panel
    buttonPanel.add(Democrat);
    buttonPanel.add(Republican);
    buttonPanel.add(Independent);

class CardLayout implements ItemListener 
{

    public void addComponentToPane(Container pane) 
    {
        JLabel[] state = {new JLabel("Alabama"), new JLabel("Alaska"), new JLabel("Arizona"), new JLabel("Arkansas"), new JLabel("California"),
                   new JLabel("Colorado"), new JLabel("Connecticut"), new JLabel("Delaware"), new JLabel("Florida"), new JLabel("Georgia"),
                   new JLabel("Hawaii"), new JLabel("Idaho"), new JLabel("Illinois"), new JLabel("Indiana"), new JLabel("Iowa"), new JLabel("Kansas"),
                   new JLabel("Kentucky"), new JLabel("Louisiana"), new JLabel("Maine 1st"), new JLabel("Maine 2nd"), new JLabel("Maine Popular"),
                   new JLabel("Maryland"), new JLabel("Massachusetts"), new JLabel("Michigan"), new JLabel("Minnesota"), new JLabel("Mississippi"),
                   new JLabel("Missouri"), new JLabel("Montant"), new JLabel("Nebraska 1st"), new JLabel("Nebraska 2nd"), new JLabel("Nebraska 3rd"),
                   new JLabel("Nebraska Popular"), new JLabel("Nevada"), new JLabel("New Hampshire"), new JLabel("New Jersey"), new JLabel("New Mexico"),
                   new JLabel("New York"), new JLabel("North Carolina"), new JLabel("North Dakota"), new JLabel("Ohio"), new JLabel("Oklahoma"),
                   new JLabel("Oregon"), new JLabel("Pennsylvania"), new JLabel("Rhode Island"), new JLabel("South Carolina"), new JLabel("South Dakota"),
                   new JLabel("Tennessee"), new JLabel("Texas"),new JLabel("Utah"),new JLabel("Vermont"),new JLabel("Virginia"),new JLabel("Washington"),
                   new JLabel("West Virginia"),new JLabel("Wisconsin"),new JLabel("Wyoming"),new JLabel("Washington,D.C."),};   

        for (int i = 0; i < state.length ; i++)
        {
            stateNames.add(state[i]);       //stateNames is a JPanel already
        }
        for( int j = 0; j<state.length; j++)
        {
            stateNames.setText(stateNames.getText(state[j]));
        }

        //Create the "cards".
        JPanel buttonsCard = new JPanel();
        buttonsCard.add(new JRadioButton("Democrat"));
        buttonsCard.add(new JRadioButton("Republican"));
        buttonsCard.add(new JRadioButton("Indepdent"));

        //JPanel card2 = new JPanel();


        //Create the panel that contains the "cards".

        JPanel cards = new JPanel(new CardLayout());

        pane.add(stateNames, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }

    public void itemStateChanged(ItemEvent evt) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, (String)evt.getItem());
    }

Upvotes: 0

Views: 81

Answers (1)

kiheru
kiheru

Reputation: 6618

You have a custom class CardLayout, and that is used in the line JPanel cards = new JPanel(new CardLayout()); instead of the standard class CardLayout as intended. Either rename your class (this would certainly be the simplest and least confusing), or use the full name java.awt.CardLayout where needed.

Upvotes: 2

Related Questions