JAVA版进程管理器

本文介绍了如何利用JAVA编程实现一个简单的进程管理器。通过ProcessViewer.java类,展示了界面设计和进程信息的展示,利用TaskList获取系统进程,并在JTable中进行显示。

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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值