gpt4 book ai didi

java - 制作第二个线程时遇到问题

转载 作者:行者123 更新时间:2023-11-30 11:04:53 25 4
gpt4 key购买 nike

我正在尝试制作一个小程序,它在一定的时间间隔内单击鼠标左键一段时间。我无法解决的唯一问题是能够随时停止循环,甚至在计时器用完之前。我发现应该在工作线程中运行循环,并且我的停止按钮应该以某种方式中断该线程,但我无法管理它。我希望你能帮我写一些代码。

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class KeyRepeater_v2 extends javax.swing.JFrame {

Robot robot;

boolean stop;
double time;
double time_milli;


public KeyRepeater_v2() throws AWTException {
this.robot = new Robot();
initComponents();
}

/**
* 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();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();

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);
}
});

jButton2.setText("Stop");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jLabel1.setText("Timer");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

pack();
}// </editor-fold>


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
stop = true;
//setFocusable (true);
//JOptionPane.showMessageDialog(null, stop, "Test Titel", JOptionPane.OK_CANCEL_OPTION);
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
stop = false;
robot.delay(5000); //in milliseconds

do {
leftClick();
robot.delay(1500);
time_milli = time_milli - 1700;
} while (time_milli > 0);

}



private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
time = Double.parseDouble(jTextField1.getText());
//System.out.print(time);
time_milli = time * 1000;
}




/**
* @param args the command line arguments
*/
public static void main(String args[]) throws AWTException {
/* 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(KeyRepeater_v2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(KeyRepeater_v2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(KeyRepeater_v2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(KeyRepeater_v2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
try {
new KeyRepeater_v2().setVisible(true);
} catch (AWTException ex) {
Logger.getLogger(KeyRepeater_v2.class.getName()).log(Level.SEVERE, null, ex);
}
});

}


private void leftClick() {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

netbeans 生成了相当多的代码...我真的不知道如何创建一个新线程,我也不知道如何让我的文本字段(用于计时器)和我的按钮与线程通信......真的希望你能帮助我:D

最佳答案

您肯定想看看计时器。基本上,它是为了方便您使用线程。我不想修改太多你的代码,所以我为你提供了一个示例来替换你正在使用的 do while 循环(它来自官方 Timer's javadoc )。

ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
do {
System.out.println("left click");
leftClick();
robot.delay(1500);
time_milli = time_milli - 1700;
} while (time_milli > 0);
}
};
new Timer(5000, taskPerformer).start();

请注意,我明确地为您留下了控制台打印,以检查您是否被要求执行每 5 秒的操作;当您单击更加用户友好的开始按钮时,定时器还可以防止您的 GUI 被卡住。

关于java - 制作第二个线程时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com