Java GUI线程和更新
我有两个类,一个叫做GUIFrame,它保存所有图形元素,另一个叫做squeak的线程类。在GUIFrame中有两个图形元素,第一个是一个按钮,单击该按钮可启动线程:Java GUI线程和更新,java,multithreading,swing,user-interface,Java,Multithreading,Swing,User Interface,我有两个类,一个叫做GUIFrame,它保存所有图形元素,另一个叫做squeak的线程类。在GUIFrame中有两个图形元素,第一个是一个按钮,单击该按钮可启动线程: futures.put(1, pool.submit(new squeak("SqueakyThread",this.jTextArea1))); 第二个是一个javax.swing.JTextArea,带有一个名为jTextArea1的变量 提到的第二个类是名为squeak(implements Runnable)的线程类,它
futures.put(1, pool.submit(new squeak("SqueakyThread",this.jTextArea1)));
第二个是一个javax.swing.JTextArea,带有一个名为jTextArea1的变量
提到的第二个类是名为squeak(implements Runnable)的线程类,它包含while(true)循环。该类所做的只是生成一个介于1和10之间的随机数,然后将该数输出到GUIFrame类中的jtextraea1
问题:这是在Java中更新GUI元素的正确正式方式吗
我意识到这个问题已经被问了很多次了,但是有了所有不同的答案,我希望这可以为我自己和其他人提供一个简单的模板
GUIFrame.java
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class GUIFrame extends javax.swing.JFrame {
public GUIFrame() {
this.pool = Executors.newCachedThreadPool();
initComponents();
}
private ExecutorService pool;
private Map<Integer, Future<?>> futures = new HashMap<>();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 468, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
futures.put(1, pool.submit(new squeak("SqueakyThread",this.jTextArea1)));
}
/**
* @param args the command line arguments
*/
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(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUIFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.SwingUtilities;
public class squeak implements Runnable {
private String Type = "";
private javax.swing.JTextArea MW;
squeak (String type, javax.swing.JTextArea MW)
{
this.Type = type;
this.MW = MW;
}
@Override
public void run ()
{
while(true)
{
UpdateGUI(RandomNumber()+"\r\n");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e)
{
UpdateGUI("Thread is now Exiting!\r\n");
//Return cause program to exit the while(true) loop and end
return;
}
}
}
private int RandomNumber(){
Random r = new Random();
int num = r.nextInt(10-1) + 1;
return num;
}
private void UpdateGUI(final String foo) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Here, we can safely update the GUI
// because we'll be called from the
// event dispatch thread
MW.append(foo);
}
});
}
}
关于,
private void updateGUI(final String foo) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Here, we can safely update the GUI
// because we'll be called from the
// event dispatch thread
MW.append(foo);
}
});
}
是的,这绝对是允许非Swing事件线程中的代码更新Swing组件的一种方法——通过SwingUtilities.invokeLater(new Runnable(){…})
将可运行线程排队到Swing事件线程队列
另一种方法,也是我首选的方法,是使用SwingWorker,因为它内置了一种机制,允许在后台线程中运行代码,并且仍然能够在Swing事件线程上安全地进行Swing调用
我的第一个批评(我必须找到一些东西来批评,对吗?)是您的代码应该遵循Swing命名约定。例如,变量名应该以小写字母开头,而不是大写字母。如果您只是为了自己的乐趣而创建代码,那么这一点并不重要,但是如果您希望其他人查看、更新或维护您的代码,那么这一点就变得非常重要
我的第二个批评是:
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton1MouseReleased(evt);
}
});
你不应该在按钮上使用鼠标侦听器,因为它的级别太低了。使用ActionListeners,因为这是它们的用途,而且更安全。例如,如果您的代码禁用按钮的操作或其模型,则按钮不应响应,但您的代码将无法正常运行,也无法正确禁用。关于
private void updateGUI(final String foo) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Here, we can safely update the GUI
// because we'll be called from the
// event dispatch thread
MW.append(foo);
}
});
}
是的,这绝对是允许非Swing事件线程中的代码更新Swing组件的一种方法——通过SwingUtilities.invokeLater(new Runnable(){…})
将可运行线程排队到Swing事件线程队列
另一种方法,也是我首选的方法,是使用SwingWorker,因为它内置了一种机制,允许在后台线程中运行代码,并且仍然能够在Swing事件线程上安全地进行Swing调用
我的第一个批评(我必须找到一些东西来批评,对吗?)是您的代码应该遵循Swing命名约定。例如,变量名应该以小写字母开头,而不是大写字母。如果您只是为了自己的乐趣而创建代码,那么这一点并不重要,但是如果您希望其他人查看、更新或维护您的代码,那么这一点就变得非常重要
我的第二个批评是:
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
jButton1MouseReleased(evt);
}
});
你不应该在按钮上使用鼠标侦听器,因为它的级别太低了。使用ActionListeners,因为这是它们的用途,而且更安全。例如,如果您的代码禁用按钮的操作或其模型,则按钮不应响应,但您的代码将无法正常运行,也无法正确禁用。同意变量命名=))也测试了addMouseListener,如果禁用按钮,则仍然可以正常工作。我纠正了你的建议:jButton1ActionPerformed(java.awt.event.ActionEvent evt)@DevilCode:谢谢你这么做。关于你原来的问题,你还有什么问题吗?我的回答是否充分解决了您的问题,如果不是,还有什么尚不清楚?同意变量命名=))也测试了addMouseListener,如果禁用按钮,则说明您是正确的,它仍然有效。我纠正了你的建议:jButton1ActionPerformed(java.awt.event.ActionEvent evt)@DevilCode:谢谢你这么做。关于你原来的问题,你还有什么问题吗?我的回答是否充分解决了您的担忧,如果不是的话,还有什么尚不清楚的地方?1)为了更快地获得更好的帮助,请发布一篇文章。2) 源代码中只有一行空白就足够了。3) 请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用它们。1)为了更快地获得更好的帮助,请发布一篇文章。2) 源代码中只有一行空白就足够了。3) 请学习类、方法和属性名称的通用(特别是用于名称的大小写),并一致使用它们。