您好我的documentlistener有问题。 将值插入文本字段后,程序停止工作。该程序应该对具有第二行文本字段的第一行文本字段进行异或,并将结果放入第三行文本字段
package opa.beta1;
import java.awt.BorderLayout;
import java.math.BigInteger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextFieldEvent {
JTextField arrayIV[] = new JTextField[8];
JTextField plaintextArray[] = new JTextField[8];
JTextField ciphertextArray[] = new JTextField[8];
JFrame frame = new JFrame("OPA");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
public void setJPanel(JTextField array[], JPanel container, String s) {
for (int i = 0; i < 8; i++) {
array[i] = new JTextField(s, 4);
array[i].getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {
setCiphertext();
}
@Override
public void removeUpdate(DocumentEvent de) {
setCiphertext();
}
@Override
public void changedUpdate(DocumentEvent de) {
setCiphertext();
}
});
container.add(array[i]);
}
}
public String setXORText(JTextField textfield1, JTextField textfield2) {
String okno1, okno2;
okno1 = textfield1.getText();
okno2 = textfield2.getText();
if (okno1.equals("")) {
return okno2;
}
BigInteger pom1 = new BigInteger(okno1, 16);
if (okno2.equals("")) {
return okno1;
}
BigInteger pom2 = new BigInteger(okno2, 16);
BigInteger res = pom1.xor(pom2);
String s = res.toString(16);
return s;
}
public String setXORText(JTextField pole1[], JTextField pole2[], int i) {
String okno1 = pole1[i].getText();
String okno2 = pole2[i].getText();
if (okno1.equals("")) {
return pole2[i].getText();
}
BigInteger pom1 = new BigInteger(okno1, 16);
if (okno2.equals("")) {
return pole1[i].getText();
}
BigInteger pom2 = new BigInteger(okno2, 16);
BigInteger res = pom1.xor(pom2);
String s = res.toString(16);
return s;
}
public void setCiphertext() {
//textfield3.setText(setXORText(textfield1, textfield2));
for (int i = 0; i < 8; i++) {
ciphertextArray[i].setText(setXORText(arrayIV, plaintextArray, i));
}
}
public TextFieldEvent() {
setJPanel(arrayIV, panel1, "1a");
setJPanel(plaintextArray, panel2, "1b");
setJPanel(ciphertextArray, panel3, "1");
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.add(panel3, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(675, 275);
frame.setResizable(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TextFieldEvent();
}
});
}
}
堆栈跟踪
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1338) at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:659) at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718) at opa.beta1.TextFieldEvent.setCiphertext(TextFieldEvent.java:82) at opa.beta1.TextFieldEvent$1.removeUpdate(TextFieldEvent.java:33)
答案 0 :(得分:1)
将setCiphertext();
包裹在SwingUtilities.invokeLater
答案 1 :(得分:1)
这一行
setJPanel(ciphertextArray, panel3, "1");
还会为DocumentListener
ciphertextArray
的每个元素添加TextField
。因此,每当其中一个密文TextField
的值发生更改时,都会调用方法setCipherText()
。
这导致以下顺序:
TextField
或arrayIV
plainTextArray
之一中输入新值
setCipherText()
已被调用setCipherText()
更改密文TextField
setCipherText()
(上面的解释)。回到2。这就是为什么你得到IllegalStateException
。
所以解决方案是不要在DocumentListener
的{{1}}中添加TextFields
。
解决方案可以按如下方式实施:
ciphertextArray