java代码帮忙注释下!O(∩_∩)O谢谢要考试了,不懂啊!可以的话每段后面注释下!
/*1.设计一个跟踪鼠标移动的窗口。窗口中间添加一个蓝色背景*白色文字的标签,在标签上显示鼠标指针当前坐标值。在窗口下方*添加一个按钮,点击按钮可以在停止跟踪和继续跟踪之...
/*1. 设计一个跟踪鼠标移动的窗口。窗口中间添加一个蓝色背景
*白色文字的标签,在标签上显示鼠标指针当前坐标值。在窗口下方
*添加一个按钮,点击按钮可以在停止跟踪和继续跟踪之间进行切换。
*程序文件命名为Exe10_1.java。
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Exe10_1 extends JFrame
implements MouseMotionListener,ActionListener{
JLabel tracer;
JButton start;
boolean tracing = true;
public Exe10_1(){
super("鼠标跟踪");
setBounds(300,300,300,300);
setLayout(new FlowLayout());
tracer = new JLabel();
tracer.setPreferredSize(new Dimension(100,30));
tracer.setBackground(Color.blue);
tracer.setForeground(Color.white);
tracer.setOpaque(true);
addMouseMotionListener(this);
add(tracer);
start = new JButton("停止跟踪");
start.addActionListener(this);
add(start);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void mouseDragged(MouseEvent e){
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
public void mouseMoved(MouseEvent e){
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
public void actionPerformed(ActionEvent e){
if(tracing == true){
removeMouseMotionListener(this);
start.setText("继续跟踪");
tracing = false;
}else{
addMouseMotionListener(this);
start.setText("停止跟踪");
tracing = true;
}
}
public static void main(String[] args){
Exe10_1 frame = new Exe10_1();
}
} 展开
*白色文字的标签,在标签上显示鼠标指针当前坐标值。在窗口下方
*添加一个按钮,点击按钮可以在停止跟踪和继续跟踪之间进行切换。
*程序文件命名为Exe10_1.java。
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Exe10_1 extends JFrame
implements MouseMotionListener,ActionListener{
JLabel tracer;
JButton start;
boolean tracing = true;
public Exe10_1(){
super("鼠标跟踪");
setBounds(300,300,300,300);
setLayout(new FlowLayout());
tracer = new JLabel();
tracer.setPreferredSize(new Dimension(100,30));
tracer.setBackground(Color.blue);
tracer.setForeground(Color.white);
tracer.setOpaque(true);
addMouseMotionListener(this);
add(tracer);
start = new JButton("停止跟踪");
start.addActionListener(this);
add(start);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void mouseDragged(MouseEvent e){
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
public void mouseMoved(MouseEvent e){
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
public void actionPerformed(ActionEvent e){
if(tracing == true){
removeMouseMotionListener(this);
start.setText("继续跟踪");
tracing = false;
}else{
addMouseMotionListener(this);
start.setText("停止跟踪");
tracing = true;
}
}
public static void main(String[] args){
Exe10_1 frame = new Exe10_1();
}
} 展开
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
/**
* 继承JFrame 实现 MouseMotionListener,ActionListener
*
*/
public class Exe10_1 extends JFrame implements MouseMotionListener,
ActionListener {
JLabel tracer;//声明一个JLabel
JButton start;//声明一个JButton
boolean tracing = true;// 定义一个Boolean变量
/**
* 构造函数
*/
public Exe10_1() {
super("鼠标跟踪");//设置JFrame的title
setBounds(300, 300, 300, 300); // JFrame大小
setLayout(new FlowLayout()); //JFrame的布局为FlowLayout
tracer = new JLabel();//给刚才声明的JLabel赋值
tracer.setPreferredSize(new Dimension(100, 30));//
tracer.setBackground(Color.blue);//设置背景色
tracer.setForeground(Color.white);//设置前景色
tracer.setOpaque(true);
addMouseMotionListener(this);//整个JFrame监听鼠标事件
add(tracer); //JFrame添加JLabel
start = new JButton("停止跟踪");//Jbutton初始化的名字为“停止跟随”
start.addActionListener(this);//JButton添加监听事件
add(start);//JFrame添加JLabel
setVisible(true);//JFrame的可见性
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//右上角的【关闭】按钮
}
/* (当按下鼠标 不放开时 滑动鼠标 JLabel上打印内容)
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
/* (鼠标在JFrame上滑动时 JLabel上打印内容)
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e) {
tracer.setText("(x,y) = (" + e.getX() + "," + e.getY() + ")");
}
/* (鼠标监听事件处理)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (tracing == true) {
removeMouseMotionListener(this);//JFrame移除监听鼠标事件
start.setText("继续跟踪");//JLabel重新设置Text
tracing = false;
} else {
addMouseMotionListener(this);//JFrame添加鼠标事件监听
start.setText("停止跟踪");//JLabel重新设置Text
tracing = true;
}
}
/** 程序入口
* @param args
*/
public static void main(String[] args) {
Exe10_1 frame = new Exe10_1();//生成一个Exe10_1的实例 实例名为:frame
}
}
希望对你有帮助
追问
你好强啊!有问题再问你!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询