当点击添加按钮时,jtextfield从B3001开始递增1

时间:2017-11-15 08:10:36

标签: java swing

点击提交按钮后,我的帐号(使用jtextfield)必须自动递增1。我需要像B3001,B3002等格式的sequtenial值....请帮助我帮助我

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

 }
}

1 个答案:

答案 0 :(得分:0)

这是工作代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

public class Printbill extends JFrame {
    public static class SequentialNumber {
        private static int currentNumber = 3000;

        public static String GetNextNumber() {
            currentNumber++;
            return "B" + currentNumber;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Printbill();
            }
        });
    }

    public Printbill() {

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

        contentPane1.setLayout(new BorderLayout(5, 5));

        final JTextField dcfield = new JTextField();

        contentPane1.add(dcfield);
        dcfield.setColumns(10);
        String contractNo = SequentialNumber.GetNextNumber();
        dcfield.setText(contractNo);
        JButton btnAdd = new JButton("ADD");
        contentPane1.add(btnAdd, BorderLayout.EAST);
        getContentPane().add(contentPane1);
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String stt = SequentialNumber.GetNextNumber();
                dcfield.setText(stt);
            }
        });
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

    }
}

您的代码出了什么问题:

  • 您尝试使用绝对布局(setLayout(null))。这是不好的做法,所以我将布局更改为BorderLayout。之后我添加了pack()来为窗口设置合适的大小(此方法仅适用于非空布局)。有关详细信息,请参阅here
  • 我已更改动作侦听器,因此它只更新文本字段。
  • 正确启动Swing应用程序需要
  • SwingUtilities.invokeLater。必须在事件调度程序线程(EDT)中执行所有窗口小部件初始化/操作。 SwingUtilities.invokeLater允许在EDT中调用一段代码。