⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 broadcast.java

📁 java写的局域网广播系统.本软件包括2个部分:局域网广播系统——服务端
💻 JAVA
字号:
//
//
//***************************** 局域网广播系统--服务端 *******************************//
//
//******************************** java program **************************************//
//
//********************************   2004年11月  *************************************//
//
//********         作者:happy_zh  and  other partners     ***************************//

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;
import java.applet.*;

class BroadCastWord extends JFrame implements ActionListener
{   
   int port;                             //定义端口                   
   InetAddress group=null;                          
   MulticastSocket socket=null;                 
   Timer time=null;                      //定义一个Timer的对象           
   FileDialog open=null;                 //用于选择文件的对话框                         
   Button 开始广播,停止广播;
   File file=null,helpfile;              //在textarea中显示的文件,用于显示已播放的内容和帮助文档                    
   String FileDir=null,fileName=null;    //“显示已播放的内容”的属性
   FileReader in=null;                   //文件流           
   BufferedReader bufferIn=null;         //输入输出流
   int   token=0;                                   
   TextArea 显示正在播放内容,显示已播放的内容,帮助文档;  
   Color mycolor;                        //用于设置背景颜色

   JFrame aboutf;                        //"帮助"菜单的"关于我"窗口  
   Panel p;                              //用于"帮助"菜单的"关于我"中的控件
   Label l1,l2,l3,l4;                    //用于"帮助"菜单的"关于我"中的控件
  
   JFrame helpf;                         //"帮助"菜单的"帮助主题"窗口 
   TextArea helpt;                       //用于"帮助"菜单的"帮助主题"中的控件
   FileReader input;                     //用于"帮助主题"中的读文件流
   String HelpContents="\n";             //声明记录所读文件流中的string的变量
 
   MenuBar mb;                           //在主窗口上的一个menubar
   Menu 文件;                            //菜单的选项
   Menu 编辑; 
   Menu 帮助;
   MenuItem select;                      //具体菜单的选项
   MenuItem getout;
   MenuItem copys;
   MenuItem choses;
   MenuItem pastes;
   MenuItem helps;
   MenuItem abouts;
   String ss;                            //用于操作“显示已播放的内容”(textarea)的“粘贴”和“复制”功能
   String contents="";                   //用于操作“显示已播放的内容”(textarea)的“粘贴”功能

 
   public void helppro(){                //“帮助”菜单的“帮助主题”的实现
        try{            
                Process pro=Runtime.getRuntime().exec("cmd  /c BCServerhelp.HTM");
		pro.waitFor();
		}catch(Exception e){
                JOptionPane.showMessageDialog(null,"有异常,请务必检查以排除异常。\n建议:请确定帮助文件存在和路径正确");   
                                                                                           //弹出infromation框提示有异常
		}
        }

   public void aboutpro(){                 //“帮助”菜单的“关于我”的实现

      helpf=new JFrame("关于我");
      helpf.setBounds(250,200,450,350);   
      helpf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);  //使jframe具有关闭功能     
      helpt=new TextArea(50,50);
       
