java - 如何使用 SwingWorker 从无 gui 类更新 Java Jframe 控件
问题描述
我已经问过这个问题,但需要更多细节。
无法测试提出的答案,因为当 Windows 应用程序启动时,它看起来像大小 0,0,而当我最大化时,没有绘制控件。
无论如何,这是我原来的问题:
我想要做的(并寻找如何做)是
ListBox
从实时的无 GUI 类中添加元素,或者换句话说“异步”,而不冻结我的应用程序。这清楚吗?我尝试SwingWorker
了线程,但没有结果。我所能做的就是在所有进程完成后更新列表框(显然我的应用程序冻结了,因为我的进程很长)。这是我的架构:
- 项目
- __控制器
- __商业
- __Util
- __看法
这是我的代码(试图重现提出的解决方案)
视图(使用 NetBeans 生成)
package view;
import MyController;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
public class MyView extends javax.swing.JFrame {
static MyController controller;
public MyView(DefaultListModel<String> model) {
initComponents();
pack();
setVisible(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnRun = new javax.swing.JButton();
jscrlLog = new javax.swing.JScrollPane();
jlstLog = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnRun.setText("Run");
btnRun.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRunActionPerformed(evt);
}
});
jscrlLog.setViewportView(jlstLog);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(159, 159, 159)
.addComponent(btnRun)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jscrlLog, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnRun)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jscrlLog, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {
controller.runProcess();
}
public void addButtonListener(ActionListener listener) {
btnRun.addActionListener(listener);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
controller = new MyController();
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnRun;
private javax.swing.JList jlstLog;
private javax.swing.JScrollPane jscrlLog;
// End of variables declaration
}
商业
package business;
import MyLog;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.SwingWorker;
public class MyBusiness {
private int counter = 0;
private DefaultListModel<String> model;
private MyLog log;
public MyBusiness(DefaultListModel<String> model) {
this.model = model;a
}
public void runProcess() {
SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
publish("log message number " + counter++);
Thread.sleep(2000);
}
return null;
}
@Override
protected void process(List<String> chunks) {
// this is called on the Swing event thread
for (String text : chunks) {
model.addElement("");
}
}
};
worker.execute();
}
}
日志(模型)
package util;
import javax.swing.DefaultListModel;
public class MyLog {
private DefaultListModel<String> model;
public MyLog() {
model = new DefaultListModel<String>();
}
public DefaultListModel<String> getLog(){
return model;
}
}
解决方案
当 Windows 应用程序启动时,它看起来像大小 0,0,当我最大化时,没有绘制控件。
public MyView(DefaultListModel<String> model) {
setVisible(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
我所看到的只是你使框架可见。我看不到您在哪里向框架添加了组件。那是你在哪里调用过initComponents()?
在使框架可见之前,需要将组件添加到框架中。并且您需要在框架可见之前对其进行打包()。
推荐阅读
- python - 英特尔实感 Mac OS 安装
- javascript - 如何在 Puppeteer 中单击并按住
- html - 居中文本输入
- python - 如何删除作为 Python 列表元素的字符串的一些前导字符?
- python-3.8 - psutil.STATUS_DEAD 状态是否与 psutil.STATUS_ZOMBIE 相同?
- php - 删除wordpress中的第一行html源代码
- stripe-payments - 没有这样的令牌:'btok_1JGm2oKFR93cFUSYN2bui4Yb' 当我尝试为 Stripe 客户创建银行账户时
- javascript - 无法使用官方示例在 Slate.js 编辑器中清楚地删除链接
- sql - Laravel query where field is less than sum of another field in related table (polymorphic relationship)
- algorithm - 找到强连通图,使得最大和最小边之间的差异最小