我從另一個類調用以下代碼,並且框架顯示正確,但是我的JComboBox只是不顯示!我早些時候有一個工作版本,但它沒有識別出同一個類中的一個變量!如何使用外部文件的信息創建JComboBox?
- 如何創建適用於整個類的變量字符串,而不僅僅是一部分。
- 用下面的代碼,爲什麼我的JComboBox不會顯示,我在做什麼錯誤?
import java.io.File;
import java.util.Scanner;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ProjectList extends JFrame {
private static final long serialVersionUID = 1l;
String Path = new File("").getAbsolutePath();
public Scanner x;
public ProjectList() {
super("My File");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setAlwaysOnTop(true);
setUndecorated(true);
setLocation(444, 327);
setSize(400, 250);
try {
x = new Scanner(new File(Path + "/Name.txt"));
} catch (Exception e) {
System.out.println("Path Error: Path Does Not Exist!");
}
}
public void readPJ1() {
while (x.hasNext()) {
String PJ1name = x.next();
}
String PJ1 = "";
String PJ2 = "";
String PJ3 = "";
String PJ4 = "";
String PJ5 = "";
String PJ6 = "";
String PJ7 = "";
String PJ8 = "";
String PJ9 = "";
String PJ10 = "";
String PJ11 = "";
String PJ12 = "";
String PJ13 = "";
String PJ14 = "";
String PJ15 = "";
String PJ16 = "";
String PJ17 = "";
String PJ18 = "";
String PJ19 = "";
String PJ20 = "";
JPanel p1 = new JPanel();
String[] ho = { "jo", "ho", "joe" };
JComboBox cb = new JComboBox(ho);
add(p1);
}
}