用java写的第一个比较大的程序

 

 程序具体分包如下:

--------------------------------client--------------------------------------------

-----------LoginDialog.java

 

package  client;

import  javax.swing.JDialog;
import  javax.swing.JTextField;
import  java.beans.PropertyChangeListener;
import  javax.swing.JOptionPane;
import  javax.swing.ImageIcon;
import  java.beans.PropertyChangeEvent;
import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.JPasswordField;

class  LoginDialog  extends  JDialog {
    JOptionPane optionPane;
     
public LoginDialog(JFrame frame)
  
{
      
super(frame, "Login Box"true);

      
/** label for the Welcome
   
*/

  
final JLabel Welcome = new JLabel("Welcome to Symposium");
  
/** label for the Login field
   
*/

  
final JLabel myLogin = new JLabel("User Name: ");
  
/** label for the Password field
   
*/

  
final JLabel thePassword = new JLabel("Password: ");
  
/** text field that is used to input the login name
   
*/

  
final JTextField loginName = new JTextField(12);
  
/** password field that is used to input the password
   
*/

  
final JPasswordField password = new JPasswordField(12);



  password.setEchoChar(
'*');
  
/** An object array holding the login objects
   
*/

  
//user version
  
//Object[] array = {Welcome, myLogin, loginName, thePassword, password};
  
//presenter version
  Object[] array = {Welcome, myLogin, loginName, thePassword, password};
  
final String btnString1 = "Login";
  
final String btnString2 = "Cancel";

  
/** An object array holding the button objects
   
*/

  Object[] options 
= {btnString1, btnString2};

  optionPane 
= new JOptionPane(array,
                              JOptionPane.CANCEL_OPTION,
                              JOptionPane.NO_OPTION,

                              
new ImageIcon("2.jpg") ,options,options[0]);

        
this.getContentPane().add(optionPane);
 
//this. setContentPane(optionPane);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 
//---------------------------------------------------------------
 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
           
public void propertyChange(PropertyChangeEvent e) {
               String prop 
= e.getPropertyName();

               
if (isVisible()
                
&& (e.getSource() == optionPane)
                
&& (prop.equals(JOptionPane.VALUE_PROPERTY) ||
                    prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) 
{
                   Object value 
= optionPane.getValue();

                   
if (value == JOptionPane.UNINITIALIZED_VALUE) {
                       
return;
                   }


                   
// Reset the JOptionPane's value.
                   optionPane.setValue(
                           JOptionPane.UNINITIALIZED_VALUE);

                   
if (value.equals(btnString1)) {
                           login 
= loginName.getText();
                           myPassword 
= new String(password.getPassword());

                           setVisible(
false);
                           password.setText(
"");
                   }
 else // user closed dialog or clicked cancel
                         System.exit(0);
                   }

               }

           }

       }
);

         
this.pack();
        
this.setLocation(150,150);
        
this.setVisible(true);


  }

  
public String loginName()
  
{
  
return login;
  }

  
public String getPass()
  
{
    
return myPassword;
  }

  
private String login;
/** contain the password information
 
*/

private String myPassword;
/** reflect the category
 
*/



 }

 

----------ClientUI.java

 

