나는 약간의 DERP 실수를 만들고 있어요, 그래서 만약 ...자바 : 사용자 입력을 기반으로 배열에 할당 변수
나는 JComboBox가 배열에 할당을 용서해주십시오, 자바 초보자 해요 fileNames라고합니다. 제 목표는 JComboBox가 특정 디렉토리의 모든 파일 이름을 표시하도록하는 것입니다. 지금까지 필자는 디렉토리에있는 모든 파일을 files라는 단일 문자열에 할당하는 코드를 작성했습니다.
코드 :이 후
package modmaker;
public class GuiBlocks2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JFileChooser filePath;
FileEditor fileeditor = new FileEditor();
/**
* Launch the application.
*/
static String files;
static String[] fileNames={files};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiBlocks2 frame = new GuiBlocks2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// Directory path here
String path = ".";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".png") || files.endsWith(".PNG"))
{
System.out.println(files);
}
}
}
}
파일 이름 배열에 할당 된 콤보 상자를 추가하는 또 다른 방법이있다. 내 질문 : 모든 파일을 배열에 할당하는 방법.
왜 정적 변수입니까? 왜 인스턴스 메소드에서 클래스 초기화 코드 (invokeLater 블록에 있음)를 제외한 모든 것을 수행합니까? Array를 사용하는 대신 DefaultComboBoxModel을 사용하고 for 루프에서 파일 이름을 식별 할 때 파일 이름을이 모델에 추가하십시오. –