[英]Input for JTextArea
Is there any way that if the char typed is not 'x'
, for example. 例如,是否有任何方式可以输入的字符不是
'x'
。 Than take no action at all. 比什么都不采取行动。 Code:
码:
main_text_area.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e1)
{
char c ='x';
if!((e1.getKeyChar() == c))
{
//Do nothing at all
}
}
public void keyReleased(KeyEvent e2)
{
}
You are much better off not using a KeyListener at all within Swing text components as this can interfere with the basic underlying operation of this component. 您最好不要在Swing文本组件中完全不使用KeyListener,因为这可能会干扰该组件的基本基础操作。 Also a KeyListener will not have any effect on text added or removed by any other means, such as by cut and paste.
另外,KeyListener不会对通过任何其他方式(例如,剪切和粘贴)添加或删除的文本产生任何影响。 Much better to use a higher level type of listener.
使用更高级别的侦听器更好。
For instance, if you want to be notified of any text changes, either input or removal then you would want to use a DocumentListener as this will notify you of any changes to the text, be it from key input or from cut and paste. 例如,如果您想收到任何文本更改(输入或删除)的通知,则您想使用DocumentListener,因为它会通知您文本的任何更改,无论是通过键盘输入还是通过剪切和粘贴。
If on the other hand you want to filter text input and even prevent certain text input, then attach a DocumentFilter to the JTextArea's Document. 另一方面,如果您想过滤文本输入甚至阻止某些文本输入,则将DocumentFilter附加到JTextArea的Document。
For instance, you could place in your DocumentFilter a String#replaceAll(...)
that removes all non-X chars from a String. 例如,您可以在DocumentFilter中放置一个
String#replaceAll(...)
,以从字符串中删除所有非X字符。 The regex "[^xX]"
would work well for this. 正则表达式
"[^xX]"
可以很好地解决这个问题。
eg, 例如,
import javax.swing.*;
import javax.swing.text.PlainDocument;
@SuppressWarnings("serial")
public class DocFilterExample extends JPanel {
private static final int ROWS = 30;
private static final int COLS = 40;
private JTextArea textArea = new JTextArea(ROWS, COLS);
public DocFilterExample() {
((PlainDocument) textArea.getDocument()).setDocumentFilter(new XcharFilter());
add(new JScrollPane(textArea));
}
private static void createAndShowGui() {
JFrame frame = new JFrame("DocFilterExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DocFilterExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class XcharFilter extends DocumentFilter {
private static final String REGEX = "[^xX]";
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
string = string.replaceAll(REGEX, "");
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
text = text.replaceAll(REGEX, "");
super.replace(fb, offset, length, text, attrs);
}
}
Yes. 是。 You have already the answer within your own question.
您已经在自己的问题中找到了答案。 The following explains it why.
以下说明其原因。 Alternatively, you can add this to
keyPressed()
, which is semantically the same to your variant: 另外,您可以将其添加到
keyPressed()
, 这与您的变体在语义上是相同的:
if(!(e1.getKeyChar() == 'x')); //Does nothing, notice the ';'!
else {
//Do something
}
To make it do nothing, you need to leave an empty body - what you have done an example of. 要使其无所事事,您需要留下一个空的身体-您所做的一个例子。 Then it meets the criterion and goes inside the body and does nothing.
然后,它符合标准并进入体内,什么也不做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.