我正在尝试创建JButtons并将它们添加到JScrollPane以及JButtons的向量中。当我尝试将它们添加到向量时,程序会抛出异常。
以下是代码:
public class DirectorySelectionWindow extends JFrame {
Vector<JButton> directoryButtons;
String selectedDir;
DirectorySelectionWindow(){
super("Select Directory");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String cwd = System.getProperty("user.dir");
System.out.println(cwd);
File currentDirectory = new File(cwd);
String filesInDir[] = currentDirectory.list();
JScrollPane pane = new JScrollPane();
for (int i = 0; i < filesInDir.length; i++) {
if (filesInDir[i] != null) {
if (!filesInDir[i].toString().startsWith(".")) {
System.out.println(filesInDir[i]);
JButton button = new JButton(filesInDir[i]);
directoryButtons.addElement(button);
pane.add(directoryButtons.elementAt(i));
}
}
}
this.add(pane);
this.pack();
this.setVisible(true);
}
当我尝试将按钮添加到向量(directoryButtons.add()行)时,我捕获异常。