我有jTextfield和jButton ..
如何
>用户可以点击jTextfield(鼠标可以在jtextfield上输入/退出),但如果用户输入内容,它将不会执行任何操作(除了将删除整个文本的退格键)
>当用户点击按钮时,它会
jTextfield.setText(“something”);
所以给jtextfield文本的唯一方法是单击按钮
>当那里有文本时(当光标在jtextfield中时)然后用户输入退格键,它将删除jtextfield上的整个文本.
这该怎么做?
请原谅我的英文..
非常感谢任何帮助..
解决方法:
使用DocumentFilter,只需将其添加到您的JTextField,如下所示:
public class Test {
public void initComponents() {
//create frame
//add DoucmentFilter to JTextField
MyDocumentFilter myFilter = new MyDocumentFilter();
JTextField myArea = new JTextField();
((AbstractDocument)myArea.getDocument()).setDocumentFilter(myFilter);
//add components set frame visible
}
}
class MyDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
super.replace(fb, i, i1, string, as);
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
}
或者
您可能想要创建一个已经具有DocumentFilter(用于可重用性)的custom JTextField,例如:
public class MyCustomField extends JTextField {
public MyCustomField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return ((Document) new MyDocument());
}
static class MyDocument extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
super.replace(fb, i, i1, string, as);
}
}
}
从气垫船编辑
我正在考虑这些问题
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.*;
public class Test {
public void initComponents() {
JPanel panel = new JPanel();
final MyDocumentFilter myFilter = new MyDocumentFilter();
final JTextField myArea = new JTextField(20);
((AbstractDocument) myArea.getDocument()).setDocumentFilter(myFilter);
panel.add(myArea);
panel.add(new JButton(new AbstractAction("Set Text") {
@Override
public void actionPerformed(ActionEvent arg0) {
myFilter.setFiltering(false);
myArea.setText("Fe Fi Fo Fum");
myFilter.setFiltering(true);
}
}));
JOptionPane.showMessageDialog(null, panel);
// add components set frame visible
}
public static void main(String[] args) {
new Test().initComponents();
}
}
class MyDocumentFilter extends DocumentFilter {
private boolean filtering = true;
@Override
public void replace(FilterBypass fb, int i, int i1, String string,
AttributeSet as) throws BadLocationException {
if (!filtering) {
super.replace(fb, i, i1, string, as);
}
}
@Override
public void remove(FilterBypass fb, int i, int i1)
throws BadLocationException {
int offset = 0;
int length = fb.getDocument().getLength();
super.remove(fb, offset, length);
}
@Override
public void insertString(FilterBypass fb, int i, String string,
AttributeSet as) throws BadLocationException {
if (!filtering) {
super.insertString(fb, i, string, as);
}
}
public void setFiltering(boolean filtering) {
this.filtering = filtering;
}
}
标签:java,swing,jtextfield
来源: https://codeday.me/bug/20190725/1536619.html