gpt4 book ai didi

java - 您可以通过在后台运行中调用的方法使用发布吗?

转载 作者:行者123 更新时间:2023-12-01 11:58:48 25 4
gpt4 key购买 nike

我正在使用 swingworker 在后台运行一个方法,并定期用信息更新 gui,但从我发现的情况来看,无法从另一个类调用发布。这是我的 Swingworker 的名称:

private void start() {
worker = new SwingWorker <Void, String>() {

@Override
protected Void doInBackground() throws Exception {
navigator.navigator();

return null;
}
@Override
protected void process(List<String> chunks) {
for (String line : chunks) {
txtrHello.append(line);
txtrHello.append("\n");
}
}
@Override
protected void done() {

}

};
worker.execute();
}

现在我想从导航器方法调用 publish(String);,我该怎么做?将我的所有方法移至 doInBackground() 中是不可能的。

最佳答案

可能的解决方案是向 Navigator 对象添加一个观察者,关键是以某种方式允许 Navigator 与其状态已更改的任何监听器(此处为 SwingWorker)进行通信:

  • 为 Navigator 提供一个 PropertyChangeSupport 对象以及 addPropertyChangeListener(PropertyChangeListener listener)将传入的监听器添加到支持对象的方法。
  • 为 Navigator 提供某种类型的“绑定(bind)”属性,该属性在其状态发生更改时通常位于 setXxxx(...) 中。 type 方法,通知支持对象此更改。
  • 然后在 SwingWorker 构造函数中,将 PropertyChangeListener 添加到 Navigator 对象。
  • 在此监听器中,使用 Navigator 对象中的新数据调用发布方法。
<小时/>

例如:

import java.awt.event.ActionEvent;
import java.beans.*;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class PropChangeSupportEg extends JPanel {
private MyNavigator myNavigator = new MyNavigator();
private JTextField textField = new JTextField(10);

public PropChangeSupportEg() {
textField.setFocusable(false);
add(textField);
add(new JButton(new StartAction("Start")));
add(new JButton(new StopAction("Stop")));
}

private class StartAction extends AbstractAction {
public StartAction(String name) {
super(name);
}

@Override
public void actionPerformed(ActionEvent e) {
if (myNavigator.isUpdatingText()) {
return; // it's already running
}
MyWorker worker = new MyWorker();
worker.execute();
}
}

private class StopAction extends AbstractAction {
public StopAction(String name) {
super(name);
}

@Override
public void actionPerformed(ActionEvent e) {
myNavigator.stop();
}
}

private class MyWorker extends SwingWorker<Void, String> {
@Override
protected Void doInBackground() throws Exception {
if (myNavigator.isUpdatingText()) {
return null;
}

myNavigator.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (MyNavigator.BOUND_PROPERTY_TEXT.equals(evt.getPropertyName())) {
publish(evt.getNewValue().toString());
}
}
});
myNavigator.start();
return null;
}

@Override
protected void process(List<String> chunks) {
for (String chunk : chunks) {
textField.setText(chunk);
}
}


}

private static void createAndShowGui() {
PropChangeSupportEg mainPanel = new PropChangeSupportEg();

JFrame frame = new JFrame("Prop Change Eg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class MyNavigator {
public static final String BOUND_PROPERTY_TEXT = "bound property text";
public static final String UPDATING_TEXT = "updating text";
private static final long SLEEP_TIME = 1000;
private PropertyChangeSupport pcSupport = new PropertyChangeSupport(this);
private String boundPropertyText = "";
private String[] textArray = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private int textArrayIndex = 0;
private volatile boolean updatingText = false;

public void start() {
if (updatingText) {
return;
}
updatingText = true;
while (updatingText) {
textArrayIndex++;
textArrayIndex %= textArray.length;
setBoundPropertyText(textArray[textArrayIndex]);
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {}
}
}

public void stop() {
setUpdatingText(false);
}

public String getBoundPropertyText() {
return boundPropertyText;
}

public boolean isUpdatingText() {
return updatingText;
}

public void setUpdatingText(boolean updatingText) {
boolean oldValue = this.updatingText;
boolean newValue = updatingText;
this.updatingText = updatingText;
pcSupport.firePropertyChange(UPDATING_TEXT, oldValue, newValue);
}

public void setBoundPropertyText(String boundPropertyText) {
String oldValue = this.boundPropertyText;
String newValue = boundPropertyText;
this.boundPropertyText = boundPropertyText;
pcSupport.firePropertyChange(BOUND_PROPERTY_TEXT, oldValue, newValue);
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
}

关于java - 您可以通过在后台运行中调用的方法使用发布吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28138394/

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