package  client;
import  javax.swing. * ;
import  java.awt. * ;
import  java.awt.event. * ;
import  java.beans. * ;
import  constant.Constant;
public   class  ClientUI  extends  JFrame  implements  ActionListener,PropertyChangeListener {
    String myName;
    
private ClientClass client;
   
private JButton send;
   
private JTextArea edit;
   
private JTextArea area;
   
private JScrollPane show1;
   
private JScrollPane show2;
   
private JLabel label;
   
private JMenu action;
   
private JMenuItem modifyName;
   
private JMenuBar menuBar;
   
private JMenuItem exit;
   
private JMenu     help;
   
private JMenuItem about;
   
/*
  *  (non-Javadoc)
  * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
  
*/

   
public void propertyChange(PropertyChangeEvent e)
   
{
           String name
=e.getPropertyName();
           
if(name.equals("public_chat"))
           
{
                  rMsg((String) e.getNewValue());
           }


   }

   
/* manipulate receive msg
    *
    
*/

   
public void rMsg(String msg)
   
{

           area.append(msg);
   }

  
/* init method--------------------------------------
   *
   
*/

   
public void init()
{
     send
=new JButton("Send");

     send.addActionListener(
this);
     label
=new JLabel("Message :  ");
     edit
=new JTextArea(3,3);
     show2
=new JScrollPane(edit);
     area
=new JTextArea("Room 111 chat:  ",14,30);
     area.setEditable(
false);
     show1
=new JScrollPane(area);
  
// JMenu-------------------------------------------
    menuBar=new JMenuBar();
    exit
=new JMenuItem("Exit");
    exit.addActionListener(
this);
    action
=new JMenu("Action");
    modifyName
=new JMenuItem("ModifyName");
    modifyName.addActionListener(
this);
    action.add(modifyName);
    action.add(exit);
    help
=new JMenu("Help");
    about
=new JMenuItem("About");
    about.addActionListener(
this);
    help.add(about);

    menuBar.add(action);
    menuBar.add(help);
 
//layout------------------------------------------
     JPanel panel=new JPanel(new BorderLayout());
     panel.add(label,BorderLayout.WEST);
     panel.add(show2,BorderLayout.CENTER);
     panel.add(send,BorderLayout.EAST);
    getContentPane().add(show1,BorderLayout.CENTER);
    getContentPane().add(panel,BorderLayout.SOUTH);
    
this.setJMenuBar(menuBar);
    
this.addWindowListener(new WindowAdapter() {
        
public void windowClosing(WindowEvent we) {
           System.exit(
0);
    }

}
);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    
this.setBounds(0,0,400,400);
    
this.setVisible(true);
}

public ClientUI()
{
         
super("Client!");

         client
=new ClientClass(Constant.Server_address,Constant.Server_port);
          loginUser();
         client.addPropertyChangeListener(
this);
         init();
}

//loginUser()------------------------------------------
 public void  loginUser()
 
{
     LoginDialog log
=new LoginDialog(this);
      myName
=log.loginName();
      String myPassword
=log.getPass();
     
if(myName.equals("")||myPassword.equals(""))
     
{
        System.exit(
-1);
     }

     client.loginUser(myName,myPassword);
 }

//--------------actionPerform-------------------------
  public void actionPerformed(ActionEvent e)
  
{

          
if(e.getSource()==send)
          
{
                
if(!edit.getText().equals(""))
                
{
                 
//   area.append(edit.getText()+" ");
                    client.output(myName +"_________说 :::  "+edit.getText());
                    edit.setText(
null);
                }

          }

        
else if(e.getSource()==modifyName)
        
{
          myName
=JOptionPane.showInputDialog(null,"input your wish name","modifyName"
                                             ,JOptionPane.INFORMATION_MESSAGE);
        }

       
else if(e.getSource()==exit)
       
{
        System.exit(
0);
       }

      
else if(e.getSource()==about)
      
{
         JOptionPane.showMessageDialog(
null,"作者—— 梁景波,作品完成于51假期, "
                                       ,
"author",JOptionPane.INFORMATION_MESSAGE);
      }

   }

   
public static void main(String[] args) {
                ClientUI client
=new ClientUI();

        }


}

 

----------ClientClass.java

 

package  client;
import  java.net. * ;
import  java.io. * ;
import  java.beans. * ;
import  constant.Constant;
public   class  ClientClass  {
        
private Socket clientSocket;
    
private ObjectInputStream in;
        
private PropertyChangeSupport propertySupport;
        
private ObjectOutputStream out;
//constructor-------------------------------------------

    
public ClientClass(String address,final int port){
     
try{
         clientSocket
=new Socket(address,port);
        out
=new ObjectOutputStream(clientSocket.getOutputStream());
        in
=new ObjectInputStream(clientSocket.getInputStream());
     }
catch(IOException e){
       e.printStackTrace();

    }

         propertySupport
=new PropertyChangeSupport(this);
         ServerReader socketRead 
= new ServerReader(this);
                socketRead.start();
    }

/* loginUser()-------------------------------------------------------
 *
 
*/


 
public void loginUser(String name,String passWord)
 
{
   
try{
     out.writeObject(name);
     out.writeObject(passWord);
     Integer value
=(Integer)in.readObject();

     
if(value.intValue()==Constant.Login_OK)
         
return;
     
else System.exit(0);
   }
catch(Exception e){
       System.out.println(
"_______the connect to server is failed!!");
       e.printStackTrace();
   }


 }

/*output data------------------------------------------
 *
 
*/

    
public void output(String str)
    
{
        
try{

        out.writeObject(str);
       
// out.close();
        }
catch(IOException e){}

    }

/* receive message alter clientUI
 *
 
*/

    
public void receiveMsg(String msg)
    
{
        propertySupport.firePropertyChange(
"public_chat",null,msg);

    }

    
/**
     * addPropertyChangeListener registers a Listener
     *
     * 
@param listener The Listener for perperty change
     
*/

public void addPropertyChangeListener (PropertyChangeListener listener) {
   propertySupport.addPropertyChangeListener (listener);
}

}

 

----------ServerReader.java

 

