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=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();
		}  
        
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章