【发布时间】:2016-04-02 17:01:24
【问题描述】:
我想制作它以便它可以制作按钮 例如,如果 txt 文件是: * * * * * * 它将删除空格到 ***** 然后将每个 * 放入自己的按钮中
//loads file and removes the spaces and stores it in an array
public static void loadFile(JButton[][] board, String fileName) throws IOException {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(fileName));
String lineRead = inputStream.readLine();
while (lineRead != null) {
String[] splited = lineRead.split(" ");
for(int i = 0; i < board.length; i++){
board[i] = lineRead.split(" ");
}
System.out.print(lineRead);
}
lineRead = inputStream.readLine();
}
catch (FileNotFoundException exception) {
System.out.println("Error opening file");
}
finally {
if (inputStream != null)
inputStream.close();
}
}
//a button that opens the fileselector and then calls the loadfile method
JButton file1 = new JButton("Player File");
file1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser open = new JFileChooser();
open.showOpenDialog(null);
loadFile(buttonPlayer, "CPU.txt");
}
});
【问题讨论】:
-
您提供的 sn-p 究竟有什么问题?它是否因异常而失败?产生错误的结果?
-
它只是说 jbutton[] 不能转换为 string[]
-
你应该把
board[i] = lineRead.split(" ");换成board[i] = new JButton(lineRead[i]);这一行