Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!)
jialichao





发贴: 12
积分: 0
于 2006-05-31 16:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
例如这段代码,怎么把它写成若干个独立的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.addCamera;
    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文件是如何进行参数传递和协同工作的,望高手指点。谢谢了。


jialichao edited on 2006-06-03 17:24


请求JAVA学习方向?????

话题树型展开
人气 标题 作者 字数 发贴时间
5007 怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) jialichao 9229 2006-05-31 16:23
4273 Re:怎么讲一个java文件分开写成多个java文件啊? lisliefor 208 2006-05-31 17:11
3938 Re:怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) jialichao 32 2006-06-03 17:26
3998 Re:怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) jameszhang 111 2006-06-04 14:37

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923