简体   繁体   中英

Issue with getting Variables values in java. Scope of Variable

Here is my Code which output is as shown in Picture below. I need to get values of x_coor and y_coor outside the mousePressed() method. but i am not able to do it. i have tried so far

  1. Declaring variable in Constructor .

  2. Declaring variable as global.

  3. Declaring variable as static.

  4. Declaring variable in main() .

but all not got so far i want.

Note: Do not Mention the problem I already know it. I need solution

 public class Tri_Angle extends MouseAdapter {

    Tri_Angle(){
      //            int  x_coor=0;
      //          int y_coor=0;
    }

public static void main(String[] args) {

    JFrame frame = new JFrame ();  

    final int FRAME_WIDTH = 500;  
    final int FRAME_HEIGHT = 500;  

    frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);         
      frame.addMouseListener(new MouseAdapter() { 
      @Override
      public void mousePressed(MouseEvent me) { 
      int    x_coor= me.getX();
      int   y_coor= me.getY();
      System.out.println("clicked at (" + x_coor + ", " + y_coor + ")");        
      } 

    });

    frame.setTitle("A Test Frame");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    frame.setVisible(true);  

//This is what i want to do, but it does not know x_coor variable here. 

           if(x_coor>=0)
           {
                     System.out.println("clicked at (" + x_coor + ", " + y_coor + ")");
           }       
    }
 }

在此处输入图片说明

x_coor and y_coor are local variables defined in the mousePressed function you defined. Since they are local to that function, you cannot access them outside of that function as you are trying to do.

You can instead declare them as member variables and write the MouseAdapter mousePressed override routine to update them. Then you want to add a Tri_Angle object to the frame as a mouseListener, not just a mouseListener object. Example:

public class Tri_Angle extends MouseAdapter {
  int x_coor, y_coor;

  Tri_Angle()
  {
    x_coor = 0;
    y_coor = 0;
  }

  @Override
  public void mousePressed(MouseEvent me)
  {
    x_coor = me.getX();
    y_coor = me.getY();
  }

public static void main(String[] args)
{

  // code...
  frame.addMouseListener(new Tri_Angle());

  // Access x_coor and y_coor as needed

}

Also keep in mind that your if(x_coor >= 0) statement in your main routine is only going to run 1 time (towards the beginning of the program). It does not run every time a mouse is pressed. If you want something to run every time a mouse is pressed, that needs to be in your mousePressed routine.

Declare the variable inside the main method and initialize

public class Tri_Angle extends MouseAdapter {
.....
public static void main(String[] args) {
 int x_coor =0 , y_coor=0;
 ......
}
.....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM