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

📄 portpane.java

📁 网络代理服务器的设计与实现 实现对网络的管理
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class portPane extends JPanel{
	private JLabel label;
	private JLabel portLabel;
	private JTextField portText;
	private JButton submit1;
	//private JButton cancel1;
	
	private JLabel path_label;
	private JLabel pathLabel;
	private JTextField pathText;
	private JButton browseButton;
	private JButton openButton;
	private JButton clearButton;
	private JButton submit2;
	private JButton cancel2;
	
	public static int port;
	public static String path;
	
	public portPane(){
		this.setLayout(null);
    	
    	label=new JLabel("管理HTTP代理服务器的端口");
    	label.setBounds(new Rectangle(10,20,200,20));
    	portLabel=new JLabel("设置端口:");
    	portLabel.setBounds(new Rectangle(30,45,60,20));
    	portText=new JTextField(50);
    	portText.setBounds(new Rectangle(87,45,50,20));
    	submit1=new JButton("确定");
    	submit1.setBounds(new Rectangle(144,45,60,20));
    	//cancel1=new JButton("返回");
    	//cancel1.setBounds(new Rectangle(206,45,60,20));
    	
    	JSeparator s=new JSeparator(JSeparator.HORIZONTAL);
    	s.setBounds(new Rectangle(0,70,500,2));
    	
    	path_label=new JLabel("管理日志文件");
    	path_label.setBounds(new Rectangle(10,80,200,20));
    	pathLabel=new JLabel("日志文件保存至文件夹:");
    	pathLabel.setBounds(new Rectangle(30,105,132,20));
    	pathText=new JTextField(200);
    	pathText.setBounds(new Rectangle(162,105,200,20));
    	browseButton=new JButton("浏览");
    	browseButton.setBounds(new Rectangle(364,105,60,20));
    	openButton=new JButton("打开日志");
    	openButton.setBounds(new Rectangle(87,140,80,20));
    	clearButton=new JButton("清空日志");
    	clearButton.setBounds(new Rectangle(169,140,80,20));
    	submit2=new JButton("确定");
    	submit2.setBounds(new Rectangle(251,140,60,20));
    	cancel2=new JButton("返回");
    	cancel2.setBounds(new Rectangle(313,140,60,20));
    	
    	port=Integer.parseInt(portGet());
    	portText.setText(String.valueOf(port));
    	path=logPathGet();
    	pathText.setText(path);    	
    	
    	this.add(label);
    	this.add(portLabel);
    	this.add(portText);
    	this.add(submit1);
    	//this.add(cancel1);
    	this.add(s);
    	this.add(path_label);
    	this.add(pathLabel);
    	this.add(pathText);
    	this.add(browseButton);
    	this.add(openButton);
    	this.add(clearButton);
    	this.add(submit2);
    	this.add(cancel2);
    	
    	submit1.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			portWrite();
    		}
    	});
    	/*
    	cancel1.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			eventFrame.jSplitPane1.remove(eventFrame.jSplitPane1.getRightComponent());
		        if(toolBarPanel.state()){
		        	eventFrame.jSplitPane1.add(eventFrame.welcomePanel1,JSplitPane.RIGHT);
		    	}else{
		    		eventFrame.jSplitPane1.add(eventFrame.disPlay,JSplitPane.RIGHT);
		    	}
    		}
    	}); 
    	*/
    	browseButton.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e){
	    		openFolder();
	    	}
	    });
    	
    	openButton.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e){
	    		openLog();
	    	}
	    });
    	
    	clearButton.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e){
	    		clearLog(path);
	    	}
	    });
    	
    	submit2.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e){
	    		logPathWrite();
	    	}
	    });
    	
    	cancel2.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e){
	    		eventFrame.jSplitPane1.remove(eventFrame.jSplitPane1.getRightComponent());
		        if(toolBarPanel.state()){
		        	eventFrame.jSplitPane1.add(eventFrame.welcomePanel1,JSplitPane.RIGHT);
		    	}else{
		    		eventFrame.jSplitPane1.add(eventFrame.disPlay,JSplitPane.RIGHT);
		    	}
	    	}
	    });
	}
	
