Topic: 怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!)

  Print this page

1.怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) Copy to clipboard
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.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文件是如何进行参数传递和协同工作的,望高手指点。谢谢了。

2.Re:怎么讲一个java文件分开写成多个java文件啊? [Re: jialichao] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-05-31 17:11

问题就在这个地方,上面那段代码使用内部类避免的问题,就是两个类之间的传值、对象引用的问题。
如果仅仅是传递值,可以写一些取值的方法,但如果涉及到对象的引用,比如说:
MyListener类中,加了这样一个判断,if(e.getSource()==submit),如果两个类分开写,那么传递当前窗口的submit组件的引用,将会一个很大的问题,至少我还不知道怎么解决。
期待前辈给出解决办法,继续关注......

3.Re:怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) [Re: jialichao] Copy to clipboard
Posted by: jialichao
Posted on: 2006-06-03 17:26

论坛里前辈这么多,怎么都不说话呀,给点意见啊。
我再次谢谢了。

4.Re:怎么讲一个java文件分开写成多个java文件啊?(高手请指教啊!) [Re: jialichao] Copy to clipboard
Posted by: jameszhang
Posted on: 2006-06-04 14:37

先说你的提问方式,问题比较大, 即使有人看了你的问题,也不一定有时间给你回答清楚, 而且 你还贴了那么长的代码, 问题尽量 精炼短小具体, 除非跟那位老大熟悉 直接pm 。

看了下,先问:你的问题和你代码有关系吗?


   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