【发布时间】:2023-03-07 01:09:01
【问题描述】:
我有以下代码:
问题似乎无法将标题放入表格中的单独列中,而是我在这部分中定义的列标题
String[] columnNames ={container.toString()};
并且源自
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
//create array--------------
ArrayList list = new ArrayList();
Scanner scanner = new Scanner(in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//add line to array-------------
list.add(line);
}
//get headings of data------------------
//String heading[] = list.get(2).toString();
String[] simpleArray = new String[list.size()];
list.toArray(simpleArray);
String j = (String) list.get(2);
String [] items = j.split(";");
List<String> container = Arrays.asList(items);
createUserInterface(container);
全部出现在第一列(因此是表中的唯一列) 因此,我只看到“名字”出现在数据字段中
我的标题的Println是
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R,S] *I have substituted my actual headings with letters
帮助赞赏
代码如下--
public void createUserInterface(List<String>container) {
//create user interface---------------------
setLayout( new BorderLayout());
setTitle("Fund");//setTitle is also a JAVA Parameter
setSize(2000, 200);
setBackground(Color.gray);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create a panel to hold all other components
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel);
//
System.out.println(container.toString());
List list = Arrays.asList(container);
Object[] columnNames ={list};
Object[][] data = {{"First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years"}};
table = new JTable(data, columnNames);
// Create a new table instance
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
setVisible(true);
}
【问题讨论】:
-
以后,请发布格式良好的代码。如果我们能阅读您的代码,我们就能更好地理解它并为您提供帮助。
-
我已经用更多信息更新了我的问题
-
您仍然没有将 Array 或 Collection 传递给您的 JTable 构造函数。你正在传递一个字符串!如果它是一个列表,则将参数从
Object更改为List<String>并将其传递给JTable,而不是toString()。这完全没有意义。 -
而且您的代码格式仍然很糟糕。再次,请修复。您发布的所有左对齐代码 - 为什么?
-
抱歉,我会解决这个问题,我对 Java 比较陌生,所以如果我遇到天真,我很抱歉。
标签: java arrays swing jtable heading