我想知道我是如何从 GUI <停止 线程的。我想如果线程是在按钮制作之前制作的,我会没事的,但我也试图放入JProgressBar以便我可以更新它。所以现在我被困在如何做到这一点。如果你能提供帮助那就太好了。
提前致谢!
代码:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import javax.swing.JOptionPane;
public class mainJFrame extends javax.swing.JFrame {
//set Global variables
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
final int screenH = dim.height, screenW = dim.width;
private volatile boolean isStop = true;
public mainJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jProgressBar1 = new javax.swing.JProgressBar();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Zeveso's MD5 Cracker");
setBounds(new java.awt.Rectangle(200, 200, 0, 0));
setResizable(false);
jLabel1.setText("MD5:");
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jMenu1.setText("File");
jMenuItem1.setText("Create MD5");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(isStop == true){
jProgressBar1.setIndeterminate(true);
myMDThread.start();
isStop = false;
}
else{
System.out.println("Already running!");
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
createMD5 myMD = new createMD5();
String myPass = JOptionPane.showInputDialog(null, "Please put your password!");
myPass = myMD.getMD5(myPass);
JOptionPane.showMessageDialog(null, "MD5: " + myPass);
System.out.println(myPass);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(myPass);
clipboard.setContents(stringSelection, null);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jProgressBar1.setIndeterminate(false);
isStop = true;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mainJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JTextField jTextField1;
}
答案 0 :(得分:3)
没有安全方法强制线程停止(请阅读Thread.stop()
方法上的JavaDoc以获取更多解释。)
最好的方法是调用interrupt
方法,这是一个“合作”停止。如果线程处于可中断调用的中间,那么它将抛出InterruptedException
。如果线程是计算密集型的,那么它应该定期用Thread.interrupted()
检查自己的中断状态,并在请求时干净地退出。
答案 1 :(得分:1)
你有线程的来源吗?
通常,除非您具有这样做的能力,否则无法停止线程。例如,您的线程可能会公开stopProcessing
之类的方法,在调用时会设置一个标志。你的线程的run
方法会定期检查这个标志,看它是否应该停止。
如果你有一些工作没有很好地映射到这个,你唯一真正的选择就是忽略线程,隐藏进度条,并给出线程停止的错觉。