2012-02-27 115 views
1

我正在研究我的论文并需要帮助。我的论文的一部分是我需要创建一个游戏选择器,我的连接类型是蓝牙,客户端是android手机,服务器是java。 Iam使用JFileChooser来获取数组的值,我需要将该数组传递给另一个类。我试图按照这个帖子http://www.dreaminco...ifferent-class/但在我的节目我似乎无法得到它在这里工作是我的代码:从另一个类获取数组

DemoJFileChooser(班我会得到排列为路径[])

package com.luugiathuy.apps.remotebluetooth; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.File; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class DemoJFileChooser extends JPanel implements ActionListener { 
    JButton go; 

    JFileChooser chooser; 
    String choosertitle; 
    public int x=0; 
    public String path[] = new String[10]; 

    public DemoJFileChooser() { 
    go = new JButton("Do it"); 
    go.addActionListener(this); 
    add(go); 
    } 

    public Dimension getPreferredSize(){ 
    return new Dimension(200, 200); 
    } 

    public static void main(String s[]) { 
     Thread waitThread = new Thread(new WaitThread()); 
     waitThread.start(); 

    JFrame frame = new JFrame(""); 
    DemoJFileChooser panel = new DemoJFileChooser(); 
    frame.addWindowListener(
     new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      System.exit(0); 
      } 
     } 
    ); 
    frame.getContentPane().add(panel,"Center"); 
    frame.setSize(panel.getPreferredSize()); 
    frame.setVisible(true); 
    } 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 
    int result; 

    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File(".")); 
    chooser.setDialogTitle(choosertitle); 
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
     // 
    // disable the "All files" option. 
    // 
    chooser.setAcceptAllFileFilterUsed(false); 
    //  

    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
     System.out.println("getCurrentDirectory(): " 
     + chooser.getCurrentDirectory()); 
     System.out.println("getSelectedFile() : " 
     + chooser.getSelectedFile()); 

      File paths = chooser.getSelectedFile(); 
      path[x] = paths.getAbsoluteFile().toString(); 
      x++; 

    for (int y=0;y<9;y++){ 
      System.out.println(path[y]); 
      setArray(path); 

    } 
    } 
    else { 
     System.out.println("No Selection "); 
     } 

} 
    void setArray(String paths[]) { // saves surname to variable 
      path= paths; 

    } 

    String[] getArray() { //gets the surname when called 
      return path; 
    } 
} 

ProcessConnectionThread(类由我所需要的阵列)

package com.luugiathuy.apps.remotebluetooth; 

import java.awt.MouseInfo; 
import java.awt.Point; 
import java.awt.PointerInfo; 
import java.awt.Robot; 
import java.awt.event.InputEvent; 
import java.awt.event.KeyEvent; 
import java.io.InputStream; 
import java.io.OutputStream; 
import com.luugiathuy.apps.remotebluetooth.DemoJFileChooser; 


import javax.microedition.io.StreamConnection; 

public class ProcessConnectionThread implements Runnable{ 
    //DemoJFileChooser path; 
    //public String[] path = new String[10]; 

    private StreamConnection mConnection; 

    // Constant that indicate command from devices 
    //private static final int EXIT_CMD = -1; 
    private static final int STEER_RIGHT = 1; 
    private static final int STEER_LEFT = 2; 
    private static final int ACCE = 3; 
    private static final int REV = 4; 
    private static final int MOVE = 5; 
    private static final int LEFT_CLICK = 6; 
    private static final int DOUBLE_CLICK = 7; 
    private static final int RIGHT_CLICK = 8; 
    private static final int HOLD = 9; 
    private static final int RELEASE = 10; 
    private static final int MOVEX = 11; 
    private static final int MOVEY = 12; 
    private static final int GAME = 20; 
    private static final int GAME1 = 11; 
    private static final int GAME2 = 12; 
    private static final int GAME3 = 13; 
    private static final int GAME4 = 14; 
    private static final int GAME5 = 15; 
    private DemoJFileChooser patharray[] = new DemoJFileChooser[10]; //create object array from student class 

    public ProcessConnectionThread(StreamConnection connection) 
    { 
     mConnection = connection; 
    } 

    public void FileChooser() { //constructor use keyword public just to make sure it is visible 
     for (int studI=0;studI<10;studI++) { 
      patharray[studI] = new DemoJFileChooser(); 
     } 
    } 
    @Override 
    public void run() { 
     try { 

      // prepare to receive data 
      InputStream inputStream = mConnection.openInputStream(); 
      OutputStream outputStream = mConnection.openOutputStream(); 
      System.out.println("waiting for input"); 

      while (true) { 
       int command = inputStream.read(); 
       System.out.println(command); 

       processCommand(command); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Process the command from client 
    * @param command the command code 
    */ 
    private void processCommand(int command) { 
     try { 


      case GAME1: 
       for (int y=0;y<10;y++){ 
         patharray[y].getArray(); 
         System.out.println(patharray[y]); 
       } 
       Process p = Runtime.getRuntime().exec(patharray[0]); 
       p.waitFor(); 



       break; 


      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 


    } 

我在遇到错误的for循环,patharray []。

任何帮助/建议将不胜感激。谢谢。

+0

错误信息说什么? – Mahesh 2012-02-27 20:59:50

+0

http://i630.photobucket.com/albums/uu26/damnjeka/error.jpg – 2012-02-27 21:13:15

回答

1

创建数组深度复制的最简单方法是使用内置的Arrays.copyOf函数系列。

例如:

String [] srcArray = { "item 1", "item 2", "item 3" }; 
String [] copy = Arrays.copyOf(srcArray, srcArray.length); 

至于你的代码去,有跳出几个问题。在DemoJFileChooser.actionPerformed中,您始终通过System.out.println打印路径数组的所有10个元素,但不能保证所有这些元素中都有对象。你可能会在这里得到一个NullPointerException。您的for-loop应限制为x。此外,在该循环内,您打电话给setArray(path),这意味着您无缘无故地调用了该函数10次。此外,您真的在做path = path,因为您将path传递给setArray,该地址指定给路径。

您还应该检查x尚未超过path的大小,当您将它分配给path[x] = paths.getAbsoluteFile().toString();时。

作为一个单独的建议,不要每次迭代数组时执行for(int i=0; i<10; i++) {...},您可能希望使用for(int i=0; i<theArray.length; i++) {...},这样如果您更改了数组大小,则不需要去更新for循环遍布你的程序。