简体   繁体   English

需要帮助让JRadioButton将Value返回到JTextField

[英]Need help getting JRadioButton to return Value to JTextField

My program is simulating voting in the Electoral College. 我的程序是在选举学院模拟投票。 Each state has a certain number of votes. 每个州都有一定数量的选票。 I am having trouble with getting my buttons for each state to return the proper amount of votes to a total, and then printing that total to the screen in a JTextField each time a button for that party is clicked. 我在使每个状态的按钮返回正确的总数时遇到麻烦,然后每次单击该方的按钮时将该总数打印到JTextField的屏幕上。

My question is should I be using an ItemStateChanged or ActionListener? 我的问题是我应该使用ItemStateChanged还是ActionListener? After some research online, I currently am using an ActionListener, but I can't seem to figure out the final implementation or if this is the best way of going about finishing my program. 经过一些在线研究之后,我目前正在使用ActionListener,但似乎无法弄清楚最终的实现,或者这是否是完成程序的最佳方法。

ActionListener RadioButtonActionListener = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource() instanceof JRadioButton)
                {
                    if(Democrat.isSelected() == true)
                    {
                        String Count = StateValue.getText();
                        int countInteger = Integer.parseInt(Count);
                        int demoCount = demoCount + countInteger;
                        demoTotal.setText(demoCount);
                    }
                    else if (Democrat.isSelected() == false)
                    {

                    }
                }
            }

        };

Rest of my code: 我的其余代码:

public class ElectoralCollegeGUI extends JFrame 
{
private static final long serialVersionUID = 1L;
private static int demoVoteCount = 0;
private static int repVoteCount = 0;
private static int undVoteCount = 0;
private JRadioButton Democrat,Republican,Undecided;
private static String whatState;

public ElectoralCollegeGUI()
{
    super("Cast Your Votes");
    //JPanel mainGridPanel = new JPanel();
    setLayout(new GridLayout(22,5));
    JLabel demoVoteLabel = new JLabel("Democrat Votes");
    JTextField demoTotal = new JTextField();
    JLabel repVoteLabel = new JLabel("Republican Votes");
    JTextField repTotal = new JTextField();     
    JLabel undVoteLabel = new JLabel("Undecided Votes");
    JTextField undTotal = new JTextField();




    demoTotal.setEditable(false);
    repTotal.setEditable(false);
    undTotal.setEditable(false);

    add(demoTotal, BorderLayout.SOUTH);
    add(repTotal, BorderLayout.SOUTH);
    add(undTotal,BorderLayout.SOUTH);
    add(demoVoteLabel);
    add(repVoteLabel);
    add(undVoteLabel);

    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
    {
        private static final long serialVersionUID = 1L;

        public VoteChoice(String state, String voteValue)
        {
            setLayout(new FlowLayout());

            JLabel StateName = new JLabel(state);
            JLabel StateValue = new JLabel(voteValue);

            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(StateName, BorderLayout.WEST);
            add(StateValue,BorderLayout.WEST);
            add(Democrat, BorderLayout.EAST);
            add(Republican, BorderLayout.EAST);
            add(Undecided, BorderLayout.EAST);

            RadioButtonActionListener actionListener = new RadioButtonActionListener();
            Democrat.addActionListener(actionListener);
            Republican.addActionListener(actionListener);
            Undecided.addActionListener(actionListener);

So, from what I can ascertain from you code snippet, you want to know when changes occur on VoteChoice and update a field in the ElectoralCollegeGUI . 因此,根据我的代码片段,您想知道VoteChoice何时发生更改,并更新ElectoralCollegeGUI的字段。

Now you are creating multiple instances of VoteChoice ... 现在您正在创建VoteChoice多个实例...

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

But the JRadioButton 's are instance fields of the ElectoralCollegeGUI class, this is going to mean that the fields will only have context to the last instance of VoteChoice that you created. 但是JRadioButtonElectoralCollegeGUI类的实例字段,这意味着这些字段将仅具有您创建的VoteChoice的最后一个实例的VoteChoice

Instead, the radio buttons should be instance fields of the VoteChoice class and the VoteChoice should monitoring their states. 相反,单选按钮应该是VoteChoice类的实例字段,而VoteChoice应该监视其状态。

When the VoteChoice detects a change, it should trigger an event back to the ElectoralCollegeGUI describing what has changed. VoteChoice检测到更改时,它将触发一个事件返回ElectoralCollegeGUI描述发生了什么更改。 While you could reuse one of the many listener types available in the API, you might create your own, and using enum s, you could easily determine what has changed... 虽然您可以重用API中可用的许多侦听器类型之一,但是可以创建自己的侦听器类型,并使用enum ,可以轻松确定发生了什么变化。

public interface VoteChoiceListener {
    public enum Party {
        DEMOCRAT,
        REPUBLICAN,
        UNDECIDED;
    }

    public void voteCast(VoiceChoice choice, Party party);
}

You would then register an instance of VoteChoiceListener with each instance of VoteChoice so it can trigger the event when a vote changes 然后,您将注册的实例VoteChoiceListener用的每个实例VoteChoice所以它可以触发事件时,一票改变

This basically a observer pattern 这基本上是观察者模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM