gpt4 book ai didi

java - 检查文本退格问题

转载 作者:行者123 更新时间:2023-11-30 08:16:38 25 4
gpt4 key购买 nike

我在禁用按钮的地方做了它,启用它的唯一方法是在字段中键入文本。

这是我的代码:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

class checkText extends DocumentFilter {
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
super.replace(fb, offset, length, text, attrs);
main.enableButton();
}
}

public class main extends JFrame {
static JFrame inputFrame = new JFrame();
static JTextField myTextfield = new JTextField(10);
static JButton myButton = new JButton("Test");

public main() {
inputGUI();
}

private static void inputGUI() {
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputFrame.setTitle("The INPUT");
inputFrame.setLayout(new FlowLayout());
inputFrame.setSize(640, 480);
inputFrame.setVisible(true);
inputFrame.setLocationRelativeTo(null);

inputFrame.add(myButton);

DocumentFilter filter = new checkText();
((AbstractDocument) myTextfield.getDocument()).setDocumentFilter(filter);
inputFrame.add(myTextfield);
myButton.setEnabled(false);
}

public static void enableButton() {
myButton.setEnabled(true);
}

public static void main(String args[]) { new main(); }
}

每当我退格并删除所有文本时。该按钮仍然有效。当字段中没有文本时,如何禁用它?

最佳答案

您的 DocumentFilter 从不检查 Document 或过滤器的方法会对 Document 产生什么影响,因此您不应该对它不起作用感到惊讶。您也只重写了三种 DocumentFilter 方法中的一种。此外,您的主类有一个 enableButton() 但外部类无法禁用该按钮。 ....

我自己,我不会使用 DocumentFilter,而是使用 DocumentListener,因为您想在文本更改被注册后之后检查文档,而不是之前,因此使用 DocumentFilter 只会混淆问题。我只需检查文档中文本的长度,就可以了。要获取文档,请对传递到所有重写方法的 DocumentEvent 对象调用 getDocument()。然后只需对此调用 getLength()。如果大于 0,则启用该按钮,否则禁用它。

例如,

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;


public class Main2 extends JPanel {
private JButton testButton = new JButton("Test");
private JTextField textField = new JTextField(11);

public Main2() {
add(testButton);
add(textField);

testButton.setEnabled(false);
textField.getDocument().addDocumentListener(new DocumentListener() {

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

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

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

private void checkDoc(DocumentEvent e) {
Document doc = e.getDocument();
testButton.setEnabled(doc.getLength() > 0);
}
});
}

private static void createAndShowGui() {
Main2 mainPanel = new Main2();

JFrame frame = new JFrame("Main2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 检查文本退格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com