在java swing

时间:2015-08-13 05:34:00

标签: java swing jtextfield jformattedtextfield

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

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

我的代码

    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 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。没有完成,但你明白了!

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

    }
}

答案 1 :(得分:0)

  

输入时如何打印值55-55

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

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