gpt4 book ai didi

java - ActionListener 未按预期工作

转载 作者:行者123 更新时间:2023-12-02 02:56:22 24 4
gpt4 key购买 nike

EditMSFrame 类中,我有 JTextPane,其中写有 JSON。单击按钮时,我检查它是否是有效的 JSON。

现在,如果我这样写

this.checkButton.addActionListener(e -> {
System.out.println(isValid(getTextPane().getText()));
});

当我更改 JTextPane 内部的文本时,它工作正常并写入 true/false。

但是如果我这样写

this.checkButton.addActionListener(new ValidateJSON());

--

public class ValidateJSON implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {
EditMSFrame editMSframe = new EditMSFrame();
System.out.println(editMSframe.isValid(editMSframe.getTextPane().getText()));
}

}

它只会检查 JSON 一次,如果它有效,即使我将其更改为无效,它也会打印 true

最佳答案

care to make edit to my code?

老实说,我不应该这样做,这是编程101。坦率地说,你应该查阅一本好的教科书或教程,例如Passing Information to a Method or a Constructor

如果这让我成为一个 SCSS ,我很抱歉,但你会通过努力成为一个更好的编程者

public class ValidateJSON implements ActionListener {

private EditMSFrame editMSframe;

public ValidateJSON(EditMSFrame someInstance) {
editMSframe = someInstance;
}

@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(editMSframe.isValid(editMSframe.getTextPane().getText()));
}

}

关于java - ActionListener 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025458/

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