package  client;
import  java.net. * ;
import  java.io. * ;
import  constant.Constant;
public   class  ServerReader  extends  Thread  {
   
private ServerSocket sClient;
   
private ClientClass c;
   
private Socket clientSocket ;
   
private ObjectInputStream in;
  
/* constructor
   * @param client classclient of name
   
*/

   
public ServerReader(ClientClass client)
   
{     this.c=client;

   }

  
/*  getMsg()method
   *
   
*/

 
/*  public  String getMsg()
   {  String str="";
           try{
          in=new ObjectInputStream(clientSocket.getInputStream());
            str=(String)in.readObject();
           }catch(Exception e){}
           return str;
   }
*/

  
/*  thread run()
   *
   
*/

   
public void run()
   
{
           
try{
                   sClient
=new ServerSocket(Constant.Client_port);
                   clientSocket
=sClient.accept();
                   in
=new ObjectInputStream(clientSocket.getInputStream());
           }
catch(IOException e){}
            
while(true){
                
try{
                String received;
                received
=(String)in.readObject();
                
if(received.equals(""))continue;
                
else {

                    c.receiveMsg(received);
                    received
="";
                }

            }
catch(Exception e){}

   }

}

}

 

 

---------------------------- server---------------------------------------------

----------------------Login.java

 

package  server;
import  java.net. * ;
import  java.io. * ;
import  java.lang.Thread;
import  constant.Constant;
public   class  Login   extends  Thread {
    
private ServerSocket server;
    
private Socket client;
    
private ObjectInputStream in;
    
private ServerClass s=new ServerClass();
    
public Login()
    
{
         
try{
                 System.out.println(
"initialization!");
                 server
=new ServerSocket(Constant.Server_port);

           }
catch(IOException e){e.printStackTrace();}

    }

    
public synchronized void run()
   
{
         
int i=0;
         String clientName;
           
while(true)
           
{  try{

                   i
++;
                   System.out.println(
"server is running!");
                   client
=server.accept();
                   ObjectInputStream in 
= new ObjectInputStream(client.getInputStream());
                   ObjectOutputStream outSide 
= new ObjectOutputStream(client.getOutputStream());
                                String name 
= (String)in.readObject();
                                String pass 
= (String)in.readObject();
                                System.out.println(
"accepted:  " +"clientName : "+ name + " password :" + pass );



               
//temporary avariable..............
                   s.loginUser(client,name,pass,outSide);
              
//     ClientReader cc=new ClientReader(s,client,in);
                
//   cc.start();
           }
catch(Exception e){ e.printStackTrace(); }

           }


   }

        
public static void main(String[] args) {
                Login login
=new Login();
                login.start();

        }


}

 

----------------------ServerClass.java

 

package  server;
import  java.net. * ;
import  java.io. * ;
import  constant.Constant;
import  java.util.Vector;
public   class  ServerClass  {
 
//data field-------------------------------

        
private Socket socket;
        
private ObjectOutputStream out;
        
private Vector allOut;
/** constructor ServerClass
 *
 
*/

        
public ServerClass()
        
{
                allOut
=new Vector(10);

        }

        
/** loginUser()method
         *
         
*/

        
public void loginUser(Socket s,String name,String pass,ObjectOutputStream outSide)
        
{

                
try{
                        
this.socket=new Socket(s.getInetAddress(),Constant.Client_port);
                        out
=new ObjectOutputStream(socket.getOutputStream());
                        
if(name.equals("ljb")||name.equals("lm"))
                   outSide.writeObject(
new Integer(Constant.Login_OK));
                         
else
                   outSide.writeObject(
new Integer(Constant.Login_fail));
                }
catch(IOException e){}

                allOut.add(out);
        }

        
/** broadcastMsg() method
         *
         
*/

        
public void broadcastMsg(String msg)
        
{
                
for(int i=0;i<allOut.size();i++)
                
{
                        
try{
                    ObjectOutputStream temp
=(ObjectOutputStream)allOut.get(i);
                     temp.writeObject(
" "+msg);
                        }
catch(IOException e){e.printStackTrace();}
                }


        }

        
/** main() method
         * 
@param args
         
*/

        
public static void main(String[] args) {

        }


}

 

----------------------ClientReader.java

 

package  server;
import  java.lang.Thread;
import  java.net. * ;
import  java.io. * ;
import  constant.Constant;
public   class  ClientReader  extends  Thread  {

   
private ServerClass server;
   
private ObjectInputStream in;
   
private Socket sInto;
/** constructor
 * 
@param
 
*/

  
public ClientReader(ServerClass server,Socket s,ObjectInputStream in)
        
{
        
this.server=server;
        
this.in=in;
        
this.sInto=s;
        }

/** run()method--read message...
 *
 
*/

        
public synchronized void run()
        
{
                
while(true)
                
{
                        String msg;
                        
try{

                            msg
=(String)in.readObject();
                                
if(!msg.equals(""))
                                
{
                                        server.broadcastMsg(msg);
                                    msg
="";
                                }

                                }
catch(Exception e){e.printStackTrace();}


                }




            }

        
/**
         * 
@param args
         
*/

        
public static void main(String[] args) {


        }


}

 

----------------------constant-------------------------------------------

----------------------Constant.java

package  constant;

public   class  Constant  {

 
public  static  final int Server_port =8587;
 
public  static final String Server_address="127.0.0.1";
 
public  static final int Client_port=8829;
 
public  static final int Login_OK=22;
 
public  static final int Login_fail=10;
}

 

----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值