setupdialog.java

来自「一款JAVA开发的类迅雷的多线程下载系统,希望对大家有帮助」· Java 代码 · 共 252 行

JAVA
252
字号
package downfile;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JCheckBox;

public class SetUpDialog extends JDialog implements ActionListener,ItemListener{
	static int cishu = 5;   //连接失败重试次数
	static int TaskNum = 5;  //最大连接任务数
	static int waittime = 5;  //连接失败等待时间
	static boolean xianzhicishu = false;   //是否限制重试次数
	static boolean autorun = true;  //程序启动是否自动运行上次未完成的任务
	static boolean play = true;  //下载完成后是否播放提示音
	static boolean tishi = false; //如果当前目录中已经存在此文件名,是否提示用户修改用户名
	static int kuaishu = 5;  //单个任务的线程数
		
	JButton okbutton = new JButton("确定");
	JButton cancelbutton = new JButton("取消");
	
	JCheckBox check1 = new JCheckBox("程序启动后自动开始未完成的任务");
	JCheckBox check2 = new JCheckBox("任务完成后播放提示音");
	
	JLabel lb2 = new JLabel("最多同时建立的任务数:");
	JComboBox list2 = new JComboBox();  //最多同时建立的任务数
	
	JLabel lb1 = new JLabel("重试等待时间:");
	JComboBox list1 = new JComboBox();   //超时
	
	JCheckBox check3 = new JCheckBox("不限制重试次数");
	
	JLabel lb3 = new JLabel("重试次数:");
	JComboBox list3 = new JComboBox();
	
	JLabel lb4 = new JLabel("单个任务线程数:");
	JComboBox list4 = new JComboBox();
	
	JCheckBox check4 = new JCheckBox("提示文件名重复");
	
