当前位置: 技术问答>java相关
一个使用JTextField的小problem?
来源: 互联网 发布时间:2015-05-24
本文导语: 本人欲往一个JTextField中放一个Object对象(本来JTextField中放的是一个String类型的对象,我们可以用getText()获得JTextField中的内容),然后通过getObject()获得放在JTextField中的对象,可是总是不如我想象的那般简单,各位可...
本人欲往一个JTextField中放一个Object对象(本来JTextField中放的是一个String类型的对象,我们可以用getText()获得JTextField中的内容),然后通过getObject()获得放在JTextField中的对象,可是总是不如我想象的那般简单,各位可否指点一二,本人将以重分酬谢!
|
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;}
}
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 () :
你的意思是不是这样的?将一个对象序列化,并将对象的序列化字串设置为文本框的内容。 然后得到文本框的字符串,将字符串再还原成原来的对象
你是不是这个意思,我怕我弄错了,如果是,我可以为你写一个例子,请回答我!
TO shelly () :
你的意思是不是这样的?将一个对象序列化,并将对象的序列化字串设置为文本框的内容。 然后得到文本框的字符串,将字符串再还原成原来的对象
你是不是这个意思,我怕我弄错了,如果是,我可以为你写一个例子,请回答我!