Java 单击按钮时增加一个值,并用该值更新文本字段

Java 单击按钮时增加一个值,并用该值更新文本字段,java,swing,actionlistener,textfield,Java,Swing,Actionlistener,Textfield,我被一个任务困住了,每次用户点击一个按钮时,我都需要更新一个文本字段。总共有5个按钮,每个按钮都有自己的文本字段,单击时应更新这些字段。我遇到的问题是,当多次单击时,计数器似乎不会更新文本字段。因此,我第一次单击按钮时,文本字段将显示“1”,但在多次单击后,它仍保持不变 private class ButtonListener implements ActionListener {

我被一个任务困住了,每次用户点击一个按钮时,我都需要更新一个文本字段。总共有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 
              }
          }
所有“计数器”都是局部变量,因此每次调用
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
实例


查看更多详细信息

将操作侦听器添加到按钮

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


}

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

  • 使这些字段成为类的静态字段
  • 从当前按钮中获取
    snickers
    totalItems
    ,将它们解析为int值并基于这些值进行计算

  • 是的,我在每个按钮上使用相同的按钮列表。非常感谢,我的柜台现在可以用了。