本文介绍了当点击添加按钮时,Jtextfield从B3001开始递增1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bill_number(使用jtextfield)必须自动递增1。我需要B3001,B3002等格式的sequtenial值....请帮助我



我尝试了什么:



bill_number(using jtextfield) must auto increment by 1 after clicking submit button. I need sequtenial values in format like B3001,B3002, and so on....kindly plz help me

What I have tried:

public static class SequentialNumber
{
       private static int currentNumber=3000;
       public static String GetNextNumber()
       {
            currentNumber++;
            return "B"+currentNumber;
       }
}
public Printbill() {


    contentPane1 = new JPanel();
    contentPane1.setBackground(Color.WHITE);
    contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));

    contentPane1.setLayout(null);



    dcfield = new JTextField();

    dcfield.setBounds(553, 109, 86, 20);
    contentPane1.add(dcfield);
    dcfield.setColumns(10);
    String ContractNo=SequentialNumber.GetNextNumber();
    dcfield.setText(ContractNo);
    JButton btnAdd = new JButton("ADD");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
          Printbill p=new Printbill();
          dispose();
            p.setVisible(true);
            SequentialNumber s=new SequentialNumber();
            String stt=s.GetNextNumber();
           dcfield.setText(stt);
        }
    });

     }
            }

推荐答案

class SequentialNumber
{
  private static int currentNumber = 3000;
  public static String GetNextNumber()
  {
    currentNumber++;
    return "B"+currentNumber;
  }
}

public class program
{
  public static void main(String args[])
  {
    for (int n=0; n<20; ++n)
    {
      String sn =  SequentialNumber.GetNextNumber();
      System.out.printf("sequential_number[%d] = %s\n", n, sn);
    }
  }
}


这篇关于当点击添加按钮时,Jtextfield从B3001开始递增1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:16