package table;
public class grade
{
String name;
int china;
int math;
int all;
boolean ok;
public grade(String name, int china, int math, int all, boolean ok)
{
this.name = name;
this.china = china;
this.math = math;
this.all = all;
this.ok = ok;
}
}
package table;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.*;
public class table
{
public table()
{
JFrame f=new JFrame();
Vector<grade> va=new Vector<grade>();
grade gr1=new grade("网鹏",new Integer(82),new Integer(91),new Integer(100),new Boolean(true));
grade gr2=new grade("网",new Integer(82),new Integer(91),new Integer(100),new Boolean(true));
grade gr=new grade("鹏",new Integer(82),new Integer(91),new Integer(100),new Boolean(true));
va.addElement(gr);
va.addElement(gr2);
va.addElement(gr1);
Vector<String> ve=new Vector<String>();
ve.add("姓名");
ve.add("语文");
ve.add("数学");
ve.add("总分");
ve.add("及格");
JTable table=new JTable(va,ve); // 这里报错为什么啊 table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
f.getContentPane().add(scrollPane,BorderLayout.CENTER);
f.setTitle("表格测试窗口");
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new table();
}
}
------解决方案--------------------
用object
------解决方案--------------------
请参考 Java API Doc 中对 JTable的这个构造方法的描述
JTable
public JTable(Vector rowData,
Vector columnNames)构造一个 JTable 来显示 Vector 所组成的 Vector rowData 中的值,其列名称为 columnNames。rowData 中包含的 Vector 应该包含该行的值。换句话说,可以通过以下代码获取第 1 行,第 5 列单元格的值:
((Vector)rowData.elementAt(1)).elementAt(5);
参数:
rowData - 新表的数据
columnNames - 每列的名称
你需要在 Vector中填充一个Vector,表示1行的数据,而不是你自定义的grade类
PS:类名最好以大写字母开头
------解决方案--------------------
- Java code
public class table { public table() { JFrame f = new JFrame(); Vector<Vector<Object>> va = new Vector<Vector<Object>>(); grade gr1 = new grade("网鹏", new Integer(82), new Integer(91), new Integer(100), new Boolean(true)); grade gr2 = new grade("网", new Integer(82), new Integer(91), new Integer(100), new Boolean( true)); grade gr = new grade("鹏", new Integer(82), new Integer(91), new Integer(100), new Boolean( true)); va.addElement(gr.toVector()); va.addElement(gr2.toVector()); va.addElement(gr1.toVector()); Vector<String> ve = new Vector<String>(); ve.add("姓名"); ve.add("语文"); ve.add("数学"); ve.add("总分"); ve.add("及格"); JTable table = new JTable(va, ve); // 这里报错为什么啊 // table.setPreferredScrollableViewportSize(new // Dimension(550,30)); JScrollPane scrollPane = new JScrollPane(table); f.getContentPane().add(scrollPane, BorderLayout.CENTER); f.setTitle("表格测试窗口"); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]) { new table(); }}class grade { String name; int china; int math; int all; boolean ok; public grade(String name, int china, int math, int all, boolean ok) { this.name = name; this.china = china; this.math = math; this.all = all; this.ok = ok; } public Vector<Object> toVector() { Vector<Object> v = new Vector<Object>(); v.add(name); v.add(china); v.add(math); v.add(all); v.add(ok); return v; }}