在 java swing 中向 JTextfield 添加列

Adding columns to JTextfield in java swing

我必须创建一个 Jtextfield,用户将在其中输入大约 30 个 characters.i 已经创建了 15 个单独的 Jtextfield 并让用户在每个 Jtextfield 中仅输入 2 个字符 Jtextfield.when 用户输入两个第一个字段中的字符它会自动跳转到下一个字段并继续直到达到 30 个字符。

我的问题是,是否有更好的方法来创建 Jtextfield.Instead 创建单独的 15 个 Jtextfields 我们可以创建一个 Jtextfield 并将其划分为 15 个分区并提供相同的字符输入流程。 如果这不是一个好的解决方案,请帮助我提供更好的解决方案。 提前致谢

我的代码

    cmd1=new JLabel("Command");
    cmd1.setBounds(20, 76, 70, 30);
    cmd1.setFont(new Font("serif",Font.BOLD,13));
    commandpanel.add(cmd1);

    cmdtext1=new JTextField();
    cmdtext1.setBounds(80, 80,70,25);
    commandpanel.add(cmdtext1);

    examplelabel=new JLabel("Enter HexaDecimal values (For example : 45,1E) ");
    examplelabel.setBounds(260, 50, 250,25);
    examplelabel.setFont(new Font("serif",Font.PLAIN,12));
    commandpanel.add(examplelabel);


    delete1=new JButton("Delete");
    delete1.setBounds(606, 80, 70, 25);
    commandpanel.add(delete1);


    command11=new JTextField(2);
    command11.setBounds(155,80,25,25);      
    command11.setDocument(new JTextFieldLimit(2));
    command11.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command11.getText().length() == 2)
            {
                command12.requestFocus();
            }

        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command11);

    command12=new JTextField();
    command12.setBounds(180,80,25,25);
    command12.setDocument(new JTextFieldLimit(2));
    command12.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command12.getText().length() == 2)
            {
                command13.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command12);

    command13=new JTextField();
    command13.setBounds(205,80,25,25);
    command13.setDocument(new JTextFieldLimit(2));
    command13.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command13.getText().length() == 2)
            {
                command14.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command13);

    command14=new JTextField();
    command14.setBounds(230,80,25,25);
    command14.setDocument(new JTextFieldLimit(2));
    command14.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command14.getText().length() == 2)
            {
                command15.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command14);

    command15=new JTextField();
    command15.setBounds(255,80,25,25);
    command15.setDocument(new JTextFieldLimit(2));
    command15.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command15.getText().length() == 2)
            {
                command16.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command15);

    command16=new JTextField();
    command16.setBounds(280,80,25,25);
    command16.setDocument(new JTextFieldLimit(2));
    command16.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command16.getText().length() == 2)
            {
                command17.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command16);

    command17=new JTextField();
    command17.setBounds(305,80,25,25);
    command17.setDocument(new JTextFieldLimit(2));
    command17.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command17.getText().length() == 2)
            {
                command18.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command17);

    command18=new JTextField();
    command18.setBounds(330,80,25,25);
    command18.setDocument(new JTextFieldLimit(2));
    command18.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command18.getText().length() == 2)
            {
                command19.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command18);

    command19=new JTextField();
    command19.setBounds(355,80,25,25);
    command19.setDocument(new JTextFieldLimit(2));
    command19.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command19.getText().length() == 2)
            {
                command110.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command19);

    command110=new JTextField();
    command110.setBounds(380,80,25,25);
    command110.setDocument(new JTextFieldLimit(2));
    command110.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command110.getText().length() == 2)
            {
                command111.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command110);

    command111=new JTextField();
    command111.setBounds(405,80,25,25);
    command111.setDocument(new JTextFieldLimit(2));
    command111.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command111.getText().length() == 2)
            {
                command112.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command111);

    command112=new JTextField();
    command112.setBounds(430,80,25,25);
    command112.setDocument(new JTextFieldLimit(2));
    command112.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command112.getText().length() == 2)
            {
                command113.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command112);

    command113=new JTextField();
    command113.setBounds(455,80,25,25);
    command113.setDocument(new JTextFieldLimit(2));
    command113.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command113.getText().length() == 2)
            {
                command114.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command113);

    command114=new JTextField();
    command114.setBounds(480,80,25,25);
    command114.setDocument(new JTextFieldLimit(2));
    command114.addKeyListener(new KeyListener()
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            // TODO Auto-generated method stub
            if(command114.getText().length() == 2)
            {
                command115.requestFocus();
            }
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub

        }
    });
    commandpanel.add(command114);

    command115=new JTextField();
    command115.setBounds(505,80,25,25);       
    command115.setDocument(new JTextFieldLimit(2));
    commandpanel.add(command115);

    add1=new JButton("Add");
    add1.setBounds(547, 80,56,25);
    add1.addActionListener(new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
         add1();

        }

    });
    commandpanel.add(add1);

我想这就是您要找的。没看完但你明白了!

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JComponentLayout extends JFrame implements KeyListener{
    JLabel cmd1;
    JButton add1;
    JTextField cmdtext1;
    JButton delete1;
    JLabel examplelabel;


    Vector<Component> fields = new Vector<Component>(15);
    JPanel commandpanel = new JPanel(new FlowLayout());
   public static void main(String [] args){
       new JComponentLayout();
   }
   public JComponentLayout(){
  cmd1=new JLabel("Command");
    cmd1.setBounds(20, 76, 70, 30);
    cmd1.setFont(new Font("serif",Font.BOLD,13));
    commandpanel.add(cmd1);

    cmdtext1=new JTextField();
    cmdtext1.setBounds(80, 80,70,25);
    commandpanel.add(cmdtext1);

    examplelabel=new JLabel("Enter HexaDecimal values (For example : 45,1E) ");
    examplelabel.setBounds(260, 50, 250,25);
    examplelabel.setFont(new Font("serif",Font.PLAIN,12));
    commandpanel.add(examplelabel);


    for(int e = 0;  e < 15; ++e){
        JTextField command = new JTextField(2);
        command.addKeyListener(this);
        commandpanel.add(command);
        fields.add(command);
    }


    delete1=new JButton("Delete");
    delete1.setBounds(606, 80, 70, 25);
    commandpanel.add(delete1);


    add1=new JButton("Add");
    add1.setBounds(547, 80,56,25);
    add1.addActionListener(new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e){
            // TODO Auto-generated method stub
        }

    });

    commandpanel.add(add1);
    this.add(commandpanel);
    this.setVisible(true);
   }



    @Override
    public void keyTyped(KeyEvent e) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e){
        JTextField source = (JTextField)e.getSource();

        // TODO Auto-generated method stub
        if(source.getText().length() == 2){
            fields.get((1 + fields.indexOf(source)) 
            % fields.size()).requestFocus();
        }       
    }

    @Override
    public void keyPressed(KeyEvent e) 
    {
        // TODO Auto-generated method stub

    }
}

how to print the value 55-55 when entered

默认情况下,格式化文本字段只会以有效格式保存数据。要强制保存不完整的数据,您需要:

MaskFormatter format = new MaskFormatter("##-##-##-##-##");
JFormattedTextField textField = new JFormattedTextField( format );
textField.setFocusLostBehavior( JFormattedTextField.COMMIT );