Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 用java写的第一个比较大的程序
【标  题】:用java写的第一个比较大的程序
【关键字】:java
【来  源】:http://blog.csdn.net/liangjingbo/archive/2006/10/19/1341278.aspx

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

Your Ad Here

 

 程序具体分包如下:

--------------------------------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