      try {                                                           //从当前目录读取“BCSeverabout.txt”文件的内容
  		input=new FileReader("BCServerabout.txt");              
                BufferedReader br = new BufferedReader(input);      
                String s=br.readLine();
                while(s!=null)
		{  
		   HelpContents+=s;	   //用HelpContents把每次读到的一行行字符串连成原文的多行内容
                   HelpContents+="\n";	   		
		   s=br.readLine();		  
		}
		br.close();		
	  }catch(IOException ex){
 		System.out.println(ex);    //在dos里输出异常的内容
                JOptionPane.showMessageDialog(null,"有异常,请务必检查以排除异常。\n建议:检查要播放的文件。");   
                                                                                        //弹出infromation框提示有异常
      }
                helpt.setText( (String) HelpContents);                //显示的HelpContents即原文档的内容
      		helpt.setBackground(mycolor);                         //设置背景色
      		helpf.getContentPane().add(helpt);                    //jframe上add一个textarea
      		helpf.setVisible(true);      
   }
  
  public void timepro(){                 //选择文件和读文件的timer控制   
       String s;
        try
            {  
              if(token==-1)
                 {
                   file=new File(FileDir,fileName);    
                   in=new FileReader(file);                      
                   bufferIn=new BufferedReader(in);
                  }
             
              s=bufferIn.readLine();

              if(s!=null)
                {
                  token=0;                              //如果s不空,就把token置0;   
                  显示正在播放内容.setText("广播的文件:\n\t"+fileName+"\n\n文件的路径:\n\t"+FileDir+"\n\n正在广播内容:\n"+s);
                  显示已播放的内容.append(s+"\n");
                  停止广播.setEnabled(true);            //“停止广播”按钮可以被按动了
                  contents=contents+s+"\n";             //把已播放的内容存在字符串contents中
                  DatagramPacket packet=null;                               
                  byte data[]=s.getBytes(); 
                  packet=new DatagramPacket(data,data.length,group,port); 
                  socket.send(packet);                                      
                }
              else
                {
                  token=-1;                            //如果s空,就把token置-1
                }
            }
        catch(IOException ee)
            {
                JOptionPane.showMessageDialog(null,"有读文件异常,请务必检查以排除异常。");   //弹出infromation框提示有异常
            } 
  }

  public void selectpro(){               //选择文件和播放文件        
      显示已播放的内容.setText(null);
      open.setVisible(true);
      fileName=open.getFile();
      FileDir=open.getDirectory(); 
      if(fileName!=null)
       { 
         显示正在播放内容.setText("广播的文件:\n\t"+fileName+"\n\n文件的路径:\n\t"+FileDir); 
           						                           //显示文件在服务器上的文件名和路径	               time.stop();                                                
         file=new File(FileDir,fileName);
         try
            {
               file=new File(FileDir,fileName);     //读操作
               in=new FileReader(file);                        
               bufferIn=new BufferedReader(in);
               开始广播.setEnabled(true);           //按钮可用了
            }
         catch(IOException ee)
            {
              JOptionPane.showMessageDialog(null,"有读操作异常,请务必检查以排除异常。");   //弹出infromation框提示有异常
            }
       }
 }  

  public  BroadCastWord()
  {
   super("局域网广播系统--服务端");       //创建一个jframe,名为"局域网广播系统--服务端"
 
   mb=new MenuBar();                      //为他加上一个menubar
   setMenuBar(mb);

   文件=new Menu(" 文 件 ");              //menubar上的选项
   编辑=new Menu(" 编 辑 ");
   帮助=new Menu(" 帮 助 ");
   
   select=new MenuItem(" 选择要广播的文件 ");    //文件菜单的选项和监听器的添加
   getout=new MenuItem(" 退 出 ");               //有" 选择要广播的文件 "和" 退 出 "
   select.addActionListener(this);
   getout.addActionListener(this);               
   文件.add(select);
   文件.add(getout);
   文件.addActionListener(this); 
   
   copys=new MenuItem(" 复 制 ");                //编辑菜单的选项和监听器的添加
   choses=new MenuItem(" 全 选 ");               //有" 复 制 "和" 全 选 "和" 粘 贴 "
   pastes=new MenuItem(" 粘 贴 ");
   copys.addActionListener(this);
   choses.addActionListener(this);
   pastes.addActionListener(this);
   编辑.add(copys);
   编辑.add(choses);
   编辑.add(pastes);
   编辑.addActionListener(this); 

   helps=new MenuItem(" 帮助主题 ");             //帮助菜单的选项和监听器的添加
   helps.addActionListener(this);                //有" 帮助主题 "和"  关于我  "
   abouts=new MenuItem("  关于我  ");
   abouts.addActionListener(this);
   帮助.add(helps);
   帮助.add(abouts);
   帮助.addActionListener(this); 

   mb.add(文件);                                 //menubar上有“文件”和“编辑”和“帮助”
   mb.add(编辑);
   mb.setHelpMenu(帮助);

   开始广播=new Button("   开始广播   ");        //即将放在主窗口上的"   开始广播   "和"   开始广播   "按钮
   开始广播.setEnabled(false);
   停止广播=new Button("   停止广播   ");
   停止广播.setEnabled(false);  
   开始广播.addActionListener(this); 
   停止广播.addActionListener(this); 

   time=new Timer(2000,this);                    //2秒钟执行一次   
   open=new FileDialog(this,"选择要广播的文件",FileDialog.LOAD);    //选择

   显示正在播放内容=new TextArea(7,20);               
   显示正在播放内容.setForeground(Color.blue); 
   
   int r=191,g=211,b=249;
   mycolor=new Color(r,g,b);  
   显示正在播放内容.setBackground(mycolor);      //设置一个颜色作为窗口的主要颜色

   显示已播放的内容=new TextArea(20,20);

   Panel south=new Panel();                      //给按钮安排位置
   south.add(开始广播);
   south.add(停止广播);
   getContentPane().add(south,BorderLayout.SOUTH);
   getContentPane().add(显示正在播放内容,"Center");
   getContentPane().add(显示已播放的内容,"North");
   显示已播放的内容.setBackground(mycolor);
   显示已播放的内容.setEditable(true);
  
   validate();
   setBounds(200,100,600,520);                   //窗口的位置和大小
   setVisible(true);

   try 
      {                          
       port=5000;     				//端口                              
       group=InetAddress.getByName("239.255.0.0");  
       socket=new MulticastSocket(port);        //套接字      
       socket.setTimeToLive(1);                     
       socket.joinGroup(group);                     
      } 
  catch(Exception e)
       {
         System.out.println("Error: "+ e);  
         JOptionPane.showMessageDialog(null,"有异常,请务必检查以排除异常。\n建议:请检查网络状态。");  
										 //弹出infromation框提示有异常        
       }

  addWindowListener(new WindowAdapter()        //事件监听器
                     { public void windowClosing(WindowEvent e)
                       { System.exit(0);
      	               }
      	             });
 }
 public void actionPerformed(ActionEvent e)    //各个控件的工作反应和函数调用
 {
   if(e.getSource()==select)
     {
        selectpro();
      开始广播.setEnabled(true); 
     }
   else if(e.getSource()==开始广播)
     {
        time.start();
        开始广播.setEnabled(false); 
     }
   else if(e.getSource()==time)
     {
       timepro();
     }
   else if(e.getSource()==停止广播)
     {
       time.stop();
       停止广播.setEnabled(false); 
       开始广播.setEnabled(true); 
     }
   else if(e.getSource()==getout)
     {
       System.exit(0);
     }
   else if(e.getSource()==copys)
     {
       ss=显示已播放的内容.getSelectedText();  //得到选中的内容记录在字符串ss中
     }
   else if(e.getSource()==choses)
     {      
       显示已播放的内容.selectAll();
     }
   else if(e.getSource()==pastes)              
     {  
       ss=contents+ss;                             //把选中的内容ss粘贴在已播放的内容contents的后面
						   //并且更新contents的内容
       显示已播放的内容.setText( (String) (ss));   
       contents=ss;                               
     }
   else if(e.getSource()==helps)
     {
       helppro();
     }
   else if(e.getSource()==abouts)
     {
       aboutpro();
     }
 }

}

public class BroadCast{                                       
 public static void main(String[] args)          //主函数
   {
      BroadCastWord broad=new BroadCastWord();
   }

 } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -