我正在研究我的论文并需要帮助。我的论文的一部分是我需要创建一个游戏选择器,我的连接类型是蓝牙,客户端是android手机,服务器是java。我使用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 :(得分: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循环应该绑定到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++) {...}
,您可能希望使用{{1}},这样如果您更改数组大小,则不需要去更新程序中的for循环。