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

📄 managerui.java

📁 用java 实现的一个任务管理器
💻 JAVA
字号:
package com.fang.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import com.fang.client.config.FTaskConfig;
import com.fang.client.net.StatusChangeListener;
import com.fang.commom.util.StringParser;

public class ManagerUI {

	private JTable table;
	private JFrame frame;
	
	public ManagerUI() {
		initialize();
	}
	
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setLayout(new BorderLayout());
		frame.setBounds(100, 100, 609, 435);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JMenuBar menuBar = new JMenuBar();
		menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.X_AXIS));
		frame.setJMenuBar(menuBar);

		final JMenu menu = new JMenu();
		menu.setText("Menu Item");
		menuBar.add(menu);

		final JMenuItem menuItem_2 = new JMenuItem();
		menuItem_2.setText("Menu Item");
		menu.add(menuItem_2);

		final JMenuItem menuItem_1 = new JMenuItem();
		menuItem_1.setText("Menu Item");
		menu.add(menuItem_1);

		final JMenuItem menuItem = new JMenuItem();
		menuItem.setText("Menu Item");
		menu.add(menuItem);

		final JMenuItem menuItem_3 = new JMenuItem();
		menuItem_3.setText("Menu Item");
		menu.add(menuItem_3);

		final JMenu menu_1 = new JMenu();
		menu_1.setText("Menu Item");
		menuBar.add(menu_1);

		final JMenu menu_2 = new JMenu();
		menu_2.setText("Menu Item");
		menuBar.add(menu_2);

		final JMenuItem menuItem_4 = new JMenuItem();
		menuItem_4.setText("Menu Item");
		menu_2.add(menuItem_4);

		final JPanel panel = new JPanel();
		final BorderLayout borderLayout = new BorderLayout();
		borderLayout.setHgap(16);
		panel.setLayout(borderLayout);
		frame.getContentPane().add(panel);

		final JPanel panel_1 = new JPanel();
		final FlowLayout flowLayout_2 = new FlowLayout();
		flowLayout_2.setVgap(0);
		flowLayout_2.setHgap(0);
		panel_1.setLayout(flowLayout_2);
		final FlowLayout flowLayout_1 = new FlowLayout();
		flowLayout_1.setVgap(0);
		panel_1.setLayout(flowLayout_1);
		panel_1.setBackground(Color.WHITE);
		panel.add(panel_1, BorderLayout.NORTH);

	
		final JButton button = new JButton();
		button.setText("新建任务");
		button.setActionCommand(Constants.ADD);
		button.addActionListener(new BtnActionListener());
		panel_1.add(button);

		final JButton button_1 = new JButton();
		button_1.setText("取消任务");
		button_1.setActionCommand(Constants.DEL);
		button_1.addActionListener(new BtnActionListener());
		panel_1.add(button_1);

		final JButton button_2 = new JButton();
		button_2.setText("修改任务");
		button_2.setActionCommand(Constants.UPDATE);
		button_2.addActionListener(new BtnActionListener());
		panel_1.add(button_2);

		final JButton button_3 = new JButton();
		
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				//TODO 打开任务监视器。
				try {
					FMonitor frame = new FMonitor();
					frame.setVisible(true);
				} catch (Exception ee) {
					ee.printStackTrace();
				}
				
			}
		});
		button_3.setText("监视器");
		panel_1.add(button_3);

		final JPanel panel_2 = new JPanel();
		panel_2.setLayout(new BorderLayout());
		final FlowLayout flowLayout = new FlowLayout();
		flowLayout.setHgap(0);
		panel_2.setLayout(flowLayout);
		panel.add(panel_2, BorderLayout.CENTER);

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(800, 1000));
		panel_2.add(scrollPane);

		table = new JTable();
		table.setModel(TableModelMirror.newInstance());
		//设置表视口的选择模式为SINGLE_SELECTION
		table.setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(true);
       //		注册选择行时的监听器
		table.getSelectionModel().addListSelectionListener(new RowListener());
		
		table.setPreferredScrollableViewportSize(new Dimension(800, 1000));
		scrollPane.setViewportView(table);
	}
	
	
	public static void main(String[] args) {
		//try {
			//ManagerUI window = new ManagerUI();
			//window.frame.setVisible(true);
		//} catch (Exception e) {
		//	e.printStackTrace();
		//}
	    
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	ManagerUI window = new ManagerUI();
            	window.frame.pack();
            	window.frame.setVisible(true);
            }
        });

		new StatusChangeListener();
	
}
	}

//TODO  任务管理界面表格的数据模型
class TableModelMirror extends AbstractTableModel {
	 /**
	 * 
	 */
	private static TableModelMirror tableModel=null;
	
	private static final long serialVersionUID = 1L;
	private String[] columnNames = {"任务编号",
            "任务名称",
            "命令参数",
            "任务触发时机",
            "任务间隔时间(分)",
            "任务类型"};
	
    private   FTaskConfig config=null;  

    protected TableModelMirror(){
    	
    }
    
    public static  TableModelMirror newInstance(){
    	if(tableModel==null)
    		tableModel=new TableModelMirror();
    	return tableModel;
    }
    
          public int getColumnCount() {
             return columnNames.length;
           }

         public int getRowCount() {
        	 //获取配置层组件的引用
              if(config==null){
            	  config=new FTaskConfig();
              }
            //利用configure来完成对全局唯一任务集数据信息的获取。
        	return  config.getTaskCount();
           }

