【发布时间】:2025-11-25 14:20:05
【问题描述】:
我正在从另一个类调用以下代码,并且框架显示正确,但是我的 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);
}
}
【问题讨论】:
标签: java swing file-io jcombobox