我需要一个下拉菜单,列出现有文本文件中的所有条目。我还没有真正了解如何执行此操作,文本文件中的每个条目都位于新行上。这是我到目前为止所拥有的:
JComboBox cb = new JComboBox();
cb.Items.AddRange(System.IO.File.ReadAllLines("names.txt"));
我收到一条警告
References to generic type JComboBox should be parameterized
还有两个错误
Items cannot be resolved or is not a field
IO cannot be resolved or is not a field
有人能给我指出正确的方向或给出解决方案吗?非常感谢
最佳答案
System.IO.File.ReadAllLines 是 C# 方法,而不是 Java。
您想使用java.nio.file.Files.readAllLines .
This page展示如何通过参数化正确使用 JComboBox。
关于Java 使用文本文件中的条目创建一个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36598598/