📄 mainframe.java
字号:
if((mods&InputEvent.BUTTON3_MASK)!=0)
rightMouseMenu.show(this, evt.getX(), evt.getY());
}//GEN-LAST:event_formMouseClicked
private void fileMenuActionPerformed (java.awt.event.ActionEvent evt)//GEN-FIRST:event_fileMenuActionPerformed
{//GEN-HEADEREND:event_fileMenuActionPerformed
// TODO 将在此处添加您的处理代码:
}//GEN-LAST:event_fileMenuActionPerformed
/**
* @param args the command line arguments
*/
public static void main (String args[])
{
if (!isOnly())
{
JOptionPane.showMessageDialog(null, "端口已被占用","错误", JOptionPane.ERROR_MESSAGE);
return;
}
java.awt.EventQueue.invokeLater (new Runnable ()
{
public void run ()
{
trans.Listener l = new trans.Listener();
l.start ();
Thread client = new Thread(new Server(true,new Socket()));
client.start();
m.start ();
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(m);
}
catch(Exception ex){ex.printStackTrace();
}
m.setLocation ( 300,300);
m.setVisible ( true );
}
});
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JMenuItem configItem;
private javax.swing.JLabel countLabel;
private javax.swing.JMenuItem exitItem;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenu helpMenu;
private javax.swing.JTable infoTable;
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JButton listButton;
private java.awt.MenuItem menuItem1;
private java.awt.MenuItem menuItem2;
private java.awt.MenuItem menuItem3;
private java.awt.MenuItem menuItem4;
private javax.swing.JTextArea msgTA;
private javax.swing.JMenuItem openItem;
private java.awt.PopupMenu popupMenu1;
private javax.swing.JButton refreshButton;
private javax.swing.JPopupMenu rightMouseMenu;
private javax.swing.JButton sendButton;
private javax.swing.JMenuItem sendItem;
// 变量声明结束//GEN-END:variables
public static int getIPType(String ip)
{
String []s = ip.split ("\\.");
if ( s.length > 4 ) return 0;
try {
Integer.parseInt (s[0]);
Integer.parseInt (s[1]); //检查前两个段是否为数字
if ( "*".equals (s[2]) && "*".equals (s[3])) return 3; //添加一个B类网
if ( "*".equals (s[3]) ) return 2; //添加一个C类网
for (int i = 0; i < 4 ; i ++)
if ( Integer.parseInt ( s[i] ) > 255 )return 0 ;
}
catch (NumberFormatException e)
{
return 0 ;
}
return 1; //添加一个IP地址
}
public static String getBNet(String ip) //返回一个IP地址的B类网IP段
{
String [] s = ip.split ( "\\.");
if ( getIPType( ip ) > 1 ) return ip;
else
return s[0] + "." + s[1] + "." + s[2] +".*";
}
private boolean findInTable ( String ip )
{
for ( int i = 0 ; i < tableModel.getRowCount () ; i ++)
if ( ip.equals ( (String)tableModel.getValueAt ( i , 2) ) ) return true;
return false;
}
public void addToTable( InetAddress ip ,String userName )
{
if ( findInTable( ip.getHostAddress () ) ) return ;
try
{
tableModel.addRow (new Object [] {ip.getHostName (),userName,ip.getHostAddress ()});
}
catch (Exception e){}
}
public static void detect(char flag ,InetAddress address,String msg)
{
Thread sendThread=new Thread(new BroadCast(flag,address,msg));
sendThread.start();
}
private void start ()
{
try
{
message = "";
BCIP.clear ();
while(m.tableModel.getRowCount () != 0)
m.tableModel.removeRow ( 0 );
m.countLabel.setText ("在线人数: 0");
String hostName = System.getProperty ("user.name");
InetAddress local = InetAddress.getLocalHost ();
nearIP = getBNet( local.getHostAddress () );
message += hostName;
if ( ! "127.0.0.*".equals ( nearIP ) )
detect('S', InetAddress.getByName( nearIP.substring (0 , nearIP.length () - 1) + "255" ), message );
else detect('S', InetAddress.getByName( "127.0.0.1" ), message );
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,this.getClass () + e.toString (),"错误", JOptionPane.DEFAULT_OPTION);
}
File file = new File ( "config.ini" );
try
{
if( !file.exists())
{
JOptionPane.showMessageDialog(null,"读配置文件错误","错误", JOptionPane.ERROR_MESSAGE);
file.createNewFile ();
return;
}
RandomAccessFile fout = new RandomAccessFile(file,"r");
String tempIP;
while( (tempIP = fout.readLine() ) != null )
{
BCIP.add(tempIP);
}
fout.close();
}
catch ( Exception e )
{
JOptionPane.showMessageDialog( null,"读广播地址出错!"+e.toString (),"错误", JOptionPane.ERROR_MESSAGE );
}
//向局域网广播
try{
InetAddress sendAddress;
String strIP;
for(int i = 0; i < BCIP.size() ; i ++ )
{
strIP = ( String ) BCIP.get(i);
int type = getIPType( strIP );
if( type == 1 )
{
sendAddress = InetAddress.getByName(strIP);
detect('S', sendAddress, message);
}
else if( type == 2)
{
strIP = strIP.substring(0, strIP.length()-1);
for (int ip = 2; ip < 255 ;ip ++)
detect('S', InetAddress.getByName( strIP + String.valueOf (ip) ), message );
// detect('S', InetAddress.getByName( strIP + "255" ), message );
}
else if ( type == 3 )
{
strIP = strIP.substring ( 0 , strIP.length () - 3 );
for (int ip = 1; ip < 255 ;ip ++)
for (int j = 2 ; j < 255 ; j++)
detect('S', InetAddress.getByName( strIP + String.valueOf (ip) + "." + String.valueOf (j) ), message );
// detect('S', InetAddress.getByName( strIP + String.valueOf (ip) + ".255" ), message );
}
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog( null, e.getMessage(),"错误", JOptionPane.ERROR_MESSAGE );
}
}
void paintList()
{
if ( m.fileVector.size () == 0)
{
listButton.setVisible ( false );
return ;
}
listButton.setVisible ( true ) ;
listButton.setText ( m.fileVector.size () + "个文件和文件夹");
}
void openFile()
{
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled ( true );
fc.setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES );
if ( fc.showOpenDialog ( this ) == JFileChooser.CANCEL_OPTION ) return;
File []file = fc.getSelectedFiles ();
for (int i = 0 ; i <file.length ; i ++)
m.fileVector.add ( file [i] );
}
public static void append(String path,String msg)
{
try
{
File file = new File(path);
if( !file.exists() )
file.createNewFile();
RandomAccessFile rf = new RandomAccessFile(file,"rw");
rf.seek(rf.length());
msg += "\r\n";
byte [] m = msg.getBytes();
//rf.writeUTF( msg + "\r\n" );
rf.write( m );
rf.close();
}
catch(Exception e){
System.out.println( e.toString() );
}
}
void setCount( String s)
{
countLabel.setText ( s );
}
public void systemTray()
{
try
{
if (java.awt.SystemTray.isSupported())
{// 判断当前平台是否支持系统托盘
java.awt.SystemTray st = java.awt.SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/trans/resourses/ipmsg.gif"));//定义托盘图标的图片
java.awt.TrayIcon ti = new java.awt.TrayIcon( image);
ti.setToolTip ( "文件传输工具 ");
ti.setPopupMenu ( this.popupMenu1);
st.add(ti);
}
}
catch (Exception e)
{
}
}
void send ()
{
String sendMsg = msgTA.getText();
int index[]=infoTable.getSelectedRows ();
Vector sendList = new Vector( m.fileVector );
if(!"".equals(sendMsg))
{
String temps = System.getProperty("user.name");
temps += "|";
temps += sendMsg;
try{
for( int i = 0;i < index.length; i ++)
detect('M', InetAddress.getByName( ( String ) tableModel.getValueAt ( index[i] ,2 ) ) , temps);
msgTA.setText ("");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"发送消息错误: "+e.getMessage(),"错误", JOptionPane.ERROR_MESSAGE);
}
}
if( sendList.size () > 0 )
{
for ( int i = 0 ; i < sendList.size () ; i ++)
{
File file = ( File ) sendList.get ( i );
if(!file.exists())
{
sendList.remove ( file );
JOptionPane.showMessageDialog(null,"文件:"+ file.getName ()+ " 不存在!","错误", JOptionPane.ERROR_MESSAGE);
}
}
}
if ( sendList.size () > 0 )
{
try{
for( int j = 0;j < index.length ; j ++ )
{
InetAddress sendAddress=InetAddress.getByName( ( String ) tableModel.getValueAt ( index[j] ,2 ) );
Thread clientThread=new Thread(new Client( sendList ,sendAddress));
clientThread.setPriority(6);
clientThread.start();
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null , "发送文件错误了: " + e.toString (),"错误", JOptionPane.ERROR_MESSAGE);
}
}
sendingFile.addAll ( sendList );
m.fileVector.clear ();
m.paintList ();
}
public static boolean isOnly() {
try {
ServerSocket sock = new ServerSocket(MainFrame.PORT);
sock.close ();
} catch (BindException e) {
return false;
} catch (Exception e) {
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -