我的计划的目的是帮助我玩Conquer Online,它将自动从F1到F10。 问题是我在Robot类上苦苦挣扎,当我按下停止按钮时,它不会停止程序,我甚至无法点击该按钮从GUI的“X”按钮,所以我必须做的是完全关闭我的IDE(BlueJ)以退出程序。 顺便说一句,所提供的代码还没有最终我只是在努力解决它的反应迟钝以及keyListeners。对不起格式。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Autopot extends JFrame
{
boolean FunctionRun;
JButton Start= new JButton("Start");
JButton Stop= new JButton("Stop");
public Autopot()
{
setTitle("Auto Pot");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400,230,420,300);
getContentPane().setBackground(Color.BLACK);
JLabel Functions=new JLabel("Keys to repeat");
Functions.setBounds(35,1,350,100);
Functions.setFont(new Font("Comic Sans MS",Font.BOLD,28));
Functions.setForeground(Color.GREEN);
getContentPane().add(Functions);
final ArrayList<String> FKeys=new ArrayList<String>();
FKeys.add("A");
FKeys.add("F2");
FKeys.add("F3");
FKeys.add("F4");
FKeys.add("F5");
FKeys.add("F6");
FKeys.add("F7");
FKeys.add("F8");
FKeys.add("F9");
FKeys.add("F10");
final String A="A";
final JComboBox Keys= new JComboBox(FKeys.toArray());
Keys.setBounds(55,75,150,20);
getContentPane().add(Keys,A);
JLabel Intervals=new JLabel("Intervals");
Intervals.setBounds(35,75,350,100);
Intervals.setFont(new Font("Comic Sans MS",Font.BOLD,28));
Intervals.setForeground(Color.GREEN);
getContentPane().add(Intervals);
ArrayList<String> Interval=new ArrayList<String>();
Interval.add("500");
Interval.add("1000");
Interval.add("1500");
JComboBox KeyIntervals= new JComboBox(Interval.toArray());
KeyIntervals.setBounds(55,150,150,20);
getContentPane().add(KeyIntervals);
Stop.setMnemonic(KeyEvent.VK_ENTER);
Start.setBounds(75,200,120,40);
getContentPane().add(Start);
Start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
Start.setEnabled(false);
Stop.setEnabled(true);
Robot bot=new Robot();
FunctionRun=true;
while(FunctionRun)
{
if(Keys.getSelectedItem()==A)
{
bot.delay(500);
bot.keyPress(KeyEvent.VK_A);
bot.keyRelease(KeyEvent.VK_A);
}
else
{JOptionPane.showMessageDialog(null,Keys);
break;
}
}
}
catch(Exception Error)
{
JOptionPane.showMessageDialog(null,Error);
}
}
});
Stop.setBounds(230,200,120,40);
getContentPane().add(Stop);
Stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent click)
{
JOptionPane.showMessageDialog(null,"You clicked: "+click.getSource());
Start.setEnabled(true);
}
});
Stop.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent stop)
{
JOptionPane.showMessageDialog(null,"Key Typed: " +stop.getKeyCode());
}
public void keyReleased(KeyEvent stop)
{
JOptionPane.showMessageDialog(null,"Key Released: " +stop.getKeyCode());
}
public void keyPressed(KeyEvent stop)
{
JOptionPane.showMessageDialog(null,"Key Pressed: " +stop.getKeyCode());
}
});
}
public static void main(String[]args)
{
Autopot pot= new Autopot();
pot.setVisible(true);
pot.setResizable(false);
}
}