快来找错呀
我想实现的功能是:从表单接收数据,然后调用jtable浏览刚才表单的内容,而jtable的构造方法中有一种是new jtable(object[][] o1,object[] o2),第一个参数是二维数组,用来存记录,第二个是一维数组,用来存字段,表单里的数据是不断填加的,所以二维数组应该是动态的,我用下面这个例子实现了动态二维数组,不过感觉麻烦,请高手指点在线恭候答案,谢谢!!此程序编译正常, "填加 "按钮正常,而 "浏览 "按钮就出错,怎么回事呢?
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class sunrg extends JFrame implements ActionListener
{
JTable jt;
String[] tableHead={ "姓名 ", "电话 ", "年龄 "};
Object [][] recorder;
JButton jb1;
JButton jb2;
JButton jb3;
JTextField[] jtf=new JTextField[3];
Vector v;
Vector v1;
JPanel jp;
public sunrg()
{
//final JFrame f=this;
//v1=new Vector();
//v1.addElement((JFrame)f);
v=new Vector();
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(null);
//cp.setBounds(new Rectangle(100,100,300,300));
jp=new JPanel();
jp.setLayout(null);
jp.setBounds(new Rectangle(50,50,500,500));
JLabel jl=new JLabel( "信息采集表 ");
jl.setBounds(new Rectangle(90,20,100,50));
JLabel jl1=new JLabel( "姓名 ");
jl1.setBounds(new Rectangle(60,60,25,15));
jtf[0]=new JTextField( " ");
jtf[0].setBounds(new Rectangle(86,60,100,15));
JLabel jl2=new JLabel( "电话 ");
jl2.setBounds(new Rectangle(60,80,25,15));
jtf[1]=new JTextField( " ");
jtf[1].setBounds(new Rectangle(86,80,100,15));
JLabel jl3=new JLabel( "年龄 ");
jl3.setBounds(new Rectangle(60,100,25,15));
jtf[2]=new JTextField( " ");
jtf[2].setBounds(new Rectangle(86,100,100,15));
jb1=new JButton( "填加 ");
jb1.setBounds(new Rectangle(40,125,60,25));
jb2=new JButton( "浏览 ");
jb2.setBounds(new Rectangle(110,125,60,25));
jb3=new JButton( "删除 ");
jb3.setBounds(new Rectangle(180,125,60,25));
jp.add(jl);
jp.add(jl1);
jp.add(jtf[0]);
jp.add(jl2);
jp.add(jtf[1]);
jp.add(jl3);
jp.add(jtf[2]);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
cp.add(jp);
//jtf1.addActionListener(this);
//jtf2.addActionListener(this);
//jtf3.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb1)
{
for(int j=0;j <=2;j++)
{
v.addElement((String)jtf[j].getText());
jtf[j].setText( " ");
}
}
if(ae.getSource()==jb2)
{
int i=0;//遍历vector用的索引值
//下面代码将vector里的元素负值给一个二维数组,此数组已知一维
for(int k=0;k <((int)v.size()/3);k++)
{
for(int h=0;h <=2;h++)
{
recorder[k][h]=(String)v.elementAt(i);
i++;
System.out.println(recorder[k][h]);
}
}
//用jtable(object[][] o1,object[] o2)构造函数创建表
jt=new JTable(recorder,tableHead);
//将表jt放到jscrollpane里
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(jt,v,h);
jsp.setBounds(new Rectangle(0,200,300,300));
jp.add(jsp);
}
}
public static void main(String[] args)
{
new sunrg();
}
}
[解决办法]
错误信息放上来啊。
[解决办法]
看代码看得好累 写的很业余哦
[解决办法]
总得说说哪方面的错误啊,中午在来看看