http://i.stack.imgur.com/XvHm5.png
当我单击 On 按钮时,它应该开始在 JTextField 上发送垃圾邮件 1。而是整个 GUI 冻结,包括关闭按钮。我正在使用一个 while 循环,当您再次单击 jButton 时该循环将停止。因为 jButton 被冻结我无法停止这个程序。我已经读过线程和多线程会有所帮助,但是所有的教程都太复杂了,我无法理解。他们说将循环和 GUI 放在不同的线程上。我想了解这一点以修复我正在处理的另一个更复杂的程序。这就是为什么我做了一个简单的例子来展示我的问题。
http://i.stack.imgur.com/tbNAF.png
这就是程序的显示方式。(jTextField 甚至没有更新。我要问的是是否有人知道解决这个问题的方法,无论是否使用线程。我的这个程序的代码如下。
public class whileLoop extends javax.swing.JFrame {
public whileLoop() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToggleButton1.setText("On");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jToggleButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jToggleButton1))
.addContainerGap(12, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jToggleButton1.isSelected()) {
jToggleButton1.setText("Off");
while (jToggleButton1.isSelected()) {
jTextField1.setText(jTextField1.getText() + "1");
}
} else {
jToggleButton1.setText("On");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new whileLoop().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}
代码的主要区域是:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jToggleButton1.isSelected()) {
jToggleButton1.setText("Off");
while (jToggleButton1.isSelected()) {
jTextField1.setText(jTextField1.getText() + "1");
}
} else {
jToggleButton1.setText("On");
}
}