这是我的代码,其输出如下图所示。 我需要在mousePressed()
方法之外获得x_coor和y_coor的值。但我无法做到。我已尝试到目前为止在java中获取变量值的问题。变量的范围
声明变量
Constructor
。将变量声明为全局变量。
声明变量为静态。
声明变量
main()
。
但所有没有得到我想要的。
注意:不要提及我已经知道的问题。我需要的解决方案
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 + ")");
}
}
}
我知道。但那就是我想要做的。那么最新的解决方案 – 2014-12-27 19:12:59
我已经试过了。仍然存在问题。 – 2014-12-27 19:22:28
你可以发布代码和你得到的问题吗? – Dtor 2014-12-27 19:23:05