如何使用来自外部文件的信息创建 JComboBox?
How To Create JComboBox With Info From External Files?
我从另一个 class 调用以下代码,框架显示正确,但是我的 JComboBox 没有出现!我之前有一个工作版本,但是它无法识别我在同一个 class 中的变量之一!
- 如何创建适用于整个 class 而不仅仅是一部分的可变字符串。
- 使用以下代码,为什么我的 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);
}
}
在哪里调用 readJP1()
方法?你不需要,因为 Java 不会神奇地自己调用它,它永远不会运行。因此,关于添加数据和添加 JComboBox 的一种解决方案——调用所有必要的方法。另一个问题是您永远不会将 JComboBox、cb 添加到 JPanel、p1,因此即使调用 readJP1()
方法也是不够的——您必须在该方法内将 JComboBox 添加到 JPanel。此外,您还需要将该 JPanel 添加到 GUI,然后再将其设置为可见。
关于:
How can I create a variable string that applies to an entire class and not just a section of it.
声明一个字符串字段 -- 在 class 级别声明的实例变量。
其他问题:你的文件读取代码好像全错了。如果我是你,我会尝试从你的 GUI 中单独调试它,然后在你让它工作后,将它添加到 GUI 代码中。
- 您可以创建一个可在 class
内部访问的字段(成员变量)
您需要将 JComboBox 添加到 JPanel 中:
JPanel p1 = new JPanel();
String[] ho = { "jo", "ho", "joe" };
JComboBox cb = new JComboBox(ho);
// add the JComboBox to the JPanel:
p1.add(cb);
// then add the JPanel to this JFrame:
add(p1);
我从另一个 class 调用以下代码,框架显示正确,但是我的 JComboBox 没有出现!我之前有一个工作版本,但是它无法识别我在同一个 class 中的变量之一!
- 如何创建适用于整个 class 而不仅仅是一部分的可变字符串。
- 使用以下代码,为什么我的 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);
}
}
在哪里调用 readJP1()
方法?你不需要,因为 Java 不会神奇地自己调用它,它永远不会运行。因此,关于添加数据和添加 JComboBox 的一种解决方案——调用所有必要的方法。另一个问题是您永远不会将 JComboBox、cb 添加到 JPanel、p1,因此即使调用 readJP1()
方法也是不够的——您必须在该方法内将 JComboBox 添加到 JPanel。此外,您还需要将该 JPanel 添加到 GUI,然后再将其设置为可见。
关于:
How can I create a variable string that applies to an entire class and not just a section of it.
声明一个字符串字段 -- 在 class 级别声明的实例变量。
其他问题:你的文件读取代码好像全错了。如果我是你,我会尝试从你的 GUI 中单独调试它,然后在你让它工作后,将它添加到 GUI 代码中。
- 您可以创建一个可在 class 内部访问的字段(成员变量)
您需要将 JComboBox 添加到 JPanel 中:
JPanel p1 = new JPanel(); String[] ho = { "jo", "ho", "joe" }; JComboBox cb = new JComboBox(ho); // add the JComboBox to the JPanel: p1.add(cb); // then add the JPanel to this JFrame: add(p1);