所以,我完成了一个数独解算器的制作,但我想要改进它。为此,我需要以某种方式从documentListener访问我的betterJTextField。我正在使用documentListener从我的betterJTextFields中实时读取数据,问题是在insertUpdate(DocumentEvent e)中。
我需要联系到发生DocumentEvent的betterJTextfield。例如,如果输入无效,betterJTextfield将变为红色等。
如果你想知道,我把我所有的betterJTextfield都放在一个矩阵里。每个字段处理数独中的一个数字。
@Override
public void insertUpdate(DocumentEvent e) {
//Removed code which checks if the input in the betterJTextField is fine.
}(JFormattedTextfield扩展了JTextField)
public class betterJTextField extends JFormattedTextField {
private int row;
private int column;
public betterJTextField(Format format, int row, int column) {
super(format);
this.row = row;
this.column = column;
// TODO Auto-generated constructor stub
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}发布于 2013-03-21 06:17:04
我真的不完全理解你在问什么,但我相信这就是你想要的:
private static class RedDocumentListener implements DocumentListener {
private JTextField textField;
public RedDocumentListener(JTextField textField) {
this.textField = textField;
}
@Override
public void insertUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
@Override
public void removeUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
@Override
public void changedUpdate(DocumentEvent e) {
textField.setBackground(Color.red);
}
}https://stackoverflow.com/questions/15535679
复制相似问题