	SetUpDialog(JFrame parent,String title){
		 super(parent,title,true);
		 setSize(500,400);
		 setResizable(false);
		 JPanel panel = new JPanel();
		 panel.setLayout(null);
		 
		 readConfiguration();
		 
		 JTabbedPane tabbedpane = new JTabbedPane(JTabbedPane.TOP);
         tabbedpane.setBounds(new Rectangle(0,0,500,340));
		 
   	     JPanel panel1 = new JPanel();
   	     panel1.setLayout(null);
   	     JPanel panel2 = new JPanel();
   	     panel2.setLayout(null);
   	     JPanel panel3 = new JPanel();
   	     panel3.setLayout(null);
   	     JPanel panel4 = new JPanel();
   	     panel4.setLayout(null);
   	     JPanel panel5 = new JPanel();
   	     panel5.setLayout(null);
   	  	      	     
   	     
   	     tabbedpane.addTab("常规",panel1);
   	     tabbedpane.addTab("图形/日志",panel2);
   	     tabbedpane.addTab("连接",panel3);
   	     tabbedpane.addTab("监视",panel4);
   	     tabbedpane.addTab("高级",panel5);
   	     
   	     Init();
   	     
   	     check1.setBounds(new Rectangle(50,50,250,25));
   	     panel1.add(check1);
   	     check2.setBounds(new Rectangle(50,80,200,25));
   	     panel1.add(check2);
   
   	     
   	     lb1.setBounds(new Rectangle(50,50,200,25));
   	     panel3.add(lb1);
   	     list1.setBounds(new Rectangle(255,50,50,25));
   	     panel3.add(list1);
   	     panel3.add(lb2);
   	     lb2.setBounds(new Rectangle(50,80,200,25));
   	     panel3.add(list2);
   	     list2.setBounds(new Rectangle(255,80,50,25));
   	     panel3.add(check3);
   	     check3.setBounds(new Rectangle(80,110,150,25));
   	     panel3.add(check4);
   	     check4.setBounds(new Rectangle(80,200,150,25));
   	     panel3.add(lb3);
   	     lb3.setBounds(new Rectangle(80,140,150,25));
   	     panel3.add(list3);
   	     list3.setBounds(new Rectangle(255,140,50,25));
   	     panel3.add(lb4);
   	     lb4.setBounds(new Rectangle(50,170,200,25));
   	     panel3.add(list4);
   	     list4.setBounds(new Rectangle(255,170,50,25));
   	     panel.add(tabbedpane);
   	     
   	     okbutton.setBounds(new Rectangle(310,340,60,25));
   	     panel.add(okbutton);
   	     cancelbutton.setBounds(new Rectangle(385,340,60,25));
   	     panel.add(cancelbutton);
   	     
   	     okbutton.addActionListener(this);
   	     cancelbutton.addActionListener(this);
   	     check1.addItemListener(this);
   	     check2.addItemListener(this);
   	     check3.addItemListener(this);
   	     check4.addItemListener(this);
   	     setContentPane(panel);
	 }


	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == okbutton){				
			cishu = list3.getSelectedIndex()+1;
			TaskNum = list2.getSelectedIndex()+1;
			waittime = list1.getSelectedIndex()+1;
			kuaishu = list4.getSelectedIndex()+1;			
			writeConfiguration();
			dispose();
		}
		
		if(e.getSource() == cancelbutton){
			dispose();
		}		
	}
	
   public void itemStateChanged(ItemEvent e) {
	    
       if(e.getStateChange() == ItemEvent.SELECTED){
    	   if(e.getSource() == check1)
    		   autorun = true;
    	   if(e.getSource() == check2)
    		   play = true;
    	   if(e.getSource() == check3)
    		   xianzhicishu = true;
    	   if(e.getSource() == check4)
    		   tishi = true;
       }else{
    	   if(e.getSource() == check1)
    		   autorun = false;
    	   if(e.getSource() == check2)
    		   play = false;
    	   if(e.getSource() == check3)
    		   xianzhicishu = false;
    	   if(e.getSource() == check4)
    		   tishi = false;
       }
	}
   
	public void Init(){
		if(autorun)
	    	 check1.setSelected(true);

	     if(play)
	    	 check2.setSelected(true);
	     
	     if(tishi)
	    	 check4.setSelected(true);
	     
  	     for(int i=1;i<101;i++){
  	    	 list1.addItem(i);
  	     }
  	     list1.setSelectedIndex(waittime-1);
  	     
         for(int i=1;i<11;i++){
  	    	 list2.addItem(i);
  	     }
  	     list2.setSelectedIndex(TaskNum-1);
 	     
  	     if(xianzhicishu)
  	    	 check3.setSelected(true);
  	     	 
  	     for(int i=1;i<100;i++){
	    	 list3.addItem(i);
	     }
  	     list3.setSelectedIndex(cishu-1);
  	     
  	     for(int i=1;i<11;i++){
  	    	 list4.addItem(i);
  	     }
  	     list4.setSelectedIndex(kuaishu-1);
	}
	
	public void writeConfiguration(){
		try {
			DataOutputStream 
			 dou1 = new DataOutputStream(
						new BufferedOutputStream(new FileOutputStream("Configuration.ini")));
						
			dou1.write(cishu);
			dou1.write(TaskNum);
			dou1.write(waittime);
			dou1.writeBoolean(xianzhicishu);
			dou1.writeBoolean(autorun);
			dou1.writeBoolean(play);
			dou1.writeBoolean(tishi);
			dou1.write(kuaishu);
			dou1.flush();
			dou1.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void readConfiguration(){
		
		try {
			DataInputStream 
			 dis1 = new DataInputStream(
			 new BufferedInputStream(new FileInputStream("Configuration.ini")));
			
			cishu = dis1.read();
			TaskNum = dis1.read();
			waittime = dis1.read();
			xianzhicishu = dis1.readBoolean();
			autorun = dis1.readBoolean();
			play = dis1.readBoolean();
			tishi = dis1.readBoolean();
			kuaishu = dis1.read();
						
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
}

⌨️ 快捷键说明

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