本帖最后由 曲佳奇 于 2014-4-10 19:40 编辑
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class MouseClickEvent extends JFrame {
- public static void main(String[] args) {
- JFrame jf = new JFrame("点击面板获取坐标");
- final JLabel jl = new JLabel("点击");
- jf.add(jl);//添加JLabel
-
-
- jf.addMouseListener(new MouseListener() {//为面板添加监听
-
- public void mouseClicked(MouseEvent e) {
- Date now = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");//设置时间格式
- if (e.getButton() == MouseEvent.BUTTON1) {// 判断如果是鼠标左键
- jl.setText("坐标:"+e.getX() + "," + e.getY()+"时间:"+dateFormat.format(now));//修改Label的值
- }
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- });
- jf.setVisible(true);//设置可见
- jf.setBounds(0, 0, 500, 400);//设置大小
- }
- }
复制代码 |