gpt4 book ai didi

Java documentlistener - 输入后程序停止工作

转载 作者:行者123 更新时间:2023-11-30 03:55:56 25 4
gpt4 key购买 nike

您好,我的文档监听器有问题。将值插入文本字段后,程序停止工作。该程序应将第一行文本字段与第二行文本字段进行异或,并将结果放入第三行文本字段

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)

最佳答案

这一行

setJPanel(ciphertextArray, panel3, "1");

还向 ciphertextArray TextField 的每个元素添加一个 DocumentListener。因此,每次密文 TextField 的值发生变化时,都会调用方法 setCipherText()

这会导致以下序列:

  1. 您在 arrayIVplainTextArrayTextField 之一中输入新值
  2. setCipherText() 被调用
  3. setCipherText() 更改密文的值 TextField
  4. setCipherText() 再次被调用(解释见上文)。返回2。

这就是为什么你会得到IllegalStateException

因此,解决方案是不要将 DocumentListener 添加到 ciphertextArray 中的 TextFields

解决方案可以按如下方式实现:

public void setJPanel(JTextField array[], JPanel container, String s, boolean docListener) {
for (int i = 0; i < 8; i++) {
array[i] = new JTextField(s, 4);
if (docListener) {
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]);
}
}

[...]

setJPanel(arrayIV, panel1, "1a", true);
setJPanel(plaintextArray, panel2, "1b", true);
setJPanel(ciphertextArray, panel3, "1", false);

关于Java documentlistener - 输入后程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180528/

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