0

class1 - 坐标的动态值被保存,并希望将值返回给 class2 的函数。

public class NewJFrame extends javax.swing.JFrame {    

    public NewJFrame() {
        initComponents();
    }    

    ArrayList<Integer> al= new ArrayList<Integer>();
    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
      int x= evt.getX();
      int y= evt.getY();          
      al.add(x);
      al.add(y);
       System.out.println("hi1"+al);
    }
     public ArrayList<Integer> cord()
     {
          System.out.println("hi2"+al);
         return al;
     }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }       

    private javax.swing.JLabel jLabel1;       
}

类2

public class JavaApplication13 {

    public void hello()
    {
        NewJFrame j= new NewJFrame();
        ArrayList<Integer> al=j.cord();
        System.out.println("hi3"+al);
    }

    public static void main(String[] args) {
        JavaApplication13 j= new JavaApplication13();
        j.hello();
    }
}

我想将 x 和 y 坐标的值从 class1 返回到 class2。但它显示为空。帮助。

4

1 回答 1

0

NewJFrame课堂ArrayList<Integer> al上公开静态并JavaApplication13阅读它System.out.println("hi3"+NewJFrame.al.get())。当然,这种方法只是千万种方法中的一种,而且你的代码有一些问题:你不能(我想你会从 ArrayList 中获得一些价值)做"hi3"+al

于 2013-03-23T11:18:57.390 回答