鼠标适配器程序不起作用

时间:2016-09-25 01:46:17

标签: java swing adapter

public class MouseDetailsFrame extends JFrame {

private String details;
    private JLabel statusbar;

    public MouseDetailsFrame(){

        super("MouseFrame");

        statusbar= new JLabel("Click The Mouse");
        add(statusbar, BorderLayout.SOUTH);
    addMouseListener(new MouseClickHandler());


    }
    private class MouseClickHandler extends MouseAdapter{


        public void MouseClicked(MouseEvent event){
            int xPos= event.getX();// has no use anywhere, i wonder why that is
            int yPos= event.getY();// same

            details= String.format(" clicked %d times:", event.getClickCount());


                    if (event.isMetaDown())
                        details = String.format(" clicked %d times:"+"with right mouse", event.getClickCount());
                    else if(event.isAltDown())
                        details=String.format(" clicked %d times:"+"with middle mouse", event.getClickCount());
                    else 
                        details =String.format(" clicked %d times:"+ "with left mouse", event.getClickCount());

                    statusbar.setText(details);
        }

    }
}

//当我通过主字符串运行时没有任何反应。一个窗口只显示状态栏没有显示我点击了多少时间,或者我用鼠标左键或右键单击了。请给我一个提示,我想自己做。

1 个答案:

答案 0 :(得分:2)

  1. MouseClicked方法中添加@Override注释 - 您注意到了什么?
    @Override备件中的一种情况你是悲伤的。

  2. 去检查mouseClicked方法的javadoc:你注意到拼写的区别吗?