我写了一个简单的,保存到一个xml文件中,你可以根据需要根据上面的回帖改动。
GUI界面程序:
package client;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import bean.EmailInfo;
public class ClientFrame extends JFrame {
private JLabel emailNameLbl;
private JLabel passwordLbl;
private JLabel rePasswordLbl;
private JLabel serverLbl;
private JTextField emailNameTxt;
private JPasswordField passwordTxt;
private JPasswordField rePasswordTxt;
private JButton submitBtn;
private JButton cancleBtn;
private JButton exitBtn;
public ClientFrame() {
super("邮箱注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initGUI();
addListeners();
pack();
centerWindow(this);
}
private void initGUI() {
JPanel namePnl = new JPanel();
namePnl.setLayout(new FlowLayout(FlowLayout.LEFT));
emailNameLbl = new JLabel("邮箱名 :
");
emailNameTxt = new JTextField(20);
serverLbl = new JLabel("@);
namePnl.add(emailNameLbl);
namePnl.add(emailNameTxt);
namePnl.add(serverLbl);
JPanel passwordPnl = new JPanel();
passwordPnl.setLayout(new FlowLayout(FlowLayout.LEFT));
passwordLbl = new JLabel("密码:
");
passwordTxt = new JPasswordField(10);
passwordPnl.add(passwordLbl);
passwordPnl.add(passwordTxt);
JPanel rePasswordPnl = new JPanel();
rePasswordPnl.setLayout(new FlowLayout(FlowLayout.LEFT));
rePasswordLbl = new JLabel("密码确认:");
rePasswordTxt = new JPasswordField(10);
rePasswordPnl.add(rePasswordLbl);
rePasswordPnl.add(rePasswordTxt);
JPanel mainContentPnl = new JPanel();
mainContentPnl.setLayout(new BoxLayout(mainContentPnl, BoxLayout.Y_AXIS ));
mainContentPnl.add(namePnl);
mainContentPnl.add(passwordPnl);
mainContentPnl.add(rePasswordPnl);
JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout(FlowLayout.RIGHT));
submitBtn = new JButton();
cancleBtn = new JButton("取消");
exitBtn = new JButton("退出");
buttonPnl.add(submitBtn);
buttonPnl.add(cancleBtn);
buttonPnl.add(exitBtn);
Container c = getContentPane();
c.add(mainContentPnl, BorderLayout.CENTER);
c.add(buttonPnl, BorderLayout.PAGE_END);
}
private void addListeners() {
submitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
EmailInfo info = new EmailInfo();
info.setEmailName(emailNameTxt.getText() + "test@.com");
info.setPassword(new String(passwordTxt.getPassword()));
info.setRePassword(new String(rePasswordTxt.getPassword()));
MyAction action = new MyAction(info);
action.actionPerformed(e);
if (action.getIsSaveSuc())
{
emailNameTxt.setText("");
passwordTxt.setText("");
rePasswordTxt.setText("");
}
}
});
submitBtn.setText("提交");
cancleBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
emailNameTxt.setText("");
passwordTxt.setText("");
rePasswordTxt.setText("");
}
});
exitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
private void centerWindow(Window w)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
int x = (screenSize.width - windowSize.width) / 2;
int y = (screenSize.height - windowSize.height) / 2;
w.setLocation(new Point(x, y));
}
public static void main(String[] args) {
new ClientFrame().setVisible(true);
}
}
保存邮箱信息的bean:
package bean;
public class EmailInfo {
private String emailName;
private String password;
private String rePassword;
public EmailInfo() {
}
public EmailInfo(String emailName, String password, String rePassword) {
this.emailName = emailName;
this.password = password;
this.rePassword = rePassword;
}
public String getEmailName() {
return emailName;
}
public void setEmailName(String emailName) {
this.emailName = emailName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRePassword() {
return rePassword;
}
public void setRePassword(String rePassword) {
this.rePassword = rePassword;
}
}
校验和保存的逻辑:
package client;
import java.awt.event.ActionEvent;
import java.beans.XMLEncoder;
import
import
import
import
import
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import bean.EmailInfo;
public class MyAction extends AbstractAction {
private EmailInfo info;
private String path = "c:/info.xml";
private boolean isSaveSuc = false;
public MyAction(EmailInfo info) {
this.info = info;
}
@Override
public void actionPerformed(ActionEvent event) {
if (validateInfo())
{
saveAsXml();
JOptionPane.showMessageDialog(null, "保存成功。");
isSaveSuc = true;
}
}
public boolean getIsSaveSuc()
{
return isSaveSuc;
}
private boolean validateInfo()
{
String name = info.getEmailName();
if (name == null || name.trim().length() == 0)
{
JOptionPane.showMessageDialog(null, "邮箱名不能为空");
return false;
}
String password = info.getPassword();
String rePassword = info.getRePassword();
if (password == null || password.trim().length() == 0)
{
JOptionPane.showMessageDialog(null, "密码不能为空");
return false;
}
else if (!password.equals(rePassword))
{
JOptionPane.showMessageDialog(null, "两次输入密码不同,请重新输入。");
return false;
}
return true;
}
/**
* 将一个JavaBean存储为一个XML格式的文件
*/
private void saveAsXml()
{
try {
//添加参数true,每次只是追加在文件的末尾
OutputStream out = new FileOutputStream(path, true);
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out));
e.writeObject(info);
e.close();
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "保存失败.");
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "保存失败.");
}
}
}
运行效果:
图片附件: 游客没有浏览图片的权限,请
登录 或
注册
[
本帖最后由 baifenghan 于 2010-5-22 02:07 编辑 ]