gpt4 book ai didi

java - 如何从另一个 JtextField 更新 JTextField

转载 作者:行者123 更新时间:2023-11-30 03:43:36 50 4
gpt4 key购买 nike

我正在制作一个用户界面,其中有两个 jtextfields。我想要的是当用户输入第一个jtextfield 它应该以 1 的增量镜像在第二个 jtextfield 中,并且如果用户在第二个 JtextField 中输入,它应该镜像到第一个 jtextfield 并减少1.但是我收到类似 IllegalStateException:Attempt to mutate in notification. 的错误。

示例代码:

public class Sh extends JFrame 
{
public Sh()
{
super("SH");
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);

final JTextField field = new JTextField(10);
panel.add(field);

final JTextField field1 = new JTextField(10);
panel.add(field1);

DocumentListener docListener = new DocumentListener(){

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

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

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

private void updateLabel(DocumentEvent e) {
String text = field.getText();
String text1 = field1.getText();

if (!text.isEmpty()) {
int p = Integer.parseInt(text);
int i = (p + 1);
String s = String.valueOf(i);
field1.setText(s);
} else if (!text1.isEmpty()) {
int p = Integer.parseInt(text1);
int i = (p - 1);
String s = String.valueOf(i);
field.setText(s);
}

}
};
field.getDocument().addDocumentListener(docListener);
field1.getDocument().addDocumentListener(docListener);

DocumentFilter docFilter = new DocumentFilter(){
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException {
fb.insertString(off, str.replaceAll("\\D++", ""), attr); // remove non-digits
}

@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException {
fb.replace(off, len, str.replaceAll("\\D++", ""), attr); // remove non-digits
}
};
((AbstractDocument) field.getDocument()).setDocumentFilter(docFilter);
((AbstractDocument) field1.getDocument()).setDocumentFilter(docFilter);

}
public static void main(String[] args)
{
Sh s = new Sh();
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
s.pack();
s.setVisible(true);
}
}

最佳答案

根据 MadProgrammer 的提议这是解决方案:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;

/**
* <code>Sh</code>.
*
* @author SMedvynskyy
*/
public class Sh extends JFrame
{
public Sh()
{
super("SH");
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);

final JTextField field = new JTextField(10);
panel.add(field);

final JTextField field1 = new JTextField(10);
panel.add(field1);

final DocumentListener docListener = new DocumentListener(){

private Document originator;

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

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

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

private void updateLabel(DocumentEvent e) {
if (null == originator) {
originator = e.getDocument();
String text = "";
try {
text = originator.getText(0, originator.getLength());
} catch (final Exception ex) {
ex.printStackTrace();
}

if (!text.isEmpty()) {
final int p = Integer.parseInt(text);
if (originator.equals(field.getDocument())) {
final int i = (p + 1);
final String s = String.valueOf(i);
field1.setText(s);
} else {
final int i = (p - 1);
final String s = String.valueOf(i);
field.setText(s);
}
} else {
field.setText(text);
field1.setText(text);
}

originator = null;
}

}
};
field.getDocument().addDocumentListener(docListener);
field1.getDocument().addDocumentListener(docListener);

final DocumentFilter docFilter = new DocumentFilter(){
@Override
public void insertString(FilterBypass fb, int off, String str, AttributeSet attr)
throws BadLocationException {
fb.insertString(off, str.replaceAll("\\D++", ""), attr); // remove non-digits
}

@Override
public void replace(FilterBypass fb, int off, int len, String str, AttributeSet attr)
throws BadLocationException {
fb.replace(off, len, str.replaceAll("\\D++", ""), attr); // remove non-digits
}
};
((AbstractDocument) field.getDocument()).setDocumentFilter(docFilter);
((AbstractDocument) field1.getDocument()).setDocumentFilter(docFilter);

}
public static void main(String[] args)
{
final Sh s = new Sh();
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
s.pack();
s.setVisible(true);
}
}

方法 JTextField.setText 会引发 DocumentEvent,因此如果您不特别避免它 - 您将遇到此问题(或无限递归)。

关于java - 如何从另一个 JtextField 更新 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26274827/

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