Start the Notepad program in Java and enter the contents

Source: Internet
Author: User
Tags array
The code for this article comes from the following connections.


Http://www.developer.com/java/other/article.php/10936_2212401_3 (Introduction to the Java Robot Class in Java)


code Simple Description: You can start Windows Notepad program, and enter Hello World.





import javax.swing.*;


import javax.swing.event.*;


import java.awt.*;


import java.awt.event.*;


import java.io.*;





public class robot05{


//create An array of keycode data


static int keyinput[] = {


Keyevent.vk_h,


Keyevent.vk_e,


keyevent.vk_l,


keyevent.vk_l,


Keyevent.vk_o


};//end keyinput Array





public static void Main (string[] args)


throws awtexception,ioexception{





runtime.getruntime (). EXEC ("notepad");


Robot Robot = new Robot ();


Robot.keypress (Keyevent.vk_shift);





for (int cnt2 = 0;cnt2 < Keyinput.length; cnt2++) {





if (Cnt2 > 0) {


robot.keyrelease (Keyevent.vk_shift);


}//end If





robot.keypress (Keyinput[cnt2]);


//insert A one-half second delay between


//characters.





Robot.delay (500);


}//end for Loop


}//main





}//end class Robot05







GUI version of the
Robot05.


Robot05gui.java





/**


* Robot05gui.java


* Create by Kin. 2004/11/07.


* Please enjoy this.


*/


import javax.swing.*;


import javax.swing.event.*;





import java.awt.event.*;


import java.awt.*;





/**robot05 ' s GUI version.*/


public class Robot05gui extends JFrame {





private JButton B = new JButton ("Start Notepad and input \ Hello world\");





public Robot05gui () {


super ("Start Notepad and Input \" Hello world\ ");


Getcontentpane (). Add (B,borderlayout.center);


B.addactionlistener (New ActionListener () {


public void actionperformed (ActionEvent e) {


try {


new Robot05 (). Main (New string[]{});


} catch (Exception ex) {


Ex.printstacktrace ();


}


}


});


}





public static void Main (string[] args) {


Robot05gui r = new Robot05gui ();


r.setsize (200,200);


r.setvisible (TRUE);


}


}

















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.