📄 set_one.java
字号:
pduInFlight = false; // TODO: invokeLater setMessage("Received answer"); if (pdu.getErrorStatus() != AsnObject.SNMP_ERR_NOERROR) { setErrorMessage(pdu.getErrorStatusString()); } else { try { varbind[] vars = pdu.getResponseVarbinds(); varbind var = vars[0]; if (var != null) { AsnObjectId oid = var.getOid(); toid.setText(oid.toString()); AsnObject obj = var.getValue(); tvalue.setText(obj.toString()); ttype.setText(obj.getRespTypeString()); } } catch(uk.co.westhawk.snmp.stack.PduException exc) { System.out.println("update(): PduException " + exc.getMessage()); } } setButton.setEnabled(true); getButton.setEnabled(true); getNextButton.setEnabled(true);}public void setErrorMessage(String message){ setMessage(message, true);}public void setMessage(String message){ setMessage(message, false);}public void setMessage(String message, boolean isError){ lmessage.setText(message); Color c = Color.white; if (isError) { c = Color.red; } lmessage.setBackground(c);}private void makeLayout(String host, String oid, int port, String com){ JLabel lhost, loid, lport, lcom, lbind, lvalue, lversion, lsocket, ltype; // panel for the host, etc lhost = new JLabel("Host: "); lport = new JLabel("Port: "); lcom = new JLabel("Community: "); lbind = new JLabel("Bind: "); loid = new JLabel("OID: "); lvalue = new JLabel("Value: "); ltype = new JLabel("Type: "); lversion = new JLabel("Version: "); lsocket = new JLabel("Socket: "); thost = new JTextField(host); tport = new JTextField(String.valueOf(port)); tcom = new JTextField(com); tbind = new JTextField(bindAddr); toid = new JTextField(oid); tvalue = new JTextField(); ttype = new JTextField(oid); ttype.setEditable(false); ttype.setOpaque(true); ttype.setBackground(Color.white); snmpVersion = new JComboBox(); snmpVersion.addItem("v2c"); snmpVersion.addItem("v1"); socketTypeChoice = new JComboBox(); socketTypeChoice.addItem(SnmpContextBasisFace.STANDARD_SOCKET); socketTypeChoice.addItem(SnmpContextBasisFace.TCP_SOCKET); setButton = new JButton ("Set"); getButton = new JButton ("Get"); getNextButton = new JButton ("GetNext"); lmessage = new JLabel(""); lmessage.setOpaque(true); lmessage.setBackground(Color.white); GridBagLayout hostLayout = new GridBagLayout(); JPanel hostPanel = new JPanel(); hostPanel.setLayout(hostLayout); hostPanel.add(lhost, getGridBagConstraints2(0, 0, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(thost, getGridBagConstraints2(1, 0, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lport, getGridBagConstraints2(0, 1, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(tport, getGridBagConstraints2(1, 1, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lcom, getGridBagConstraints2(0, 2, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(tcom, getGridBagConstraints2(1, 2, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lbind, getGridBagConstraints2(0, 3, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(tbind, getGridBagConstraints2(1, 3, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lsocket, getGridBagConstraints2(0, 4, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(socketTypeChoice, getGridBagConstraints2(1, 4, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lversion, getGridBagConstraints2(0, 5, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(snmpVersion, getGridBagConstraints2(1, 5, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(loid, getGridBagConstraints2(0, 6, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(toid, getGridBagConstraints2(1, 6, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lvalue, getGridBagConstraints2(0, 7, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(tvalue, getGridBagConstraints2(1, 7, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(ltype, getGridBagConstraints2(0, 8, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(ttype, getGridBagConstraints2(1, 8, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); // panel for the buttons GridBagLayout buttonLayout = new GridBagLayout(); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(buttonLayout); buttonPanel.add(setButton, getGridBagConstraints2(0, 0, 1, 1, 0.3, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); buttonPanel.add(getButton, getGridBagConstraints2(1, 0, 1, 1, 0.3, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); buttonPanel.add(getNextButton, getGridBagConstraints2(2, 0, 1, 1, 0.3, 1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); // the whole panel GridBagLayout thisLayout = new GridBagLayout(); this.setLayout(thisLayout); this.add(hostPanel, getGridBagConstraints2(0, 0, 1, 1, 1.0, 0.3, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); this.add(buttonPanel, getGridBagConstraints2(0, 1, 1, 1, 1.0, 0.3, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); this.add(lmessage, getGridBagConstraints2(0, 2, 1, 1, 1.0, 0.3, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); setButton.addActionListener(this); getButton.addActionListener(this); getNextButton.addActionListener(this);}public void freeResources(){ if (context != null) { context.dumpContexts("Dump 2: "); context.destroyPool(); }}public static GridBagConstraints getGridBagConstraints2( int x, int y, int w, int h, double wx, double wy, int anchor, int fill, Insets ins, int ix, int iy){ GridBagConstraints gc = new GridBagConstraints(); gc.gridx = x; gc.gridy = y; gc.gridwidth = w; gc.gridheight = h; gc.weightx = wx; gc.weighty = wy; gc.anchor = anchor; gc.fill = fill; gc.insets = ins; gc.ipadx = ix; gc.ipady = iy; return gc;}public void windowOpened(WindowEvent e) {}public void windowClosed(WindowEvent e) {}public void windowIconified(WindowEvent e) {}public void windowDeiconified(WindowEvent e) {}public void windowActivated(WindowEvent e) {}public void windowDeactivated(WindowEvent e) {}public void windowClosing(WindowEvent e){ this.freeResources(); System.exit(0);}public static void main(String[] args){ String propFileName = null; if (args.length > 0) { propFileName = args[0]; } set_one application = new set_one(propFileName); JFrame frame = new JFrame(); frame.setTitle(application.getClass().getName()); frame.getContentPane().add(application, BorderLayout.CENTER); application.init(); frame.addWindowListener(application); frame.setSize(new Dimension(500, 250)); frame.setVisible(true);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -