【问题标题】:increment a value when a button is clicked and update a text field with that value单击按钮时增加一个值并使用该值更新文本字段
【发布时间】:2015-04-22 03:29:34
【问题描述】:

我被困在一个任务中,每次用户点击一个按钮时,我都需要更新一个文本字段。总共有 5 个按钮,每个按钮都有自己的文本字段,单击它们时应更新。我遇到的问题是多次单击时计数器似乎没有更新文本字段。所以我第一次单击按钮时,文本字段会显示“1”,但多次单击后仍然如此。

private class ButtonListener implements ActionListener 
   {                                                     
      public void actionPerformed(ActionEvent e)
      {
          int snickers = 0;
          int butterfinger = 0;
          int lays = 0;
          int coke = 0;
          int dietCoke = 0;
          int totalItems = 0;
          double totalPrice = (totalItems * PRICE);

          if (e.getSource() == snickersButton)   
          {
                 totalItems++;                    

                 snickers++;                     
                 quantityTextS.setText(String.valueOf(snickers));        //Display snickers value in text field
                 itemsSelectedText.setText(String.valueOf(totalItems));  //Display total items value in text field 

              if(snickers > MAX)                
              {  
                  JOptionPane.showMessageDialog(null, "The maximum number of each item that can be selected is 3.", 
                  "Invalid Order Quantity", JOptionPane.ERROR_MESSAGE);
                  quantityTextS.setText("3");     //Set text to 3 
              }
          }

【问题讨论】:

    标签: java swing actionlistener textfield


    【解决方案1】:

    您所有的“计数器”都是局部变量,因此每次调用 actionPerformed 时都会重新初始化它们

    您应该改为使用计数器实例字段...

    private class ButtonListener implements ActionListener 
       {                                                     
          private int snickers = 0;
          private int butterfinger = 0;
          private int lays = 0;
          private int coke = 0;
          private int dietCoke = 0;
          private int totalItems = 0;
          public void actionPerformed(ActionEvent e)
          {
              double totalPrice = (totalItems * PRICE);
    
              if (e.getSource() == snickersButton)   
              {
    

    不过,这假设您对所有按钮都使用相同的 ButtonListener 实例

    查看Understanding Class Members了解更多详情

    【讨论】:

    • 是的,我在每个按钮上使用相同的 ButtonListener。非常感谢,我的柜台现在正在工作。
    【解决方案2】:

    为您的按钮添加动作监听器

    buttonName.addActionListener(this);
    

    另外,将保存要显示的值的变量写入全局变量。不要在函数内部声明

    试试这个代码:

    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class DrawOval implements ActionListener
    {
    
        JButton genderBtn = new JButton("gender");
        JFrame frm = new JFrame();  
        JTextField displayTF = new JTextField(15);
    
        int i = 0;
    
    
    
        public DrawOval(){
    
            displayTF.setText(""+i);
            frm.setTitle("Grocery");
            frm.setVisible(true);
            frm.setSize(200,100);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setResizable(true);
            frm.setLayout(new FlowLayout());
            frm.add(displayTF);
            frm.add(genderBtn);
            genderBtn.addActionListener(this);
        }
    
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource() == genderBtn){
                i++;
                displayTF.setText(""+i);
            }
    
        }
        public static void main(String args[]){
            new DrawOval();
        }
    
    
    }
    

    【讨论】:

      【解决方案3】:

      这是因为您将snickerstotalItems 声明为actionPerformed 方法的本地字段,因此每次单击时它们都会被创建并初始化为0。考虑以下方法之一:

      1. 将这些字段设为类的静态字段
      2. 从当前按钮中获取 snickerstotalItems,将它们解析为 int 值并根据这些值进行计算

      【讨论】:

        猜你喜欢
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多