JAVA鼠标键盘监听.docx
- 文档编号:5127261
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:15
- 大小:141.43KB
JAVA鼠标键盘监听.docx
《JAVA鼠标键盘监听.docx》由会员分享,可在线阅读,更多相关《JAVA鼠标键盘监听.docx(15页珍藏版)》请在冰豆网上搜索。
JAVA鼠标键盘监听
《Java编程》实验报告
班级
学号
任课教师
姓名
成绩
课程名称
《Java编程》
实验时间
实验题目
实验11
实验内容
packagelrs;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExperiment11_1extendsJFrameimplementsActionListener{
JPaneljp=null;
JButtonjb1=null;
JButtonjb2=null;
JButtonjb3=null;
JButtonjb4=null;
JButtonjb5=null;
JButtonjb6=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newExperiment11_1();
}
publicExperiment11_1()
{
jp=newJPanel();
jb1=newJButton("Button1");
jb2=newJButton("Button2");
jb3=newJButton("Button3");
jb4=newJButton("Button4");
jb5=newJButton("Button5");
jb6=newJButton("Button6");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb1.setActionCommand("1");
jb2.setActionCommand("2");
jb3.setActionCommand("3");
jb4.setActionCommand("4");
jb5.setActionCommand("5");
jb6.setActionCommand("6");
jp.setLayout(newFlowLayout());
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
this.add(jp);
this.setLayout(newFlowLayout());
this.setTitle("六个按钮");
this.setSize(550,80);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
inti=Integer.parseInt(e.getActionCommand());
System.out.println("Button"+i+"wasclickek");
}
}
packagelrs;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExperiment11_2extendsJFrameimplementsActionListener{
MyPanelmp=null;
JPaneljp=null;
JButtonjb1=null;
JButtonjb2=null;
JButtonjb3=null;
JButtonjb4=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newExperiment11_2();
}
publicExperiment11_2()
{
mp=newMyPanel();
jp=newJPanel();
jb1=newJButton("Left");
jb2=newJButton("Right");
jb3=newJButton("Up");
jb4=newJButton("Down");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb1.setActionCommand("1");
jb2.setActionCommand("2");
jb3.setActionCommand("3");
jb4.setActionCommand("4");
jp.setLayout(newFlowLayout());
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
this.setTitle("移动圆圈");
this.setVisible(true);
this.setSize(320,320);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(jp,BorderLayout.SOUTH);
this.add(mp,BorderLayout.CENTER);
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
switch(Integer.parseInt(e.getActionCommand()))
{
case1:
mp.runLeft();break;
case2:
mp.runRight();break;
case3:
mp.runUp();break;
case4:
mp.runDown();break;
}
}
}
classMyPanelextendsJPanel
{
intx=100;
inty=100;
publicvoidrunUp()
{
y=y-4;
this.repaint();
}
publicvoidrunDown()
{
y=y+4;
this.repaint();
}
publicvoidrunLeft()
{
x=x-4;
this.repaint();
}
publicvoidrunRight()
{
x=x+4;
this.repaint();
}
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
g.drawOval(x,y,50,50);
}
}
packagelrs;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExperiment11_3extendsJFrameimplementsMouseListener{
JPaneljp=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newExperiment11_3();
}
publicExperiment11_3()
{
jp=newJPanel();
this.add(jp);
this.setSize(200,200);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
}
@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidmousePressed(MouseEvente){
//TODOAuto-generatedmethodstub
jp.setBackground(Color.black);
}
@Override
publicvoidmouseReleased(MouseEvente){
//TODOAuto-generatedmethodstub
jp.setBackground(Color.red);
}
@Override
publicvoidmouseEntered(MouseEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidmouseExited(MouseEvente){
//TODOAuto-generatedmethodstub
}
}
packagelrs;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassExperiment11_4extendsJFrameimplementsMouseListener{
MyPanel4mp=null;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newExperiment11_4();
}
publicExperiment11_4()
{
mp=newMyPanel4();
this.add(mp);
this.setSize(300,300);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addMouseListener(this);
}
@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidmousePressed(MouseEvente){
//TODOAuto-generatedmethodstub
mp.set(e.getX(),e.getY());
}
@Override
publicvoidmouseReleased(MouseEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidmouseEntered(MouseEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidmouseExited(MouseEvente){
//TODOAuto-generatedmethodstub
}
}
classMyPanel4extendsJPanel
{
intx=0,y=0;
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
g.drawString("("+x+","+y+")",x,y);
}
publicvoidset(intx,inty)
{
this.x=x;
this.y=y;
this.repaint();
}
}
packagelrs;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassExperiment11_5extendsJFrame{
//构造函数初始化组件
publicExperiment11_5()
{
DrowLine1drowline=newDrowLine1();
this.add(drowline,"Center");
this.addKeyListener(drowline);
this.setVisible(true);
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newExperiment11_5();
}
}
//画笔
classDrowLine1extendsJPanelimplementsKeyListener
{
inti=0;
intx=50;
inty=50;
intx2=50;
inty2=50;
@Override
publicvoidkeyPressed(KeyEvente){
//TODOAuto-generatedmethodstub
switch(e.getKeyCode())
{
caseKeyEvent.VK_DOWN:
y2=y+10;break;
caseKeyEvent.VK_UP:
y2=y-10;break;
caseKeyEvent.VK_LEFT:
x2=x-10;break;
caseKeyEvent.VK_RIGHT:
x2=x+10;break;
}
this.repaint();
}
@Override
publicvoidkeyReleased(KeyEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidkeyTyped(KeyEvente){
//TODOAuto-generatedmethodstub
}
protectedvoidpaintComponent(Graphicsg){
System.out.println("x="+x+"y="+y+"x2="+x2+"y2="+y2);
g.drawLine(x,y,x2,y2);
x=x2;
y=y2;
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 鼠标 键盘 监听