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

📄 downloadgui.java

📁 Java写的下载文件程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public void popupMenuWillBecomeVisible(PopupMenuEvent pme)
  {
    popup.add(addMenuItem);
    popup.add(deleteMenuItem);
    popup.add(startMenuItem);
    popup.add(stopMenuItem);
    popup.add(selectallMenuItem);
    popup.add(deselectallMenuItem);
  }
  public void popupMenuWillBecomeInvisible(PopupMenuEvent pme)
  {
    fileMenu.add(addMenuItem,0);
    fileMenu.add(deleteMenuItem,1);
    fileMenu.add(startMenuItem,2);
    fileMenu.add(stopMenuItem,3);
    fileMenu.add(selectallMenuItem,5);
    fileMenu.add(deselectallMenuItem,6);
  }
  public void popupMenuCanceled(PopupMenuEvent pme){}

  //实现 WindowListener接口(针对主框架窗口)
  public void windowClosing(WindowEvent e)
  {
    if(closeFunc()==1)
      new DownloadGUI();
  }
  public void windowOpened(WindowEvent e){}
  public void windowClosed(WindowEvent e){}
  public void windowActivated(WindowEvent e){}
  public void windowDeactivated(WindowEvent e){}
  public void windowIconified(WindowEvent e){}
  public void windowDeiconified(WindowEvent e){}

  //实现 ChangeListener接口(针对进度条)
  public void stateChanged(ChangeEvent e)
  {
   int row=dataTable.getSelectedRow();
   if ((row>-1) && (progressbar.getValue()==100))
   {
    JOptionPane.showMessageDialog(mainFrame,"文件下载完毕!\n"+
       dataTable.getValueAt(row,1)+"\n"+dataTable.getValueAt(row,2),
       "通知",JOptionPane.INFORMATION_MESSAGE);
    filedownVector.removeElementAt(row);
    defaultModel.removeRow(row);
    dataTable.revalidate();
    if (dataTable.getRowCount()>0)
      dataTable.setRowSelectionInterval(0,0);//默认选中第一行
   }
  } 

  /*以下部分是私有成员函数(方法)*/

  //添加新任务
  private void addFunc()
  {
   setting.setMode(1,null);
   //while (setting.getDataVector()==null)
   //  Thread.sleep(100);
   Vector tempVector=new Vector();
   tempVector=setting.getDataVector();
   if (tempVector!=null)
   {
    try
    {
     FileDown fd=new FileDown(tempVector);
     defaultModel.addRow(tempVector);
     filedownVector.addElement(fd);
     dataTable.revalidate();
     fd.start();
     int row=dataTable.getRowCount()-1;
     dataTable.setValueAt(fd.getStatus(),row,0);
     dataTable.setValueAt(String.valueOf(fd.getFileLength()),row,3);
    }
    catch(IOException e){}
   }        
  }//结束私有成员函数addFunc()

  //删除表格中的一行数据并清除与之相对应的下载线程
  private void deleteFunc()
  {
   String message="确实要删除当前行吗? 这将中断正在下载的文件!";
   int result=JOptionPane.showConfirmDialog(mainFrame,message,"请确认"
          ,JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
    if (result==JOptionPane.YES_OPTION)
     {
       int row = dataTable.getSelectedRow();
       if(row >= 0)
       { System.out.println(row);
         FileDown fd=(FileDown)filedownVector.elementAt(row);
         filedownVector.removeElementAt(row);
         defaultModel.removeRow(row);
         dataTable.revalidate();
         fd.downStop();
       }
     }
  }//结束私有成员函数deleteFunc()  

  //重新启动一个下载线程
  private void startFunc()
  {
   int row = dataTable.getSelectedRow();
   if((row>=0)&&
     (dataTable.getValueAt(row,0).toString().equals("停止")))
   try
   {
    Vector temp=(Vector)defaultModel.getDataVector().elementAt(row);
    FileDown fd=new FileDown(temp);
    fd.start();
    filedownVector.setElementAt(fd,row);
    dataTable.setValueAt(fd.getStatus(),row,0);
    dataTable.setValueAt(String.valueOf(fd.getFileLength()),row,3);
   }
   catch(IOException e)
   { e.printStackTrace(); System.out.println("err in start"); }
  }//结束私有成员函数startFunc()

  //停止一个正在运行的下载线程
  private void stopFunc()
  {
   int row = dataTable.getSelectedRow();
   if(row >= 0)
   {
    try
    {
     FileDown x=((FileDown)(filedownVector.elementAt(row)));
     x.downStop();
     dataTable.setValueAt(x.getStatus(),row,0);
    }
    catch(Exception e)
    { e.printStackTrace(); System.out.println("err in stop");}
   }
  }//结束私有成员函数stopFunc()

  //关闭主窗口框架,结束整个程序
  private int closeFunc()
  {
   try  {  saveData(); }
   catch (Exception ee)
   {
    JOptionPane.showMessageDialog(null,"写用户数据文件失败,当前的"+
      "用户数据将不能被保存.","错误...",JOptionPane.ERROR_MESSAGE);
    System.out.println("sysem error");
   }
   int result=JOptionPane.showConfirmDialog(null,"该操作将停止当前"+
       "所有正在运行的任务并退出程序,要继续吗?","确认操作",
        JOptionPane.YES_NO_OPTION);
   if (result==JOptionPane.YES_OPTION)
   {
    filedownVector=null;
    System.exit(0);
    return 0;
   }
   else return 1;
  }//结束私有成员函数closeFunc()

  //全部选中表格中的各行数据
  private void selectallFunc()
  {
    dataTable.selectAll();
  }  

  //取消表格中被选中的行
  private void deselectallFunc()
  {
    dataTable.clearSelection();
  }  

  //设置程序的外观为java模式
  private void javaFunc()
  {
   try
   {
  UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    SwingUtilities.updateComponentTreeUI(mainFrame);
    SwingUtilities.updateComponentTreeUI(setting);
   }
   catch (Exception e1)
   {
    System.out.println("Look and Feel Exception");
    JOptionPane.showMessageDialog(null,"重新设置 Look and Feel"+
              "时发生错误.","错误...",JOptionPane.ERROR_MESSAGE);
   }
  }  

  //设置程序的外观为windows模式
  private void windowFunc()
  {
   try
   {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows."+
                                     "WindowsLookAndFeel");
    SwingUtilities.updateComponentTreeUI(mainFrame);
    SwingUtilities.updateComponentTreeUI(setting);
   }
   catch (Exception e1)
   {
    System.out.println("Look and Feel Exception");
    JOptionPane.showMessageDialog(null,"重新设置 Look and Feel"+
            "时发生错误.","错误...",JOptionPane.ERROR_MESSAGE);
   }
  }  

  //设置程序的外观为mocintosh模式
  private void macintoshFunc()
  {
   try
   {
    UIManager.setLookAndFeel("com.sun.java.swing."+
                      "plaf.motif.MotifLookAndFeel");
    SwingUtilities.updateComponentTreeUI(mainFrame);
    SwingUtilities.updateComponentTreeUI(setting);
   }
   catch (Exception e1)
   {
    System.out.println("Look and Feel Exception");
    JOptionPane.showMessageDialog(null,"重新设置 Look and Feel"+
             "时发生错误.","错误...",JOptionPane.ERROR_MESSAGE);
   }
  }  

  //响应帮助菜单中的"帮助主题"菜单项
  private void contentFunc()
  {
    JOptionPane.showMessageDialog(null,"由于时间紧迫,帮助主题还未设"+
    "计,请等待以后版本","帮助主题",JOptionPane.INFORMATION_MESSAGE);
  }  

  //响应帮助菜单中的"关于"菜单项
  private void aboutFunc()
  {
    JOptionPane.showMessageDialog(null,"无限下载:一个网络文件下载的"+
    "有力工具,类似网络蚂蚁,由纯Java代码编写.\n版权所有:吉林大学计算"+
    "学院9841班 潘轲","关于...",JOptionPane.INFORMATION_MESSAGE);
  }  

  //保存程序中未下载完的文件信息到用户数据文件中去
  private void saveData()
  {
    try
    {
      Vector temp1=new Vector();
      temp1=defaultModel.getDataVector();
      ObjectOutputStream out;
      out=new ObjectOutputStream(new FileOutputStream("user.dat"));
      out.writeObject(temp1);
      out.close();
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null,"写用户数据文件失败,当"+
      "前的用户数据无法保存.","错误...",JOptionPane.ERROR_MESSAGE);
      System.out.println("err in write user.dat");
    }
  }//结束私有成员函数saveData()

  //读取用户数据文件,得到上次未下载完的文件信息,以便继续下载
  private Vector getData()
  {
    Vector temp2=new Vector();
    try
    {
      ObjectInputStream in;
      in=new ObjectInputStream(new FileInputStream("user.dat"));
      temp2=(Vector)in.readObject();
      in.close();
    }
    catch (Exception e)
    {
     JOptionPane.showMessageDialog(null,"读用户数据文件失败,可能"+
       "是第一次运行本程序,或者用户数据文件被意外删除\n程序将自动建"+
       "立数据文件user.dat.","错误...",JOptionPane.ERROR_MESSAGE);
     System.out.println("err in read user.dat");
    }
    return temp2;
  }//结束私有成员函数getData()

  //检测是否需要显示弹出式菜单
  private void checkPopup(MouseEvent me)
  {
    if(me.isPopupTrigger())
      popup.show(me.getComponent(), me.getX(), me.getY());
  } 


  //程序的主线程
  public static void main(String args[])
  {
    new DownloadGUI();
  }

  //自定义的TableModel,继承了DefaultTableModel类
  class UserModel extends DefaultTableModel
  {
    public UserModel(Vector data, Vector columnNames)
    {
      super(data, columnNames);
    }

    //设置表格中的数据为不能修改
    public boolean isCellEditable(int row, int column)
    {
      return false;
    }
  }//结束类UserModel
 
}//结束类DownloadGUI

⌨️ 快捷键说明

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