//	返回端口
	private static String portGet(){
		String str="";
		try{
			BufferedReader in=new BufferedReader(new FileReader("web\\port.txt"));
			str=in.readLine();
		}catch(Exception e){}
		
		if(str.equals("")) 
			str="808";
		return str;
	}
	
//	重新设置端口
	private void portWrite(){
		String str=portText.getText();
		    try{
			    BufferedWriter out=new BufferedWriter(new FileWriter("web\\port.txt",false));
			    out.write(str);
			    out.newLine();
			    out.flush();
			    out.close();
			    port=Integer.parseInt(str);
			    JOptionPane.showMessageDialog(null,"设置成功","消息",JOptionPane.INFORMATION_MESSAGE);
		    }catch(IOException e){
		    	JOptionPane.showMessageDialog(null,"写文件出错!","错误",JOptionPane.ERROR_MESSAGE);
		    }
	}
	
	public static int getPort(){
		return port;
	}
    
//  打开文件夹对话框
	private void openFolder(){
		JFileChooser folderchoose=new JFileChooser();
		folderchoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result=folderchoose.showOpenDialog(null);
		if(result==JFileChooser.CANCEL_OPTION)
			return;		
		File folderName=folderchoose.getSelectedFile();		
		String str=folderName.getPath();
		pathText.setText(str);		
	}
	
    //	返回日志的路径
	private static String logPathGet(){
		String str=null;
		try{
			BufferedReader in=new BufferedReader(new FileReader("web\\logpath.txt"));
			str=in.readLine();
		}catch(Exception e){}
		
		if(str==null) {
			str="D:\\workspace\\ProxyServer\\Log";
		}
		return str;
	}
	
//	重新设置缓存路径
	private void logPathWrite(){
		String str=pathText.getText();
		    try{
			    BufferedWriter out=new BufferedWriter(new FileWriter("web\\logpath.txt",false));
			    out.write(str);
			    out.newLine();
			    out.flush();
			    out.close();
			    path=str;
			    JOptionPane.showMessageDialog(null,"设置成功","消息",JOptionPane.INFORMATION_MESSAGE);
		    }catch(IOException e){
		    	JOptionPane.showMessageDialog(null,"写文件出错!","错误",JOptionPane.ERROR_MESSAGE);
		    }
	}
	
	public static String getLogPath(){
		return path;
	}
	
	private void openLog(){
		String fileName="log";		
		Calendar  date=new GregorianCalendar();  
	    int year=date.get(Calendar.YEAR);
	    int month=date.get(Calendar.MONTH)+1;		    
	    int data=date.get(Calendar.DAY_OF_MONTH);
	    
	    fileName+=year;
	    fileName+=(month<10?"0":"")+month;
	    fileName+=(data<10?"0":"")+data;
	    fileName+=".txt";
	    
	    try{
			//Runtime.getRuntime().exec("notepad "+"d:/test2.txt");
			Runtime.getRuntime().exec("cmd /c start "+path+"\\"+fileName);
		}catch(Exception e){
			JOptionPane.showMessageDialog(null,"找不到文件","错误",JOptionPane.ERROR_MESSAGE);
		}		
	}
	
	private void clearLog(String iPath){ 
		try { 
			File theFile = new File(iPath); 
			File allFiles[] = theFile.listFiles(); 
	   
			for (int i = 0; i < allFiles.length; i++) { 
				if(allFiles[i].isDirectory()) { 
					clearLog(allFiles[i].toString()); 
					allFiles[i].delete(); 
				} 
				else{ 
					allFiles[i].delete(); 
				} 
			} 
			
			JOptionPane.showMessageDialog(this,"日志删除完毕!","消息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NullPointerException e){ 
			JOptionPane.showMessageDialog(this,"清除错误!","错误",JOptionPane.ERROR_MESSAGE);
		} 
	} 
}

⌨️ 快捷键说明

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