当前位置: 技术问答>java相关
JTable 问 题
来源: 互联网 发布时间:2015-03-22
本文导语: import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.*; import java.util.*; public class mytable extends Frame { private JTable jt; private DefaultTableModel dtm=new DefaultTableModel(); private Panel pc; private...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class mytable extends Frame
{
private JTable jt;
private DefaultTableModel dtm=new DefaultTableModel();
private Panel pc;
private JButton jb;
public mytable()
{
pc = new Panel();
jb = new JButton("P");
jt = new JTable(dtm);
pc.add(jb);
add(jt,BorderLayout.CENTER);
add(pc,BorderLayout.SOUTH);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Vector dataVector = new Vector();
Vector columnIdentifiers = new Vector();
dataVector.addElement("Asdf");
dataVector.addElement("BsfdgF");
columnIdentifiers.addElement("A");
columnIdentifiers.addElement("B");
dtm.setDataVector(dataVector,columnIdentifiers);
}
});
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String d[])
{
mytable my = new mytable();
my.setSize(400,400);
my.show();
}
}
为什么点击按钮会出错???
java.lang.ClassCastException: java.lang.String
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:241)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:197)
at mytable.(mytable.java:24)
at mytable.main(mytable.java:53)
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class mytable extends Frame
{
private JTable jt;
private DefaultTableModel dtm=new DefaultTableModel();
private Panel pc;
private JButton jb;
public mytable()
{
pc = new Panel();
jb = new JButton("P");
jt = new JTable(dtm);
pc.add(jb);
add(jt,BorderLayout.CENTER);
add(pc,BorderLayout.SOUTH);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Vector dataVector = new Vector();
Vector columnIdentifiers = new Vector();
dataVector.addElement("Asdf");
dataVector.addElement("BsfdgF");
columnIdentifiers.addElement("A");
columnIdentifiers.addElement("B");
dtm.setDataVector(dataVector,columnIdentifiers);
}
});
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String d[])
{
mytable my = new mytable();
my.setSize(400,400);
my.show();
}
}
为什么点击按钮会出错???
java.lang.ClassCastException: java.lang.String
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:241)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:197)
at mytable.(mytable.java:24)
at mytable.main(mytable.java:53)
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class mytable extends Frame
{
private JTable jt;
private DefaultTableModel dtm=new DefaultTableModel();
private Panel pc;
private JButton jb;
public mytable()
{
pc = new Panel();
jb = new JButton("P");
jt = new JTable(dtm);
pc.add(jb);
add(jt,BorderLayout.CENTER);
add(pc,BorderLayout.SOUTH);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Object[][] obj1 = {{"sdfdsf","dsfdfs"},{"dsf","dsf"}};
Object[] obj2 = {"dsf","fds"};
dtm.setDataVector(obj1,obj2);
}
});
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String d[])
{
mytable my = new mytable();
my.setSize(400,400);
my.show();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class mytable extends Frame
{
private JTable jt;
private DefaultTableModel dtm=new DefaultTableModel();
private Panel pc;
private JButton jb;
public mytable()
{
pc = new Panel();
jb = new JButton("P");
jt = new JTable(dtm);
pc.add(jb);
add(jt,BorderLayout.CENTER);
add(pc,BorderLayout.SOUTH);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Object[][] obj1 = {{"sdfdsf","dsfdfs"},{"dsf","dsf"}};
Object[] obj2 = {"dsf","fds"};
dtm.setDataVector(obj1,obj2);
}
});
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String d[])
{
mytable my = new mytable();
my.setSize(400,400);
my.show();
}
}
|
是你的类型转换有错阿
仔细找一下,看看有什么地方有用到数据类型转换的地方
/*--by bookbobby(书呆)-+
| |
| 你说爱我只是习惯 |
| 再也不是喜欢 |
| 我给你的爱 |
| 已不再温暖 |
| |
+--by bookbobby(书呆)-*/
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。