首页 > 代码库 > JAVA版进程管理器

JAVA版进程管理器

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=http://www.mamicode.com/res.replace("/",/"", ";").replace("/"", "").split(";");>

JAVA版进程管理器