一个使用JTextField的小problem? JTextField就是放文本的,你放一个Object有何意义呢?不明白你的用意到底是什么! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果非要不可的话,你可以扩展一个Document,将你的object放在document中. 你完全可以把OBJECT放入容器中,再在界面做些手脚,看起来好象文本框。 我觉得楼上几位好象误解了吧TO shelly () :你的意思是不是这样的?将一个对象序列化,并将对象的序列化字串设置为文本框的内容。 然后得到文本框的字符串,将字符串再还原成原来的对象你是不是这个意思,我怕我弄错了,如果是,我可以为你写一个例子,请回答我! To xiaoyoo:没错,我确实就是你所说的意思,现在嘛我也用了一个很简单的方法解决了这个问题,不过我还是很想看看你的例子,可以吗? import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class ObjectTest extends JFrame{ Container con=getContentPane(); JButton objectToTextField=new JButton("ObjectToTextField"); JButton reBuilderObject=new JButton("ReBuilderObject"); JTextField textField=new JTextField(12); MyObject obj=new MyObject(); byte[] bytes=null; public ObjectTest() { con.setLayout(new FlowLayout()); con.add(textField); con.add(objectToTextField); con.add(reBuilderObject); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(100,100,500,100); setResizable(false); objectToTextField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { ByteArrayOutputStream byteOut=new ByteArrayOutputStream(); ObjectOutputStream out=new ObjectOutputStream(byteOut); out.writeObject(obj); bytes=byteOut.toByteArray(); String s=new String(bytes); textField.setText(s); //System.out.println(s); }catch(IOException ioe){System.out.println(e.toString());} } }); reBuilderObject.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { String s=textField.getText(); ByteArrayInputStream bytesIn=new ByteArrayInputStream(bytes); ObjectInputStream in=new ObjectInputStream(bytesIn); //Object object=in.readObject(); MyObject myObject=(MyObject)(in.readObject()); System.out.println("--info--"); System.out.println("name_:"+myObject.getName()); System.out.println("sex_:"+myObject.getSex()); System.out.println("age_:"+myObject.getAge()); }catch(Exception ex){ex.printStackTrace();} } }); } public static void main(String[] args) { ObjectTest test=new ObjectTest(); test.show(); }}class MyObject implements Serializable{ private String name="xioyoo"; private String sex="male"; private int age=21; public String getName(){return name;} public String getSex(){return sex;} public int getAge(){return age;}} to shelly() 我写地好辛苦啊,你要给我加分哦。代码有什么不清楚的地方随时说 java中ThreadPoolException类的interruptIfIdle方法 Java 字符串替换 急用100分 求这道题的解答,用java也好,用c也好! java时间差问题 有关Jfreechart画折线图的问题 帮忙! 虚拟现实项目,不知如何报价?求救 java实现用户行为采集 求助 请教一个小问题,请各位高手赐教 如何将文件流转化为byte[]??? 如用ImageIcon在JLabel里添加一副图,出现访问被拒绝怎么办?
TO shelly () :
你的意思是不是这样的?将一个对象序列化,并将对象的序列化字串设置为文本框的内容。 然后得到文本框的字符串,将字符串再还原成原来的对象你是不是这个意思,我怕我弄错了,如果是,我可以为你写一个例子,请回答我!
没错,我确实就是你所说的意思,现在嘛我也用了一个很简单的方法解决了这个问题,不过我还是很想看看你的例子,可以吗?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ObjectTest extends JFrame
{
Container con=getContentPane();
JButton objectToTextField=new JButton("ObjectToTextField");
JButton reBuilderObject=new JButton("ReBuilderObject");
JTextField textField=new JTextField(12);
MyObject obj=new MyObject();
byte[] bytes=null;
public ObjectTest()
{
con.setLayout(new FlowLayout());
con.add(textField);
con.add(objectToTextField);
con.add(reBuilderObject);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setBounds(100,100,500,100);
setResizable(false);
objectToTextField.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream(byteOut);
out.writeObject(obj);
bytes=byteOut.toByteArray();
String s=new String(bytes);
textField.setText(s);
//System.out.println(s);
}catch(IOException ioe){System.out.println(e.toString());}
}
});
reBuilderObject.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String s=textField.getText();
ByteArrayInputStream bytesIn=new ByteArrayInputStream(bytes);
ObjectInputStream in=new ObjectInputStream(bytesIn);
//Object object=in.readObject();
MyObject myObject=(MyObject)(in.readObject());
System.out.println("--info--");
System.out.println("name_:"+myObject.getName());
System.out.println("sex_:"+myObject.getSex());
System.out.println("age_:"+myObject.getAge());
}catch(Exception ex){ex.printStackTrace();}
}
});
}
public static void main(String[] args)
{
ObjectTest test=new ObjectTest();
test.show();
}
}
class MyObject implements Serializable
{
private String name="xioyoo";
private String sex="male";
private int age=21;
public String getName(){return name;}
public String getSex(){return sex;}
public int getAge(){return age;}
}
代码有什么不清楚的地方随时说