?? sample27_3.java
字號(hào):
package wyf.jc;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//定義該類繼承自JFrame
public class Sample27_3 extends JFrame implements ActionListener
{
//創(chuàng)建按鈕數(shù)組
JButton[] jb={new JButton("按扭1"),new JButton("按扭2"),new JButton("按扭3")};
//創(chuàng)建文本框
JTextField jtf=new JTextField();
JTextField jtfMsg=new JTextField();
//創(chuàng)建文本區(qū)
JTextArea jta=new JTextArea();
//創(chuàng)建放置文本區(qū)的滾動(dòng)窗口
JScrollPane jsp=new JScrollPane(jta);
//定義Robot引用r
Robot r;
public Sample27_3()
{
//設(shè)置窗體布局管理器
this.setLayout(null);
//設(shè)置jtfMsg文本框的大小位置并將其添加進(jìn)窗體中
jtfMsg.setBounds(10,10,270,26);
this.add(jtfMsg);
//設(shè)置jtfMsg文本框?yàn)椴豢删庉嫚顟B(tài)
jtfMsg.setEditable(false);
//對(duì)按鈕數(shù)組循環(huán)處理
for(int i=0;i<jb.length;i++)
{
//設(shè)置按鈕的大小位置并將其添加進(jìn)窗體中
jb[i].setBounds(10+i*90,50,80,26);
this.add(jb[i]);
//為按鈕注冊(cè)動(dòng)作事件監(jiān)聽器
jb[i].addActionListener(this);
}
//設(shè)置文本框的大小位置并將其添加進(jìn)窗體中
jtf.setBounds(10,90,270,26);
this.add(jtf);
//為文本框注冊(cè)動(dòng)作事件監(jiān)聽器
jtf.addActionListener(this);
//設(shè)置文本區(qū)的初始內(nèi)容
for(int i=0;i<200;i++)
{
jta.append("這里是帶滾動(dòng)條的文本區(qū)。");
}
//設(shè)置文本區(qū)為自動(dòng)換行
jta.setLineWrap(true);
//設(shè)置包含文本區(qū)滾動(dòng)窗口的大小位置并將其添加進(jìn)窗體中
jsp.setBounds(10,130,270,80);
this.add(jsp);
//設(shè)置窗口的標(biāo)題、大小位置以及可見性
this.setTitle("文本區(qū)與滾動(dòng)條");
this.setResizable(false);
this.setBounds(100,100,300,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//調(diào)用creatRobot方法創(chuàng)建Robot對(duì)象并初始化
this.creatRobot();
}
//創(chuàng)建Robot對(duì)象并初始化的方法
public void creatRobot()
{
try
{ //創(chuàng)建Robot對(duì)象
r=new Robot();
//延遲1秒
r.delay(1000);
for(int i=0;i<jb.length;i++)
{//調(diào)用鼠標(biāo)點(diǎn)擊方法分別點(diǎn)擊三個(gè)按鈕一次
this.clickMouse(jb[i]);
//延遲1秒
r.delay(1000);
}
//調(diào)用鼠標(biāo)移動(dòng)方法,將鼠標(biāo)移動(dòng)到文本框中
this.clickMouse(jtf);
//循環(huán)模擬輸入文本
for(int i=0;i<26;i++)
{
r.keyPress(KeyEvent.VK_A+i);
r.keyRelease(KeyEvent.VK_A+i);
//延遲200毫秒
r.delay(200);
}
//模擬按下回車鍵
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//延遲1秒
r.delay(1000);
//調(diào)用鼠標(biāo)點(diǎn)擊方法,模擬點(diǎn)擊文本區(qū)
this.clickMouse(jsp);
//延遲1秒
r.delay(1000);
//模擬滑動(dòng)滾動(dòng)條
for(int i=0;i<15;i++)
{
r.mouseWheel(2);
//延遲100毫秒
r.delay(100);
}
//延遲1秒
r.delay(1000);
//將鼠標(biāo)移動(dòng)到窗體關(guān)閉按扭處
r.mouseMove(this.getX()+290,this.getY()+8);
//延遲5秒
r.delay(5000);
//模擬點(diǎn)擊鼠標(biāo)左鍵按下關(guān)閉窗體的按鈕
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//模擬鼠標(biāo)點(diǎn)擊指定控件的方法
public void clickMouse(JComponent c)
{
//確定點(diǎn)擊控件的位置
int x=this.getX()+c.getX()+c.getWidth()/2;
int y=this.getY()+c.getY()+c.getHeight()/2+20;
//將鼠標(biāo)移動(dòng)到指定位置
r.mouseMove(x,y);
//模擬點(diǎn)擊鼠標(biāo)左鍵
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
//實(shí)現(xiàn)ActionListener中的方法
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<jb.length;i++)
{//顯示按下按鈕的提示信息
if(e.getSource()==jb[i])
{
jtfMsg.setText("點(diǎn)擊了按扭"+(i+1));
}
}
if(e.getSource()==jtf)
{//顯示文本框輸入后回車的提示信息
jtfMsg.setText("在文本框中輸入了"+jtf.getText());
}
}
public static void main(String[] args)
{
//創(chuàng)建Sample27_3窗體對(duì)象
new Sample27_3();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -