/**************************************************************
*
* MibExplorer.java
*
* This application allows you view the MIB tree you loaded,
* get value from,or set value to some equipments in networks
* that are running SNMP agent process.
*
* Author: HEJIE
* Parameter: None
* Returns: None
* Creation date: 1/3/2007
* LastUpdate: None
*
**************************************************************/import com.adventnet.snmp.beans.*;
import com.adventnet.snmp.ui.*;
import com.adventnet.snmp.mibs.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class MibExplorer extends JFrame implements ActionListener
{
Container contentPane=getContentPane();
JMenuBar menub=new JMenuBar();
JMenu files=new JMenu("File");
JMenu opera=new JMenu("Operation");
JMenu help=new JMenu("Help");
JMenuItem save=new JMenuItem("Save Result As...");
JMenuItem exit=new JMenuItem("Exit");
JMenuItem get=new JMenuItem("Get");
JMenuItem getNext=new JMenuItem("GetNext");
JMenuItem set=new JMenuItem("Set");
JMenuItem about=new JMenuItem("About");

JLabel hostL=new JLabel("Host:");
JLabel portL=new JLabel("Port:");
JLabel communityL=new JLabel("Community:");
JLabel writeCommL=new JLabel("Write Community:");
JLabel setValueL=new JLabel("Set Value:");
JLabel oidL=new JLabel("ObjectID:");
JLabel descrL=new JLabel("Descricption:");
JTextField host=new JTextField("127.0.0.1");
JTextField port=new JTextField("161");
JPasswordField community=new JPasswordField("public");
JPasswordField writeComm=new JPasswordField("public");
JTextField setValue=new JTextField();
JTextField oid=new JTextField(".1.3.6.1.2.1.1.1.0");
JTextArea descr=new JTextArea();
JButton getB=new JButton("Get");
JButton getNextB=new JButton("GetNext");
JButton setB=new JButton("set");
JButton getTableB=new JButton("GetTable");
JButton clear=new JButton("Clear");
JTextArea mess=new JTextArea();
MibExplorer()
{
super("--Simple MibExplorer--");
int x,y;
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
x=(size.width-750)/2;
y=(size.height-500)/2;
setSize(750,500);
setLocation(x,y);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setVisible(true);
}

void init()
{
//主界面初始化
files.setMnemonic('F');
opera.setMnemonic('O');
help.setMnemonic('H');
save.setMnemonic('S');
exit.setMnemonic('X');
get.setMnemonic('G');
getNext.setMnemonic('N');
set.setMnemonic('E');
about.setMnemonic('A');
//为控件添加助记符
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
get.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
getNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
set.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
//为控件添加快捷键
files.add(save);
files.addSeparator();
files.add(exit);
opera.add(get);
opera.add(getNext);
opera.addSeparator();
opera.add(set);
help.add(about);
menub.add(files);
menub.add(opera);
menub.add(help);
setJMenuBar(menub);

save.addActionListener(this);
exit.addActionListener(this);
about.addActionListener(this);

mess.setForeground(Color.blue);
mess.setEditable(false);
descr.setEditable(false);
//mess.setLineWrap(true);
//descr.setLineWrap(true);
get.addActionListener(this);
getB.addActionListener(this);
getNext.addActionListener(this);
getNextB.addActionListener(this);
getTableB.addActionListener(this);
set.addActionListener(this);
setB.addActionListener(this);
clear.addActionListener(this);
//添加事件侦听器
descr.setBackground(new Color(220,220,220));

JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,mibTreePane(),componentPane());
getContentPane().add(splitPane);

update(getGraphics());
}

JPanel mibTreePane()
{
//此方法利用Adventnet SNMP API的MibTree等类,装载MIB模型,显示MIB树,
//程序运行后可看到效果
MibOperations mibOps = new MibOperations();
MibTree mibTree;
try 
{
  mibOps.loadMibModules("RFC1213-MIB");
}
catch (Exception ex) 

  System.err.println("Error loading MIBs: " + ex);
}
mibTree=new MibTree(mibOps);
TreeSelectionListener listener = new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent e) {
       Object selected = e.getPath().getLastPathComponent();
       if (selected instanceof NodeData) {
         Object obj = ((NodeData)selected).getUserObject();
     if (obj instanceof MibNode)
          {
           oid.setText(((MibNode)obj).getNumberedOIDString());
          descr.setText(((MibNode)obj).getDescription());
          descr.setCaretPosition(0);
        }
        }
      }
    };
mibTree.getTree().addTreeSelectionListener(listener);
return mibTree;
}

JPanel componentPane()
{
//具体的界面布局设置,主要用到了GridBagLayout网格包布局管理器
JPanel jp=new JPanel();
JPanel jp2=new JPanel();
JScrollPane sp1=new JScrollPane(mess);
JScrollPane sp2=new JScrollPane(descr);

jp2.setLayout(new GridLayout(1,5));
jp2.add(getB);
jp2.add(getNextB);
jp2.add(getTableB);
jp2.add(setB);
jp2.add(clear);

GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
jp.setLayout(gridbag);

constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(2,4,2,4);

gridbag.setConstraints(hostL,constraints);
gridbag.setConstraints(portL,constraints);
gridbag.setConstraints(communityL,constraints);
gridbag.setConstraints(writeCommL,constraints);
gridbag.setConstraints(setValueL,constraints);
gridbag.setConstraints(oidL,constraints);
gridbag.setConstraints(descrL,constraints);

constraints.gridwidth=2;
constraints.weightx=1;

gridbag.setConstraints(host,constraints);
gridbag.setConstraints(community,constraints);
gridbag.setConstraints(setValue,constraints);

constraints.gridwidth=GridBagConstraints.REMAINDER;

gridbag.setConstraints(port,constraints);
gridbag.setConstraints(writeComm,constraints);
gridbag.setConstraints(oid,constraints);
gridbag.setConstraints(jp2,constraints);


constraints.gridheight=GridBagConstraints.RELATIVE;
constraints.weighty=0.75;
gridbag.setConstraints(sp1,constraints);


constraints.gridheight=GridBagConstraints.REMAINDER;
constraints.weighty=0.25;
gridbag.setConstraints(sp2,constraints);

jp.add(hostL);
jp.add(host);
jp.add(portL);
jp.add(port);
jp.add(communityL);
jp.add(community);
jp.add(writeCommL);
jp.add(writeComm);
jp.add(setValueL);
jp.add(setValue);
jp.add(oidL);
jp.add(oid);
jp.add(jp2);
jp.add(sp1);
jp.add(descrL);
jp.add(sp2);

return(jp);
}

