import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Mouse extends JFrame
implements ActionListener, MouseListener, MouseMotionListener
{
private JCheckBox checkBox;
private JPanel panel;
private JPanel j1;
private JPanel j2;
private JLabel j3;
private boolean b;
public Mouse()
{
setBounds(200, 200, 300, 400);
setDefaultCloseOperation(3);
setTitle("鼠标坐标抓取");
Container c = getContentPane();
this.panel = new JPanel();
this.panel.setLayout(new BorderLayout());
c.add(this.panel);
this.j1 = new JPanel();
this.j1.setBorder(BorderFactory.createTitledBorder("控制面板"));
this.panel.add(this.j1, "North");
addCheckBox();
this.j2 = new JPanel();
this.j2.setBorder(BorderFactory.createTitledBorder("取点区"));
this.panel.add(this.j2);
this.j3 = new JLabel("尚未开始");
this.j3.setBorder(BorderFactory.createTitledBorder("结果显示"));
this.panel.add(this.j3, "South");
}
public void addCheckBox()
{
this.checkBox = new JCheckBox("开始");
this.checkBox.setSelected(false);
this.j1.add(this.checkBox);
this.checkBox.addActionListener(this);
}
public static void main(String[] args)
{
new Mouse().setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
this.b = this.checkBox.isSelected();
if (this.b)
{
this.j3.setText("开始抓点");
this.j2.addMouseMotionListener(this);
this.j2.addMouseListener(this);
}
else
{
this.j3.setText("抓点结束");
this.j2.removeMouseListener(this);
this.j2.removeMouseMotionListener(this);
}
}
public void mouseClicked(MouseEvent e)
{
this.j3.setText("点击获得坐标:" + e.getX() + " " + e.getY());
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
this.j3.setText("鼠标已离开区域");
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
this.j3.setText("当前坐标 X:" + e.getX() + " Y:" + e.getY());
}
}