当前位置: 首页 > 知识库问答 >
问题:

在java swing中向JTextfield添加列

令狐经武
2023-03-14

我必须创建一个Jtextfield,用户将在其中输入大约30个字符。我已经创建了15个单独的Jtextfield,并让用户在每个Jtextfield中只输入2个字符。当用户在第一个字段中输入两个字符时,它会自动跳转到下一个字段,并持续到30个字符。

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

我的代码

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

共有2个答案

罗新
2023-03-14

输入时如何打印值55-55

默认情况下,格式化文本字段将仅以有效格式保存数据。要强制保存所需的不完整数据,请执行以下操作:

MaskFormatter format = new MaskFormatter("##-##-##-##-##");
JFormattedTextField textField = new JFormattedTextField( format );
textField.setFocusLostBehavior( JFormattedTextField.COMMIT );
韩琛
2023-03-14

我想这就是你要找的。没有完成它,但你明白了!

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

    }
}
 类似资料:
  • 我已经有了一个非常基本的JavaSwing程序,但我意识到我可以使用一个简单的用户名/密码登录屏幕来限制只有具有用户名和密码的人才能访问它。 无论如何,我可以在main方法的开头插入一些代码,以防止在出现的屏幕上输入用户名和密码之前执行?

  • 问题内容: 我有以下形式的日期作为MySQL查询的一部分返回 我想将变量$ Date2设置为$ Date5,如下所示: 等等.. 以便它返回,等等。 我努力了 但这给了我约会之前。 什么是以“ Ymd”格式获取日期的正确方法,以便它们可以在其他查​​询中使用? 问题答案: 您要做的就是使用而不是这样: 并正确输出:

  • 我是java swing的新手,现在我使用NetBeant使用NetBeans中内置的拖放设计器构建GUI。我现在面临的问题是,我有下面发布的代码,它是在Eclipse中编写的,正如您所看到的,我有一个扩展JPanel的类,我将从WEB_CAM捕获的图像添加到该JPanel中。现在,自从我切换到NetBeans,我使用设计器创建了JFrame、JPanel和JButtons,在我用NetBeans

  • 我很好奇,在放置组件时,如何使用miglaway在网格上均匀分布组件: 例如,如果我在一个按钮组中放置四个JRadioButton以跨越3行2列(2,3),然后放置三个JButton,则分布的权重是不相等的,我最终会得到以下结果: 如图所示,A和B的大小相同,C保留剩余的空间:我希望A、B、C在垂直方向上等于三分之一。 如果这在GridBagLayout中是可能的,我也可以利用它,但是我在任何一种

  • 如何使用Perl中的数字签名对pdf文件进行签名?我能够提取pdf的内容,并生成签名字符串通过 #$Content->要签名的pdf内容

  • 这是我当前的目录结构(包含在目录中): 基本上来自,我正在尝试访问。 我尝试了很多变体来工作,现在我只有: 我尝试改变路径使用,使用函数,但似乎没有工作。 当我使用生成jar并运行jar文件时,它不会显示图像。 我执行