Validate JTextField value so that it starts with “RA” then has 8 digits

前端 未结 1 1006
小鲜肉
小鲜肉 2021-01-23 07:09

I have a JTextField where in the user has to input the data. Its value has to always start with RA and must have exactly 8 digits after it. So, its l

相关标签:
1条回答
  • 2021-01-23 07:49

    I'd use a JSpinner for this, and simply prefix RA to the number. E.G.

    Image

    enter image description here

    Typical Output

    RA8007006
    

    Code

    import java.awt.*;
    import javax.swing.*;
    
    class CaptureRA {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    String prefix = "RA";
                    JPanel gui = new JPanel(new FlowLayout(4));
                    gui.add(new JLabel(prefix));
                    SpinnerModel ints = new SpinnerNumberModel(
                            1000000,1000000,99999999,1);
                    JSpinner spinner = new JSpinner(ints);
                    gui.add(spinner);
    
                    JOptionPane.showMessageDialog(null, gui);
                    System.out.println(prefix + ints.getValue());
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
            SwingUtilities.invokeLater(r);
        }
    }
    
    0 讨论(0)
提交回复
热议问题