0

我有一个带有 JTextField 的 JPanel,我想实现waitForTextFieldSpace. 我认为制作自定义 JTextField 将是最优雅的解决方案,但我并不完全确定。

public class MyPanel {
   JTextField textField;

   //...constructor, methods, etc

   public void waitForTextFieldSpace() {
      //...don't return until the user has pressed space in the text field
   }
}

我确实有一些代码有一个 JFrame 等待空格键。但我不确定如何执行上面的文本字段任务。

public void waitForKey(final int key) {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == key) {
                    latch.countDown();
                }
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        try {
            //current thread waits here until countDown() is called (see a few lines above)
            latch.await();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }  
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);

    }
4

3 回答 3

4
  • 永远不要使用Keylistener(没有真正的理由,例如确定 3 个或更多按键是按下)用于JTextComponets, 使用Document, DocumentListener,DocumentFilter

  • 使用 KeyBinding 作为标准键,注意 - 必须检查 API 中是否实现了不需要的键(key_shortcut)

  • 在这种情况下,将 DocumentFilterPatern一起使用

简单的例子

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class TextAreaTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea textArea;

    public TextAreaTest() {
        textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(60, 32));
        textArea.setOpaque(true);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {

            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                //string = string.replaceAll("\\{", "\\{}");
                super.insertString(fb, offset, string, attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                if (Pattern.compile("\\p{Space}").matcher(text).find()) {
                    System.exit(0);
                }
                //text = text.replaceAll("\\{", "\\{}");
                super.replace(fb, offset, length, text, attrs);
            }
        });

        textArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update(e);
            }

            private void update(DocumentEvent e) {
                List<String> lines = getLines(textArea);
                String lastLine = lines.get(lines.size() - 1);
                int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
                        lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
                int lineHeight = getLineHeight(textArea);
                if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
                    System.out.println("Too big! Should refuse the update!");
                }
            }
        });
        getContentPane().add(textArea);
    }

    private static List<String> getLines(JTextArea textArea) {
        int lineHeight = getLineHeight(textArea);
        List<String> list = new ArrayList<String>();
        for (int num = 0;; num++) {
            int i = textArea.viewToModel(new Point(0, num * lineHeight));
            int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
            if (i == 0 && j == 0) {
                continue;
            }
            if (textArea.getDocument().getLength() == i && i == j) {
                break;
            }
            String s = removeTrailingNewLine(textArea.getText().substring(i, j));
            list.add(s);
            //System.out.println(i + " " + j + " = " + s);
        }
        return list;
    }

    private static int getLineHeight(JTextArea textArea) {
        return textArea.getFontMetrics(textArea.getFont()).getHeight();
    }

    private static String removeTrailingNewLine(String s) {
        if (s.endsWith("\n")) {
            return s.substring(0, s.length() - 1);
        } else {
            return s;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TextAreaTest test = new TextAreaTest();
                test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test.pack();
                test.setVisible(true);
            }
        });
    }
}
于 2013-01-01T20:19:33.867 回答
0

您是否考虑过使用关键事件侦听器?您可以将其添加到您的文本字段或区域并检查它的特定键。我将它用于诸如 enter 或 tab 之类的东西,因此当用户按下一个时,它会启动一个差异操作。

Java 键监听器

文档侦听器会为您工作吗?每次文本字段中的值更改时都会进行检查。

文档监听器

这是我对文本字段所做的一些示例代码,以便在按下某个键时采取行动。

textField.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(final java.awt.event.KeyEvent evt) {
        textFieldKeyPressed(evt);
    }
});

private void textFieldKeyPressed(final java.awt.event.KeyEvent evt) {
    final int key = evt.getKeyCode();
    if (key == KeyEvent.VK_ENTER || key == KeyEvent.VK_TAB) {
        if (!textField.getText().equals("Updating")) {
            loadTable();
        }
    }
}
于 2013-01-01T19:44:18.667 回答
0

正确的解决方案是创建一个自定义 JTextField 如下

public class MyTextField extends JTextField {

    private static final long serialVersionUID = 1833734152463502703L;

    public MyTextField() {

    }

    public void waitForKey(final int key) {
        final CountDownLatch latch = new CountDownLatch(1);
        KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == key) {
                    latch.countDown();
                }
                return false;
            }
        };
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
        try {
            //current thread waits here until countDown() is called (see a few lines above)
            latch.await();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }  
        KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);

    }

}

然后在原始方法中,只需在 MyTextField 对象上调用 waitForKey()

于 2013-01-01T20:15:15.130 回答