[英]returning value from one class to another
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。 但顯示為空。 救命。
在NewJFrame
類中,將ArrayList<Integer> al
公共靜態System.out.println("hi3"+NewJFrame.al.get())
,在JavaApplication13
將其讀取為System.out.println("hi3"+NewJFrame.al.get())
。 當然,這種方法只是四分之一方法的實現方法,而且您的代碼有一些問題:您不能(我想您很想從ArrayList中獲得一些價值)執行"hi3"+al
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.