主頁面類(包裝成了一個exe程序)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 腳本的主頁面
* @author Serendipity
*/
public class Client extends JFrame implements ActionListener {
private JButton JStart; //定義程序開始按鈕
private JButton JOver; //定義關閉客戶端按鈕
private JLabel Title; //定義客戶端標題
private JTextField Note; //寫一些廢話
public Client(){
super("掛機專用"); //定義軟件標題:掛機
this.setSize(600,500); // 定義軟件邊框大小
this.setLocation(600,300); //定義軟件運行時初始位置
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //當關閉這個窗口時,軟件停止運行
Txt();
Button();
}
//寫一些文字
public void Txt(){
Title = new JLabel("蒸汽機給爺爬");
this.add(Title);
Title.setBounds(200,50,200,100);
Title.setFont(new Font("宋體",Font.BOLD,25));
Note = new JTextField("因爲按鍵精靈有廣告,所以就自己做了一個 by:寧寧寧醬");
this.add(Note);
Note.setBounds(100,200,500,30);
Note.setFont(new Font("宋體",Font.BOLD,15));
Note.setEditable(false);
Note.setBorder(null);
}
//定義按鈕
public void Button(){
setLayout(null);
JStart = new JButton("開始");
this.add(JStart);
JStart.setBounds(40,250,160,50);
JStart.setFont(new Font("宋體", Font.BOLD,20));
JStart.addActionListener(this); //鍵盤監聽
JOver = new JButton("關閉");
this.add(JOver);
JOver.setBounds(380,250,160,50); //定義按鈕位置,外匯經紀商買賣價http://www.fx61.com/quotesbuy.html
JOver.setFont(new Font("宋體", Font.BOLD,20));
JOver.addActionListener(this); //鍵盤監聽
this.setVisible(true);
}
/**
* Invoked when an action occurs.
*
* @param e
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == JStart) {
this.dispose();
try {
Circulation circulation = new Circulation();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
if(e.getSource() == JOver){
System.exit(0);
}
}
}
下面是核心,Robot類,讓電腦按空格,中間設置了延遲
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* 定義讓電腦自動按下空格
* @author Serendipity
*/
public class Script {
Robot robot = new Robot();
public Script() throws AWTException {
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.delay(100);
}
}
循環
import java.awt.*;
/**
* 定義循環結構
* @author Serendipity
*/
public class Circulation {
private boolean flag = true;
public Circulation() throws AWTException {
while(flag){
Script script = new Script();
}
}
}
啓動
/**
* 運行啓動
* @author Serendipity
*/
public class Menu {
public static void main(String[] args){
Client client = new Client();
}
}
Java做一個讓電腦循環按空格的小程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.