java - 需要帮助让 JRadioButton 将值返回到 JTextField

标签 java swing jradiobutton

我的程序正在模拟选举团投票。每个州都有一定数量的选票。我无法让每个州的按钮返回适当数量的选票总数,然后每次单击该政党的按钮时将总数打印到 JTextField 中的屏幕上。

我的问题是我应该使用 ItemStateChanged 还是 ActionListener?经过一些在线研究后,我目前正在使用 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)
                    {

                    }
                }
            }

        };

我的其余代码:

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);

最佳答案

因此,从我从您的代码片段中可以确定的情况来看,您想知道 VoteChoice 何时发生更改并更新 ElectoralCollegeGUI 中的字段。

现在您正在创建 VoteChoice 的多个实例...

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

但是 JRadioButtonElectoralCollegeGUI 类的实例字段,这意味着这些字段仅具有您创建的 VoteChoice 最后一个实例的上下文。

相反,单选按钮应该是 VoteChoice 类的实例字段,并且 VoteChoice 应该监视它们的状态。

VoteChoice检测到更改时,它应该触发一个事件返回到ElectoralCollegeGUI,描述发生了什么变化。虽然您可以重用 API 中可用的多种监听器类型之一,但您也可以创建自己的监听器类型,并使用枚举,您可以轻松确定发生了什么更改...

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

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

然后,您可以向每个 VoteChoice 实例注册一个 VoteChoiceListener 实例,以便在投票发生变化时触发该事件

这基本上是一个观察者模式

关于java - 需要帮助让 JRadioButton 将值返回到 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071253/

相关文章:

java - LinearLayout 在显示时进行动画处理

java - 如何从另一个网站获取字段名称和值?

Java:在方法中引用点运算符之前的对象

java - 加载 Java JTable : Why does it not work?

Java Swing 矩形2D : how to select the one on the top

java - 使用 JtextField 创建自定义 JRadioButton?

java - 扩展 CrudRepository 并仍然使用 Spring Data JPA 为自定义方法创建自己的实现

java - 如何使用布局而不是低级坐标在 Java 中进行打印?

java - 如何显示 JRadio 按钮被选中和取消选中?

java - 我可以为枚举中的每个值添加一个 JRadioButton 吗?