首页 > 编程语言 > 详细

JAVA版进程管理器

时间:2015-01-18 22:46:23      阅读:408      评论:0      收藏:0      [点我收藏+]

ProcessViewer.java 类,负责界面实现

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

public class ProcessViewer extends JFrame{
	private BorderLayout borderlayout = new BorderLayout();
	private FlowLayout flowlayout = new FlowLayout(FlowLayout.RIGHT);
	private JPanel jpl = new JPanel();
	private JPanel jplbutton = new JPanel();
	private JTable jtable;
	private JButton jbutton;
	private JButton jbutton2;
	private JScrollPane jscrollPane;
	
	public ProcessViewer(){
		TaskList tasklist = new TaskList();
		tasklist.init();
		jtable = new JTable(tasklist.result,tasklist.title);
		jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jscrollPane  = new JScrollPane(jtable);
        jbutton = new JButton("结束进程");
        jbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String process = (String) jtable.getValueAt(jtable.getSelectedRow(), 0);
				try {
					Runtime.getRuntime().exec("taskkill /f /im "+process);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				tasklist.init();
				jtable.updateUI();
				jpl.repaint();
				System.out.println(process);
			}	
        });
        jbutton2 = new JButton("刷新进程");
        jbutton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				tasklist.init();
				jtable.updateUI();
				jpl.repaint();
			}	
        });
		
		jpl.setLayout(borderlayout);
		jpl.add(jscrollPane);
		jplbutton.setLayout(flowlayout);
		jplbutton.add(jbutton2);
		jplbutton.add(jbutton);
		
		this.pack();
		this.setTitle("进程管理器");
		this.add(jpl,BorderLayout.CENTER);
		this.add(jplbutton,BorderLayout.SOUTH);
		this.setBounds(400, 200, 600, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new ProcessViewer();

	}

}

TaskList.java 类,负责调用系统进程并生成相应格式

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class TaskList {
	public BufferedReader br=null;
	public String [][] result=new String[100][5];
	public String [] title={"映像名称","PID","会话名","会话#","内存使用"};
	public void init(){
		Process proc;
		try {
			proc = Runtime.getRuntime().exec("tasklist /NH /FO csv");
			br=new BufferedReader(new InputStreamReader(proc.getInputStream())); 
			String res=null;
			int x = 0;
            while((res=br.readLine())!=null){  
                String[] value=res.replace("\",\"", ";").replace("\"", "").split(";");
                if(value.length==5){
                	for(int i = 0;i<5;i++){ 
                        result[x][i] = value[i];
                    }
                }
                x++;
                if(x==100)
                	break;
            }
		} catch (IOException e) {
			e.printStackTrace();
		}  
        
	}
}


JAVA版进程管理器

原文:http://blog.csdn.net/lu_chaoqun/article/details/42842921

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!