如何获得一个返回值的按钮?

时间:2015-10-09 18:58:20

标签: java jtextfield jradiobutton

我正在编写一个模拟选举团投票区的计划。当选择相应方的JRadioButton以及将每一方的总票数打印到TextField以显示它时,我无法使用逻辑来计算投票。

public ElectoralCollegeGUI()
{
    super("Cast Your Votes");
    //JPanel mainGridPanel = new JPanel();
    setLayout(new GridLayout(14,8));


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

    String[] voteValue = { "9","3","11","6","55","9","7","3","29","16","4","4","20","11","6",
                        "6","8","8","1","1","2","10","11","16","10","6","10","3","1","1","1",
                        "2","6","4","14","5","29","15","3","18","7","7","20","4","9","3","11",
                        "38","6","3","13","12","5","10","3","3"};



    for ( int i = 0; i < 56 ; i++)
    {
        add(new VoteChoice(state[i] , voteValue[i]));
    }

}
    private class VoteChoice extends JPanel
    {
        public VoteChoice(String state, String voteValue)
        {
            setLayout(new FlowLayout());

            JLabel StateName = new JLabel(state);
            JLabel StateValue = new JLabel(voteValue);
            JTextField demoTotal = new JTextField();
            JTextField repTotal = new JTextField();
            JTextField undTotal = new JTextField();

            demoTotal.setText(getDemoVoteCount());
            repTotal.setText(getRepVoteCount());
            undTotal.setText(getUndVoteCount());


            ButtonGroup party;
            party = new ButtonGroup();

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

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

            add(demoTotal, BorderLayout.PAGE_END);
            add(repTotal, BorderLayout.PAGE_END);
            add(undTotal,BorderLayout.PAGE_END);

            add(StateName, BorderLayout.PAGE_START);
            add(StateValue,BorderLayout.PAGE_START);
            add(Democrat, BorderLayout.PAGE_END);
            add(Republican, BorderLayout.PAGE_END);
            add(Undecided, BorderLayout.PAGE_END);



            VoteSelected listener = new VoteSelected();
            Democrat.addItemListener(listener);
            Republican.addItemListener(listener);
            Undecided.addItemListener(listener);

        }//end vote choice constructor

            private class VoteSelected implements ItemListener
            {
                @Override
                public void itemStateChanged(ItemEvent e) 
                {
                    if(e.getStateChange() == ItemEvent.SELECTED)
                    {
                        if(e.getSource() == Democrat)
                        {
                            //code to get what state was selected
                            //code to calculate vote based on what state was selected. 
                        }
                    }
                }

            }

        }//end voteChoice contructor

}//ends main class

以上是我目前的GUI代码。我到目前为止尝试的是:

private class VoteSelected implements ItemListener
            {
                @Override
                public void itemStateChanged(ItemEvent e) 
                {
                    if(e.getStateChange() == ItemEvent.SELECTED)
                    {
                        if(e.getSource() == Democrat)
                        {
                            if(Democrat.isSelected() == true)
                            {
                                whatState = state; //gets state name
                                String numDemoVotesString = voteValue; //gets numb of votes as String
                                int numDemoVotesInt = Integer.parseInt(numDemoVotesString); //converts numb votes from String to int.
                                demoVoteCount = demoVoteCount + numDemoVotesInt;
                            }
                            else if(Democrat.isSelected() == false)
                            {
                                whatState = state; //gets state name
                                String numDemoVotesString = voteValue; //gets numb of votes as String
                                int numDemoVotesInt = Integer.parseInt(numDemoVotesString); //converts numb votes from String to int.
                                demoVoteCount = demoVoteCount - numDemoVotesInt;
                            }
                            return demoVoteCount;
                        }
                    }
                }

            }

之后我有一个getDemoVoteCount的公共类,它将作为字符串返回,我将传递给TextField。但问题是按钮逻辑不正确。有人可以帮我修改我的方法或建议一些可能更好的方法。

0 个答案:

没有答案