如何将参数传递给 SwingWorker?
我正在尝试在我的 GUI 中实现 Swing Worker。目前我有一个包含按钮的 JFrame。当按下此按钮时,它应该更新显示的选项卡,然后在后台线程中运行程序。这是我到目前为止所拥有的。
class ClassA
{
private static void addRunButton()
{
JButton runButton = new JButton("Run");
runButton.setEnabled(false);
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new ClassB().execute();
}
});
mainWindow.add(runButton);
}
}
class ClassB extends SwingWorker<Void, Integer>
{
protected Void doInBackground()
{
ClassC.runProgram(cfgFile);
}
protected void done()
{
try
{
tabs.setSelectedIndex(1);
}
catch (Exception ignore)
{
}
}
}
我不明白如何传递我的 cfgFile 对象。请问有人可以就此提出建议吗?
I'm trying to implement Swing worker in my GUI. At the moment I have a JFrame containing a button. When this is pressed it should update a tab displayed and then run a program in the background thread. Here is what I have so far.
class ClassA
{
private static void addRunButton()
{
JButton runButton = new JButton("Run");
runButton.setEnabled(false);
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
new ClassB().execute();
}
});
mainWindow.add(runButton);
}
}
class ClassB extends SwingWorker<Void, Integer>
{
protected Void doInBackground()
{
ClassC.runProgram(cfgFile);
}
protected void done()
{
try
{
tabs.setSelectedIndex(1);
}
catch (Exception ignore)
{
}
}
}
I don't understand how I can pass in my cfgFile
object though. Please can someone advise on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不给它一个 File 字段并通过采用 File 参数的构造函数填充该字段?
然后像这样运行它:
Why not give it a File field and fill that field via a constructor that takes a File parameter?
And then run it like so:
使用构造函数来传递参数。例如,像这样:
}
Use constructor to pass the parameter. For example, like this:
}