JTextField就是放文本的,你放一个Object有何意义呢?不明白你的用意到底是什么!

解决方案 »

  1.   

    如果非要不可的话,你可以扩展一个Document,将你的object放在document中.
      

  2.   

    你完全可以把OBJECT放入容器中,再在界面做些手脚,看起来好象文本框。
      

  3.   

    我觉得楼上几位好象误解了吧
    TO shelly () :
    你的意思是不是这样的?将一个对象序列化,并将对象的序列化字串设置为文本框的内容。  然后得到文本框的字符串,将字符串再还原成原来的对象你是不是这个意思,我怕我弄错了,如果是,我可以为你写一个例子,请回答我!
      

  4.   

    To xiaoyoo:
    没错,我确实就是你所说的意思,现在嘛我也用了一个很简单的方法解决了这个问题,不过我还是很想看看你的例子,可以吗?
      

  5.   

    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;}
    }
      

  6.   

    to shelly() 我写地好辛苦啊,你要给我加分哦。
    代码有什么不清楚的地方随时说