初学 java的swing控件

package  killer;
import  java.beans. * ;
import  javax.swing.event. * ;
import  java.io. * ;
import  java.awt. * ;
import  java.awt.event. * ;
import  java.util. * ;
import  javax.swing. * ; // /this is a server!!!!!!!!!!!!!!!!!!!!!!!!!
import  java.net. * ;
/**
  * ChatDisplay is a user interface to display chat messages.
  * User can add and remove a chat user, display a message in a ChatDisplay
  * or send a message to the ChatDisplay. If there is an error, nothing happens.
  
*/


public   class  Server   extends  JFrame  implements  ActionListener {
//constructor------------------------------------------------------------------------------

     
public Server() {



//--------------------------------------------------------------------------------------------
//Layout GUI----------------------------------------------------------------------------------


     
super("Server----chat");
     
this.getContentPane().setLayout(new BorderLayout());
//JMenuBar------------------------------------------------------------------------------------

      menuBar
=new JMenuBar();
      action
=new JMenu("Action");
      file
=new JMenu("File");
      file.setMnemonic(
'f');
      savePublic
=new JMenuItem("SavePublic");
      savePublic.addActionListener(
this);
      savePrivate
=new JMenuItem("SavePrivate");
      savePrivate.addActionListener(
this);

      file.add(savePublic);
      file.add(savePrivate);
  
//JMenu---------------------------------------------------------------------------------------

      exit 
=new JMenuItem("Exit");
      exit.addActionListener(
this);
      file.add(exit);
      add
=new JMenuItem("Add a Friend");
      add.addActionListener(
this);
      delete
=new JMenuItem("Delete a Friend");
      delete.addActionListener(
this);
      action.add(delete);
      action.add(add);
      menuBar.add(file);
      menuBar.add(action);

  
//JTabbedPane JScrollPane--------------------------------------------------------------------
      JPanel Jpanel4;
      Jpanel4
=new JPanel();
      jdp 
=new JTabbedPane(jdp.BOTTOM);
     p1
=new JTextArea("this is public chat :"+" ");
     p1.setPreferredSize(
new Dimension(30,40));
     jsp2
=new JScrollPane(p1);  //jsp2
     jdp.add(jsp2,Jpanel4,0);
     jdp.setTitleAt(
0,"public");


     p2
=new JTextArea("this is private chat :"+" ");
     p2.setPreferredSize(
new Dimension(30,40));
     jsp3
=new JScrollPane(p2);  //jsp3
     jdp.add(jsp3,Jpanel4,1);
     jdp.setTitleAt(
1,"private");
     Jpanel4.setLayout(
new BorderLayout());
     Jpanel4.add(jdp,BorderLayout.CENTER);

     
//JcomboBox------------------------------------------------------------------------------

      JPanel panel1;
      panel1
=new JPanel(new BorderLayout());
      sentTip
=new JLabel("Send To");
      panel1.add(sentTip,BorderLayout.WEST);
      cob
=new JComboBox(str);
      panel1.add(cob,BorderLayout.CENTER);

//JTextFiled-----------------------------------------------------------------------------------

      JPanel panel2;
      panel2
=new JPanel(new BorderLayout());
      message
=new JLabel("Message:");
      panel2.add(message,BorderLayout.WEST);
      tfd
=new JTextArea("Hello",1,2);
       jsp
=new JScrollPane(tfd);
      panel2.add(jsp,BorderLayout.CENTER);
      JButton sent 
=new JButton("Send");
      sent.addActionListener(
this);
      panel2.add(sent,BorderLayout.EAST);

      
//Layout All-----------------------------------------------------------------------------------

      JPanel panel3;
      panel3
=new JPanel(new BorderLayout());
      panel3.add(panel1,BorderLayout.NORTH);
      panel3.add(panel2,BorderLayout.SOUTH);
      Jpanel4.add(panel3,BorderLayout.SOUTH);

           
this.setSize(400,400);
           
this.getContentPane().add(Jpanel4,BorderLayout.CENTER);
           
this.setJMenuBar(this.menuBar);/important sentence!!!!
           this.addWindowListener(new WindowAdapter(){
              
public void windowClosing(WindowEvent e){
              System.exit(
0);
              }

             }
);
           
this.setVisible(true);



//Layout GUI is over-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
           try{

            server
=new  ServerSocket(PROT);

          serverSocket
= server.accept();                  //程序在这里等待!

           sin
=serverSocket.getInputStream();
            sout
=serverSocket.getOutputStream();
            }
catch(IOException e){
              e.printStackTrace();
            }

            
while(true){

         
try{

             
byte buf[]=new byte[256];         //server receive!
               sin.read(buf);
               String size1
=new String(buf,1,1);

               
int strSize=Integer.parseInt(size1);  // have how much numbers in next array;
           
//    System.out.println("size2= "+strSize);
           //------------------------------------------------

               String size2
=new String(buf,2,strSize);
               
int mesSize=Integer.parseInt(size2);
          
------------------------------------------------------------
               String str=new String(buf,strSize+2,mesSize-1);
               
if(buf[0]=='0')
               p1.append(str);
               
else
               p2.append(str);

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

         }


         
//  chat=conInner();
    }



 
//field data-------------------------------------------------------------------------------
    public final int PROT = 8000;
    Socket serverSocket;
    ServerSocket server;
    InputStream sin;
    OutputStream sout;
 
// public ChatSocket chat;
   private JScrollPane jsp,jsp2;
   
private JScrollPane jsp3;
   
private JComboBox cob;
   
private JTextArea p1;
   
private JTextArea p2;
   
private JLabel sentTip;
   
private JLabel message;
   
private JButton sent;
   
private JTextArea tfd;
   
public JMenuBar menuBar;
   
private JTabbedPane jdp;
   
private JMenuItem exit;
   
private JMenuItem savePublic,savePrivate;
   
private JMenu file;
   
private JMenuItem add;
   
private JMenu action;
   
private JLabel privated;
   
private JLabel publiced;
   
private JMenuItem delete;
   
private JTextArea private1,public1;

  String str[]
={"To public","limin","hailiang","yaya","liu"};


//actionPerformed --------------------------------------------------------------------------

  
public void actionPerformed(ActionEvent e)
   
{
   String str1
="Exit";
   String strSave1 
="SavePublic";
   String str2
="Add a Friend";
   String str4
="Delete a Friend";
   String str6
="Send";
   String strSave2
="SavePrivate";
   
if(e.getActionCommand().equals(strSave2))
   
{
     FileDialog fileDialog 
= new FileDialog(this,"Save as",FileDialog.SAVE);
  fileDialog.setVisible(
true);
  String selectedFileName 
= fileDialog.getDirectory()+ fileDialog.getFile();
  
if(selectedFileName!=null){
   String s 
= p2.getText();
   
try{
    FileOutputStream fileOut 
= new FileOutputStream(selectedFileName);
    
byte[] b = s.getBytes();
    fileOut.write(b);
    fileOut.close();
     JOptionPane.showMessageDialog(
null," save Chat-File is  succeed!");
      }
catch(IOException ee){ee.printStackTrace();}
  }



   }


   
if(strSave1.equals(e.getActionCommand()))
   
{

    FileDialog fileDialog 
= new FileDialog(this,"Save as",FileDialog.SAVE);
    fileDialog.setVisible(
true);
    String selectedFileName 
= fileDialog.getDirectory()+ fileDialog.getFile();
    
if(selectedFileName!=null){
     String s 
= p1.getText();
     
try{
      FileOutputStream fileOut 
= new FileOutputStream(selectedFileName);
      
byte[] b = s.getBytes();
      fileOut.write(b);
      fileOut.close();
       JOptionPane.showMessageDialog(
null," save Chat-File is  succeed!");
        }
catch(IOException ee){ee.printStackTrace();}
    }

   }




       
/* try{
   FileOutputStream  foPublic=new FileOutputStream("Server-public-Chat-Record.txt",true);
   if(p1.getText()!="")
   {
     byte []Public =p1.getText().getBytes();
     foPublic.write(Public);
     }
     FileOutputStream  foPrivate=new FileOutputStream("Server-private-Chat-Record.txt",true);
    if(p2.getText()!="")
    {
     byte []Private =p2.getText().getBytes();
     foPrivate.write(Private);
    }
    JOptionPane.showMessageDialog(null," save Chat-File is  succeed!");
    foPublic.close();
    foPrivate.close();
   }catch(IOException ee){}
*/

 
//  }
   if(str6.equals(e.getActionCommand()))
   
{
         
/*try{
          String str;      //server send --------------------&^&&&
      if(cob.getSelectedIndex() ==0)
      {
        str='0'+"梁景波说: "+tfd.getText() + ' ';
        p1.append("I say To all :     "+tfd.getText()+' ');

      }else{
       str='1'+"梁景波说: "+tfd.getText() + ' ';
       p2.append("To"+ cob.getItemAt( cob.getSelectedIndex()).toString()+" :    "+tfd.getText()+' ');

     }byte[] buff=str.getBytes();
          tfd.setText(null);
          sout.write(buff);


          }catch(IOException eio){}
*/


     
try{
        String str,strTemp,strSize;
        strTemp
= "梁景波说: "+tfd.getText() +' ';
        
if(cob.getSelectedIndex() ==0)                 // == public
        {
         
int i=10,j=1;
        
while((strTemp.length()+5)/i!=0)
           
{
            i
=i*10;
            j
++;
           }


         System.out.println(
"j= "+j);
         
int nn=strTemp.length()+5;
         str
="0"+j+nn+strTemp;

         p1.append(
"我说 :     "+tfd.getText()+' ');
       }

          
else                                       // == private
           {
             
int i=10,j=1;
          
while((strTemp.length()+5)/i!=0)
          
{
             i
=i*10;
             j
++;
          }



            
int nn=strTemp.length()+5;
            str
="1"+j+nn+ strTemp;
             p2.append(
"对 "+ cob.getItemAt( cob.getSelectedIndex()).toString()+" :    "+tfd.getText()+' ');
           }



// write behavior

            
byte[] buf=str.getBytes();
          tfd.setText(
null);
             sout.write(buf);
            }
catch(IOException ioe){}

   }

   
if(str1.equals(e.getActionCommand()))
   
{
      System.exit(
0);
   }

   
if(str2.equals(e.getActionCommand()))
   
{
      String str3
=JOptionPane.showInputDialog(null,"Enter you who you want to add");
      
if(str3.equals(""))
      
{
      JOptionPane.showMessageDialog(
null,"you fail to add");
      }

      
else{cob.addItem(str3);
      JOptionPane.showMessageDialog(
null,"you succeed in adding");
      }

   }

   
if(str4.equals(e.getActionCommand()))
 
{
    String str5
=JOptionPane.showInputDialog(null,"Enter you who you want to delete");
    
if(str5.equals(""))
    
{
    JOptionPane.showMessageDialog(
null,"you fail to delete!");
    }

    
else{cob.removeItem(str5);
    JOptionPane.showMessageDialog(
null,"you succeed in delete");
    }

 }

   }


//main--------------------------------------------------------------------------------------
    public static void main(String[] args) {

         Server ctd 
= new Server();
    }

}


 

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

 

package  killer;

import  java.beans. * ;
import  javax.swing.event. * ;
import  java.io. * ;
import  java.awt. * ;
import  java.awt.event. * ; /this is a client
import  java.util. * ;
import  javax.swing. * ;
import  java.net. * ;
/**
  * ChatDisplay is a user interface to display chat messages.
  * User can add and remove a chat user, display a message in a ChatDisplay
  * or send a message to the ChatDisplay. If there is an error, nothing happens.
  
*/


public   class  Client   extends  JFrame  implements  ActionListener {
//constructor------------------------------------------------------------------------------

     
public Client() {

super("Client----chat");
this.getContentPane().setLayout(new BorderLayout());
//JMenuBar------------------------------------------------------------------------------------

 menuBar
=new JMenuBar();
 action
=new JMenu("Action");
 file
=new JMenu("File");
 file.setMnemonic(
'f');
 savePublic
=new JMenuItem("SavePublic");
    savePublic.addActionListener(
this);
    savePrivate
=new JMenuItem("SavePrivate");
    savePrivate.addActionListener(
this);

    file.add(savePublic);
    file.add(savePrivate);

 
//JMenu---------------------------------------------------------------------------------------

 exit 
=new JMenuItem("Exit");
 exit.addActionListener(
this);
 file.add(exit);
 add
=new JMenuItem("Add a Friend");
 add.addActionListener(
this);
 delete
=new JMenuItem("Delete a Friend");
 delete.addActionListener(
this);
 action.add(delete);
 action.add(add);
 menuBar.add(file);
 menuBar.add(action);

 
//JTabbedPane JScrollPane--------------------------------------------------------------------
 JPanel Jpanel4;
 Jpanel4
=new JPanel();
 jdp 
=new JTabbedPane(jdp.BOTTOM);
p1
=new JTextArea("this is public chat :"+" ");
p1.setWrapStyleWord(
true);//66666
p1.setPreferredSize(new Dimension(30,40));
jsp2
=new JScrollPane(p1);  //jsp2
jdp.add(jsp2,Jpanel4,0);
jdp.setTitleAt(
0,"public");
p2
=new JTextArea("this is private chat :"+" ");
p2.setPreferredSize(
new Dimension(30,40));
jsp3
=new JScrollPane(p2);  //jsp3
jdp.add(jsp3,Jpanel4,1);
jdp.setTitleAt(
1,"private");
Jpanel4.setLayout(
new BorderLayout());
Jpanel4.add(jdp,BorderLayout.CENTER);

//JcomboBox------------------------------------------------------------------------------

 JPanel panel1;
 panel1
=new JPanel(new BorderLayout());
 sentTip
=new JLabel("Send To");
 panel1.add(sentTip,BorderLayout.WEST);
 cob
=new JComboBox(str);
 panel1.add(cob,BorderLayout.CENTER);

//JTextFiled-----------------------------------------------------------------------------------

 JPanel panel2;
 panel2
=new JPanel(new BorderLayout());
 message
=new JLabel("Message:");
 panel2.add(message,BorderLayout.WEST);
 tfd
=new JTextArea("Hello",1,2);
  jsp
=new JScrollPane(tfd);
 panel2.add(jsp,BorderLayout.CENTER);
 JButton sent 
=new JButton("Send");
 sent.addActionListener(
this);
 panel2.add(sent,BorderLayout.EAST);

 
//Layout All-----------------------------------------------------------------------------------

 JPanel panel3;
 panel3
=new JPanel(new BorderLayout());
 panel3.add(panel1,BorderLayout.NORTH);
 panel3.add(panel2,BorderLayout.SOUTH);
 Jpanel4.add(panel3,BorderLayout.SOUTH);


      
this.setSize(400,400);
      
this.getContentPane().add(Jpanel4,BorderLayout.CENTER);
      
this.setJMenuBar(this.menuBar);/important sentence!!!!
      this.addWindowListener(new WindowAdapter(){
         
public void windowClosing(WindowEvent e){
         System.exit(
0);
         }

        }
);

      
this.setVisible(true);

     
try{
      client 
=new Socket("192.168.1.4",8000);
      in
=client.getInputStream();
      out
=client.getOutputStream();

     }
catch(IOException e){}
     
while(true)                 // client  receive !
  {  try{
  
/*byte[] buf=new byte[256];
  in.read(buf);
  String str=new String(buf,1,255);
  if(buf[0]=='0')
    p1.append(str);
  else
    p2.append(str);
*/

             
byte buf[]=new byte[256];         //server receive!
             in.read(buf);
             String size1
=new String(buf,1,1);

             
int strSize=Integer.parseInt(size1);  // have how much numbers in next array;
           
//  System.out.println("size2= "+strSize);
         //------------------------------------------------

             String size2
=new String(buf,2,strSize);
             
int mesSize=Integer.parseInt(size2);
        
------------------------------------------------------------
             String str=new String(buf,strSize+2,mesSize);
             
if(buf[0]=='0')
             p1.append(str);
             
else
             p2.append(str);

  }
catch (IOException ioe){}
  }

}


//field data-------------------------------------------------------------------------------

   
private JScrollPane jsp,jsp2;
   
private JScrollPane jsp3;
   
private JComboBox cob;
   
private JTextArea p1;
   
private JTextArea p2;
   
private JLabel sentTip;
   
private JLabel message;
   
private JButton sent;
   
private JTextArea tfd;
   
public JMenuBar menuBar;
   
private JTabbedPane jdp;
   
private JMenuItem exit;
   
private JMenuItem savePublic,savePrivate;
   
private JMenu file;
   
private JMenuItem add;
   
private JMenu action;
   
private JLabel privated;
   
private JLabel publiced;
   
private JMenuItem delete;
   
private JTextArea private1,public1;
  String str[]
={"To public","limin","hailiang","yaya","liu"};
  Socket client;
     InputStream in;
     OutputStream out;

//--------------------------------------------------------------------------------
 public void actionPerformed(ActionEvent e)
   
{
   String str1
="Exit";
   String str2
="Add a Friend";
   String str4
="Delete a Friend";
   String str6
="Send";
   String strSave1
="SavePublic";
   String strSave2
="SavePrivate";
   
if(e.getActionCommand().equals(strSave2))
     
{
       FileDialog fileDialog 
= new FileDialog(this,"Save as",FileDialog.SAVE);
    fileDialog.setVisible(
true);
    String selectedFileName 
= fileDialog.getDirectory()+ fileDialog.getFile();
    
if(selectedFileName!=null){
     String s 
= p2.getText();
     
try{
      FileOutputStream fileOut 
= new FileOutputStream(selectedFileName);
      
byte[] b = s.getBytes();
      fileOut.write(b);
      fileOut.close();
       JOptionPane.showMessageDialog(
null," save Chat-File is  succeed!");
        }
catch(IOException ee){ee.printStackTrace();}
    }



     }


     
if(strSave1.equals(e.getActionCommand()))
     
{

      FileDialog fileDialog 
= new FileDialog(this,"Save as",FileDialog.SAVE);
      fileDialog.setVisible(
true);
      String selectedFileName 
= fileDialog.getDirectory()+ fileDialog.getFile();
      
if(selectedFileName!=null){
       String s 
= p1.getText();
       
try{
        FileOutputStream fileOut 
= new FileOutputStream(selectedFileName);
        
byte[] b = s.getBytes();
        fileOut.write(b);
        fileOut.close();
       JOptionPane.showMessageDialog(
null," save Chat-File is  succeed!");
          }
catch(IOException ee){ee.printStackTrace();}
      }

     }



 
//  if(strSave1.equals(e.getActionCommand()))
// {
 /* try{
 FileOutputStream  foPublic=new FileOutputStream("client-public-Chat-Record.txt",true);
 if(p1.getText()!="")
 {
   byte []Public =p1.getText().getBytes();
   foPublic.write(Public);
   }
   FileOutputStream  foPrivate=new FileOutputStream("client-private-Chat-Record.txt",true);
  if(p2.getText()!="")
  {
   byte []Private =p2.getText().getBytes();
   foPrivate.write(Private);
  }
  JOptionPane.showMessageDialog(null," save Chat-File is  succeed!");
  foPublic.close();
  foPrivate.close();
 }catch(IOException ee){}
*/

 
//}
   if(str6.equals(e.getActionCommand()))
   
{
     
try{
     String str,strTemp,strSize;
     strTemp
= "李敏说: "+tfd.getText() +' ';
     
if(cob.getSelectedIndex() ==0)                 // == public
     {
      
int i=10,j=1;
     
while((strTemp.length()+5)/i!=0)
        
{
         i
=i*10;
         j
++;
        }


 
//     System.out.println("j= "+j);
      int nn=strTemp.length()+5;
      str
="0"+j+nn+strTemp;

      p1.append(
"我说 :     "+tfd.getText()+' ');
    }

       
else                                       // == private
        {
          
int i=10,j=1;
       
while((strTemp.length()+5)/i!=0)
       
{
          i
=i*10;
          j
++;
       }



         
int nn=strTemp.length()+5;
         str
="1"+j+nn+ strTemp;
          p2.append(
"对 "+ cob.getItemAt( cob.getSelectedIndex()).toString()+" :    "+tfd.getText()+' ');
        }



// write behavior

         
byte[] buf=str.getBytes();

   
// test

  
//    System.out.println("buf[1]=  "+buf[1]);
   
//   System.out.println("str=  "+str);       //why is 54 -> 6; must be 2;
       tfd.setText(null);
          out.write(buf);
         }
catch(IOException ioe){}

   }

   
if(str1.equals(e.getActionCommand()))
   
{
      System.exit(
0);
   }

   
if(str2.equals(e.getActionCommand()))
   
{
      String str3
=JOptionPane.showInputDialog(null,"Enter you who you want to add");
      
if(str3.equals(""))
      
{
      JOptionPane.showMessageDialog(
null,"you fail to add");
      }

      
else{cob.addItem(str3);
      JOptionPane.showMessageDialog(
null,"you succeed in adding");
      }

   }

   
if(str4.equals(e.getActionCommand()))
 
{
    String str5
=JOptionPane.showInputDialog(null,"Enter you who you want to delete");
    
if(str5.equals(""))
    
{
    JOptionPane.showMessageDialog(
null,"you fail to delete!");
    }

    
else{cob.removeItem(str5);
    JOptionPane.showMessageDialog(
null,"you succeed in delete");
    }

 }

   }



//main--------------------------------------------------------------------------------------
    public static void main(String[] args) {

           Client ctd 
= new Client();

    }

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值