解决方案 »

  1.   

    JFrame tableFrame(JPanel panel,String title)
    {
    //此方法将传递的SnmpTablePanel显示在新建的JFrame里,并返回
    JFrame frame=new JFrame(title);
    int x,y;
    Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
    x=(size.width-700)/2;
    y=(size.height-400)/2;
    frame.setSize(700,400);
    frame.setLocation(x,y);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(panel);
        return(frame);
    }

    public void actionPerformed(ActionEvent e)
    {
    //事件处理部分,以下为核心代码
    Object source=e.getSource();

    if(source==getB||source==getNextB||source==getTableB||source==setB||source==get||source==getNext||source==set||source==setB)
    {
    String hostStr=host.getText();
    String portStr=port.getText();
    String oidStr=oid.getText();
    String comStr=String.valueOf(community.getPassword());
    String result=null;
    if(hostStr.equals("")||oidStr.equals("")||comStr.equals("")||portStr.equals(""))
    {
    JOptionPane.showMessageDialog(this,"Please input all of the blank !","Error!",JOptionPane.WARNING_MESSAGE);
    }
    else
    {
    if(source==get||source==getNext||source==getB||source==getNextB)
    {
    //实现get及getNext操作
    SnmpTarget target = new SnmpTarget();
    //新建SnmpTarget对象(原类参见AdventNet SNMP API)
    target.setTargetHost(hostStr);
    target.setTargetPort(Integer.valueOf(portStr).intValue());
    target.setObjectID(oidStr);
    target.setCommunity(comStr);
    //设置target的各项参数
    if(source==get||source==getB)
    result=target.snmpGet();
    //执行SNMP协议中的Get操作
    else
    {
    result=target.snmpGetNext();
    //执行SNMP协议中的GetNext操作
    oid.setText(target.getObjectID());
    }
    if(result==null)
    {
    mess.append(target.getErrorString()+"\n");
    //显示错误或操作失败的信息
    }
    else
    {
    mess.append("HOST:"+target.getTargetHost()+"\tOID:"+target.getSnmpOID()+"\n");
    mess.append("Response received. Value:\n" + result+"\n");
    //展示操作得到的结果
    result=null;
    }
    }

    else if(source==getTableB)
    {
    //实现GetTable取表操作
    SnmpTablePanel snmptablepanel=new SnmpTablePanel();
    //新建SnmpTablePanel对象,用于呈现取表的结果(原类请参见Advent SNMP API)
    snmptablepanel.setTargetHost(hostStr);
    snmptablepanel.setTargetPort(Integer.valueOf(portStr).intValue());
         snmptablepanel.setCommunity(comStr);
         //设置snmptablepanel取表所需要的各项参数
          try 
          {
    snmptablepanel.loadMibs("RFC1213-MIB");
    //装载MIB模型,源码文件夹下有"RFC1213-MIB"文件
    snmptablepanel.setTableOID(oidStr);
    //设置对象ID,必须在loadMib之后
    tableFrame(snmptablepanel,oidStr).setVisible(true);
    //调用tableFrame(),将返回的JFrame显示
    }
    catch (Exception ex)
    {
           JOptionPane.showMessageDialog(this,ex,"Error!",JOptionPane.ERROR_MESSAGE);
           //提示出错信息,如错误的OID
          }
    }

    else if(source==setB||source==set)
    {
    //Set操作跟前面的Get及GetNext的初始化部分基本相同
    String writeCommStr=String.valueOf(writeComm.getPassword());
    //Write Community用于设置操作的身份验证
    String valueStr=setValue.getText();
    if(writeCommStr.equals("")||valueStr.equals(""))
    {
    JOptionPane.showMessageDialog(this,"No WriteCommunity Or Set Value !","Error!",JOptionPane.WARNING_MESSAGE);
    }
    else
    {
    SnmpTarget target = new SnmpTarget();
    target.setTargetHost(hostStr);
    target.setTargetPort(Integer.valueOf(portStr).intValue());
    target.setObjectID(oidStr);
    target.setCommunity(comStr);
    target.setWriteCommunity(writeCommStr);
    try
    {
    result=target.snmpSet(valueStr);
    //即执行SNMP协议中的Set操作,返回操作结果
    }
    catch(Exception ex)
    {
    //JOptionPane.showMessageDialog(this,ex,"Error!",JOptionPane.WARNING_MESSAGE);
    }
    if(result==null)
    {
    mess.append(target.getErrorString()+"\n");
     //显示出错信息于结果区
    }
    else
    {
    mess.append("HOST:"+target.getTargetHost()+"\tOID:"+target.getSnmpOID()+"\n");
    mess.append("Set Success!. \n" + result+"\n");
    //展示设置成功的相关信息于结果区
    result=null;
    }
    }
    }
    }
    }
    else if(source==clear)
    mess.setText("");
    //清空结果区
    else if(source==save)
    {
    //保存取得的结果到外存
    String savePath=new String();
    FileDialog saveFile=new FileDialog(this,"Save As...",FileDialog.SAVE);
    saveFile.setVisible(true);
    savePath=saveFile.getDirectory()+saveFile.getFile();
    if(savePath!=null)
    {
    try
    {
    FileOutputStream fos=new FileOutputStream(savePath);
    fos.write(mess.getText().getBytes());
    fos.close();
    }
    catch(Exception ex){};
    }
    }
    else if(source==exit)
    System.exit(0);
    else if(source==about)
    {
    String info="--Simple MibExplorer--\nAuthor: He Jie\nVersion: 0.01\nEmail: [email protected]\nTHANKS FOR YOUR USING!";
    JOptionPane.showMessageDialog(this,info,"About...",JOptionPane.INFORMATION_MESSAGE);
    }
    }

    public static void main(String [] args)
    {
    try {
         UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
         System.err.println("Couldn't use the system look and feel:"+e);
        }
        //JFrame.setDefaultLookAndFeelDecorated(true);
    MibExplorer mibExplorer=new MibExplorer();
    mibExplorer.init();
    mibExplorer.setVisible(true);
    }
    }
      

  2.   

    这个代码不行啊,为什么我只出来一个JFrame窗口什么都不显示,下面是报错
    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jimi/core/component/JimiCanvas
    at com.adventnet.snmp.ui.MibTree.<init>(MibTree.java:179)
    at com.adventnet.snmp.ui.MibTree.<init>(MibTree.java:147)
    at InformationManager.mibTreePane(InformationManager.java:129)
    at InformationManager.init(InformationManager.java:110)
    at InformationManager.main(InformationManager.java:396)
    Caused by: java.lang.ClassNotFoundException: com.sun.jimi.core.component.JimiCanvas
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more
      

  3.   

    下载一个 jimi 类库就可以了。http://d.download.csdn.net/down/444010/hb_chenli
      

  4.   

    下载一个 jimi 类库就可以了。http://d.download.csdn.net/down/444010/hb_chenli 
    加入库后,我只出来一个JFrame窗口什么都不显示,报错都没有
    请大峡指点下