           public String getColumnName(int col) {
              return columnNames[col];
            }

           public Object getValueAt(int row, int col) {
        	   
        	   if(config==null){
        		     config=new FTaskConfig();
               }
        	   if(row>=getRowCount()){
        		   return null;
        	   }
        	   System.out.println("ddddddddddddddddddddd");
        	   
              if(col==0){
            	  return  config.getTaskByIndex(row).getId();
              }else if(col==1){
            	  return  config.getTaskByIndex(row).getTaskname();
              }else if(col==2){
            	  String [] args=config.getTaskByIndex(row).getCmdParam();
            	  String temp="";
            	  for(int i=0;i<args.length;i++)
            		  temp+=args[i]+"  ";
            	  return temp.trim();
            	  
              }
              else if(col==3){
            	  Date date=new Date(config.getTaskByIndex(row).getStarttime());
            	  
            	  return date.toLocaleString();
            	  
              }
              else if(col==4){
            	  return  (config.getTaskByIndex(row).getInternal())/60;
              }
              else {
            	  if(config.getTaskByIndex(row).getCmdType()==0)
            		  return new String("LUNIX命令");
            	  else 
            	  return  new String("URL访问");
            	  
              }
              
            }

        
           
     public Class getColumnClass(int c) {
     return getValueAt(0, c).getClass();
       }

   
      public boolean isCellEditable(int row, int col) {
       return false;
         }


      public void setValueAt(Object value, int row, int col) {
	
      }

 }



class BtnActionListener implements ActionListener{

	public  static  int rowselected=-1;
	private FTaskConfig config= null;
	
	
	
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		
        //		 Handle each button.
     if (Constants.ADD.equals(cmd)) { //first button clicked
    	 try {
 			AddDialog dialog = new AddDialog();
 			dialog.setVisible(true);
 		} catch (Exception ee) {
 			ee.printStackTrace();
 		}
     	
     } else if (Constants.UPDATE.equals(cmd)) { // second button clicked
         if(rowselected>=0){
        	 try {
        		 if(config==null)
           		   config = new FTaskConfig();
        		 
     			EditDialog dialog = new EditDialog();
     			
     			 dialog.temptask=config.getTaskByTaskID(rowselected);
     			
     			dialog.getTextField().setText(new Integer(dialog.temptask.getId()).toString());
     			dialog.getTextField_1().setText(dialog.temptask.getTaskname());
     			dialog.getTextField_2().setText(StringParser.linkup(dialog.temptask.getCmdParam()));
     		
     			dialog.getTextField_4().setText(new Long(dialog.temptask.getInternal()/60).toString());
     			
     			dialog.setVisible(true);
     		} catch (Exception ee) {
     			ee.printStackTrace();
     		}
         }
     } else if (Constants.DEL.equals(cmd)) { // third button clicked
     	 if(rowselected>=0){
     	     
     	     Object[] options = {"确定", "放弃"};
     	     
              int n = JOptionPane.showOptionDialog(null,
                         "你真是要取消任务吗?",
                         "操作确认",
                         JOptionPane.YES_NO_OPTION,
                         JOptionPane.QUESTION_MESSAGE,
                         null,
                         options,
                         options[0]);
              if (n == JOptionPane.YES_OPTION) {
            	  if(config==null)
            		   config = new FTaskConfig();
            	  //TODO  删除选中的任务
            	  if(config.delTaskByTaskID(rowselected)==1){
            		//TODO 刷新界面
                	TableModelMirror tableModel=TableModelMirror.newInstance();
       				tableModel.fireTableRowsDeleted(rowselected, rowselected);
       				//tableModel.fireTableDataChanged();
            	  }else {
            		  //TODO 提示用户操作失败
            		  JOptionPane.showMessageDialog(null,
            	              "操作失败,请稍候再试!!", "alert", JOptionPane.ERROR_MESSAGE);
            	  };
   			    rowselected=-1;
              } else if (n == JOptionPane.NO_OPTION) {
              } else {}
          }else {
         	 JOptionPane.showMessageDialog(null,
              "请选择要取消的任务!!", "alert", JOptionPane.ERROR_MESSAGE);
          }
     }
		
	}
}

class RowListener implements ListSelectionListener {
	
    private  int oldselected=-1;
    public void valueChanged(ListSelectionEvent event) {
        if (event.getValueIsAdjusting()) {
            return;
        }
        System.out.println("======================================");
        System.out.println(event);
        
        TableModelMirror tableModel=TableModelMirror.newInstance();
        
        //获取选中的任务的编号
        
        int a=event.getFirstIndex();
        int b=event.getLastIndex();
        
        if(a!=oldselected)
        { BtnActionListener.rowselected=((Integer)tableModel.getValueAt(event.getFirstIndex(), 0)).intValue();
         oldselected=a;
        }
         else {BtnActionListener.rowselected=((Integer)tableModel.getValueAt(event.getLastIndex(), 0)).intValue();
         oldselected=b;
        }
        System.out.println("--------------------------------------");
        System.out.println("row"+BtnActionListener.rowselected+"selected!!");
        System.out.println("--------------------------------------");
        System.out.println("row"+BtnActionListener.rowselected+"selected!!");
        
    }
}

⌨️ 快捷键说明

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