添加 IP 文本字段的自定义类并仅添加 KeyListener 后,Backspace 停止在 Java Swing 中的任何 JTextField 中工作

问题描述 投票:0回答:1

添加以下代码后,退格键在任何 JTextField 上停止工作,当我按退格键时,没有任何反应。

import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class JIp4AddressInput extends JTextField
{
    private final char[] buff = "  0.  0.  0.  0".toCharArray();

    private int bpos;

    private void putnum (int num, int offset)
    {
        int a = num/100;
        num -= a*100;
        int b = num/10;
        num -= b*10;
        buff[offset] = (char)('0'+a);
        buff[offset+1] = (char)('0'+b);
        buff[offset+2] = (char)('0'+num);
    }

    private void align (int base)
    {
        int end = base+3;
        StringBuffer sb = new StringBuffer();
        for (int s=base; s<end; s++)
        {
            if (buff[s] != ' ')
                sb.append(buff[s]);
        }
        while (sb.length() > 1 && sb.charAt(0) == '0')
            sb.delete(0,1);
        while (sb.length() < 3)
            sb.insert(0, ' ');
        try
        {
            int num = Integer.parseInt(sb.toString().trim());
            if (num > 255)
                sb = new StringBuffer("255");
            if (num < 0)
                sb = new StringBuffer("  0");
        }
        catch (NumberFormatException e)
        {
            sb = new StringBuffer("  0");
        }
        for (int s=base; s<end; s++)
        {
            buff[s] = sb.charAt(s-base);
        }
    }

    private void alignAll()
    {
        align(0);
        align (4);
        align(8);
        align (12);
    }

    private void fwd ()
    {
        bpos = bpos == 15 ? bpos : bpos +1;
    }

    private void back ()
    {
        bpos = bpos == 0 ? bpos : bpos -1;
    }

    private void backspace()
    {
        back();
        if (bpos == 3 || bpos == 7 || bpos == 11)
        {
            return;
        }
        if (bpos < 15)
            buff[bpos] = ' ';
    }

    private void setChar (char c)
    {
        if (bpos == 3 || bpos == 7 || bpos == 11)
        {
            fwd();
        }
        if (bpos < 15)
            buff[bpos] = c;
        fwd();
    }

    public JIp4AddressInput()
    {
        super();
        setPreferredSize(new Dimension(110, 30));
        setEditable(false);

        Action beep = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
        beep.setEnabled (false);

        setText (new String (buff));

        addFocusListener(new FocusListener()
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                setText (new String (buff));
                setCaretPosition(0);
                getCaret().setVisible(true);
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                alignAll();
                setText(new String(buff));
            }
        });

        addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyTyped (KeyEvent e)
            {
                bpos = getCaretPosition();
                char c = e.getKeyChar();
                if ((c>= '0' && c<= '9') || c == ' ')
                {
                    setChar (c);
                }
                else if (c == KeyEvent.VK_BACK_SPACE)
                {
                    backspace();
                }
                else if (c == KeyEvent.VK_ENTER)
                {
                    alignAll();
                }
                setText(new String(buff));
                setCaretPosition(bpos);
            }
        });
    }

    ////////////////////////////////////////////////////////////////////////////////////////

    public InetAddress getAddress()
    {
        String[] parts = new String(buff).split("\\.");
        byte[] adr = new byte[4];
        for (int s=0; s<4; s++)
            adr[s] = (byte)Integer.parseInt(parts[s].trim());
        try {
            return InetAddress.getByAddress(adr);
        } catch (UnknownHostException e) {
            return null;
        }
    }

    public void putAddress (InetAddress in)
    {
        byte[] adr = in.getAddress();
        putnum(adr[0]&0xff, 0);
        putnum(adr[1]&0xff, 4);
        putnum(adr[2]&0xff, 8);
        putnum(adr[3]&0xff, 12);
        alignAll();
        setText (new String(buff));
    }
}

很奇怪,代码中的KeyListener应用于特定字段,而不是其他JTextField。通过调试器,我已经检查过,当我在其他字段中按 Backspace 时,不会触发 KeyListener 内部代码。但无论如何它都没有被执行,也没有应用于该字段,只是忽略它。

Java 11。

java swing keylistener backspace
1个回答
0
投票

您可能正在使用以下几行停用退格键:

Action beep = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
beep.setEnabled (false);

由于修改发生在 DefaultEditorKit 上,因此它可能是全局的,而不仅仅是您正在查看的 JTextField。

© www.soinside.com 2019 - 2024. All rights reserved.