Results 1 to 2 of 2
  1. #1
    Xamresor is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Java mouse events in gui.

    I fiew days in java and i can't undetstand what here is wrong? why it write me x:0 y:0?
    I use netBeans.

    Java Code:
    private void But1MousePressed(java.awt.event.MouseEvent evt) {
        int aMx = getX();
        int aMy = getY();
        But1.setText("x:" + aMx + "y:" + aMy);
        
    }
    Last edited by pbrockway2; 12-19-2011 at 11:45 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Java mouse events in gui.

    You are calling getX() on whatever thing this code is part of. It could very well be a Component at the top left corner of the screen where getX() returns zero. Better to call getX() on the MouseEvent instance if you want to know the position of the mouse.

    -----

    If you have only been using Java for a few days I would strongly recommend you work through basic Java syntax either from a textbook or something online like Oracle's Tutorial. Graphical user interfaces are complex and coding them in Java is not something that can be explained piecemeal in a forum.

    In Java variables and methods start with a lowercase letter. So, but1MousePressed() and but1.

Similar Threads

  1. Help with drawing a string using mouse events
    By ptuckley in forum AWT / Swing
    Replies: 2
    Last Post: 12-14-2010, 12:09 PM
  2. Help with some basics please (mouse events)
    By tigersarehot in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2010, 03:22 AM
  3. Mouse events, are they best or only way to go?
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-10-2009, 05:34 PM
  4. Need help with looping mouse Events.
    By busdude in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 09:25 PM
  5. Demonstration of mouse events
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •