Posted by:jialichao
Posted on:2006-05-31 16:23
例如这段代码,怎么把它写成若干个独立的java文件,比如一个是控制类,作用是连接数据库写入数据。一个是边界类,生成界面。另一个是实体类,生成用户的实例。最后写一个main进行测试呢?
package mypackage;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
/**
* @param args
*/
JLabel username;
JLabel password;
JLabel repassword;
JLabel age;
JLabel name;
JLabel lname;
JTextField usertext;
JPasswordField passwordtext;
JPasswordField repasswordtext;
JTextField agetext;
JButton submit;
JRadioButton man;
JRadioButton woman;
String sex="男";
public Test()
{
Container c=this.getContentPane();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc= new GridBagConstraints();
c.setLayout(gbl);
gbc.anchor=GridBagConstraints.WEST;
username=new JLabel("用 户 名 : ");
gbc.gridx=0;
gbc.gridy=0;
gbl.setConstraints(username,gbc);
c.add(username);
usertext=new JTextField(15);
gbc.gridx=1;
gbc.gridy=0;
gbl.setConstraints(usertext,gbc);
c.add(usertext);
password=new JLabel("密 码 : ");
gbc.gridx=0;
gbc.gridy=1;
gbl.setConstraints(password,gbc);
c.add(password);
passwordtext=new JPasswordField(15);
passwordtext.setEchoChar('*');
gbc.gridx=1;
gbc.gridy=1;
gbl.setConstraints(passwordtext,gbc);
c.add(passwordtext);
repassword=new JLabel("确认密码 : ");
gbc.gridx=0;
gbc.gridy=2;
gbl.setConstraints(repassword,gbc);
c.add(repassword);
repasswordtext=new JPasswordField(15);
passwordtext.setEchoChar('*');
gbc.gridx=1;
gbc.gridy=2;
gbl.setConstraints(repasswordtext,gbc);
c.add(repasswordtext);
name=new JLabel("性 别 :");
gbc.gridx=0;
gbc.gridy=3;
gbl.setConstraints(name,gbc);
c.add(name);
Panel p=new Panel();
p.setLayout(new FlowLayout());
final ButtonGroup bg=new ButtonGroup();
man=new JRadioButton("男",true);
woman=new JRadioButton("女");
p.add(man);
p.add(woman);
c.add
;
bg.add(man);
bg.add(woman);
gbc.gridx=1;
gbc.gridy=3;
gbl.setConstraints(p,gbc);
age=new JLabel("年 龄 :");
gbc.gridx=0;
gbc.gridy=4;
gbl.setConstraints(age,gbc);
c.add(age);
agetext=new JTextField(15);
gbc.gridx=1;
gbc.gridy=4;
gbl.setConstraints(agetext,gbc);
c.add(agetext);
lname=new JLabel(" ");
gbc.gridx=1;
gbc.gridy=5;
gbl.setConstraints(lname,gbc);
c.add(lname);
submit=new JButton("注册");
gbc.gridx=1;
gbc.gridy=6;
gbl.setConstraints(submit,gbc);
c.add(submit);
final ImageIcon p1=new ImageIcon("SmsMsgSomeFail.gif");
final ImageIcon p2=new ImageIcon("SmsMsgFail.gif");
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==submit)
{
Connection con;
PreparedStatement ptm;
ResultSet rs;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:mydb";
con=DriverManager.getConnection(url,"","");
String name,age,pas,repas;
name=usertext.getText();
pas=new String(passwordtext.getPassword());
repas=new String(repasswordtext.getPassword());
age=agetext.getText();
if(pas.equals(repas)){
ptm=con.prepareStatement("insert into users values(?,?,?,?)");
ptm.setString(1,name);
ptm.setString(2,pas);
ptm.setString(3,age);
ptm.setString(4,sex);
ptm.executeUpdate();
JOptionPane.showMessageDialog(null,"注册成功 !欢迎您的加入 !","信息提示",JOptionPane.PLAIN_MESSAGE,p1);
ptm.close();
con.close();
}
else{
JOptionPane.showMessageDialog(null, "两次输入密码不相同 !", "错误提示",JOptionPane.PLAIN_MESSAGE,p2);
}
}
else
{
sex=((JRadioButton)(e.getSource())).getText();
}
}
catch(Exception ae){}
}
}
MyListener mylistener=new MyListener();
submit.addActionListener(mylistener);
man.addActionListener(mylistener);
woman.addActionListener(mylistener);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
Test enrolfram=new Test();
enrolfram.setTitle("新用户注册");
enrolfram.setResizable(false);
enrolfram.setSize(300,300);
enrolfram.setVisible(true);
}
}
我疑惑的是几个java文件是如何进行参数传递和协同工作的,望高手指点。谢谢了。