📄 displaystring.java
字号:
}public String toInternationalDisplayString(byte[] value){ // I'm expecting Unicode, so this should work with a 'normal' String String ret = null; try { ret = new String(value, "UTF-8"); } catch (java.io.UnsupportedEncodingException exc) { ret = new String(value); } return ret;}/** * Implementing the Observer interface. Receiving the response from * the Pdu. * * @param obs the Pdu variable * @param ov the varbind * * @see uk.co.westhawk.snmp.stack.GetPdu * @see uk.co.westhawk.snmp.stack.SetPdu * @see uk.co.westhawk.snmp.stack.varbind */public void update(Observable obs, Object ov){ 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(); if (obj instanceof AsnOctets) { AsnOctets oct = (AsnOctets) obj; tvalue.setText(oct.toString(this)); } else { tvalue.setText(obj.toString()); } } } 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,String value){ JLabel lhost, loid, lport, lcom, lvalue, lversion; // panel for the host, etc lhost = new JLabel("Host: "); lport = new JLabel("Port: "); lcom = new JLabel("Community: "); loid = new JLabel("OID: "); lvalue = new JLabel("Value: "); lversion = new JLabel("Version: "); thost = new JTextField(host); tport = new JTextField(String.valueOf(port)); tcom = new JTextField(com); toid = new JTextField(oid); tvalue = new JTextField(value); snmpVersion = new JComboBox(); snmpVersion.addItem("v2c"); snmpVersion.addItem("v1"); 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(loid, getGridBagConstraints2(0, 3, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(toid, getGridBagConstraints2(1, 3, 1, 1, 1.0, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); hostPanel.add(lvalue, getGridBagConstraints2(0, 4, 1, 1, 0.0, 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); hostPanel.add(tvalue, 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)); // panel for the buttons setButton = new JButton ("Set"); getButton = new JButton ("Get"); getNextButton = new JButton ("GetNext"); 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 lmessage = new JLabel(""); lmessage.setBackground(Color.white); 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 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 static void main(String[] args){ String propFileName = null; if (args.length > 0) { propFileName = args[0]; } DisplayString application = new DisplayString(propFileName); JFrame frame = new JFrame(); frame.setTitle(application.getClass().getName()); frame.getContentPane().add(application, BorderLayout.CENTER); application.init(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setSize(new Dimension(500, 200)); frame.setVisible(true);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -