Esempio n. 1
0
  protected String buildMessage(Method method, Object[] args, long timeNanoSec) {
    String res = null;

    {
      StringBuilder sb = new StringBuilder();

      {
        sb.append("Object: \"");
        sb.append(resource.getClass().getSimpleName());
        sb.append("\n");

        sb.append("Method: \"");
        sb.append(method.toString());
        sb.append("\n");

        String time = null;

        if (timeNanoSec < 1000L * 1000L) // if time is less than one millisecond...
        {
          time = timeNanoSec + " ns";
        } else {
          if (timeNanoSec < 1000L * 1000L * 1000L) // if time is less than one second...
          {
            double timeMilliSec = ((double) timeNanoSec + 500L * 1000L) / (1000L * 1000L);

            String v = f.format(timeMilliSec);
            time = v + ". ms";
          } else {
            double timeSec =
                ((double) timeNanoSec + 500L * 1000L * 1000L) / (1000L * 1000L * 1000L);

            String v = f.format(timeSec);
            time = v + ". s";
          }
        }

        sb.append("Time: ");
        sb.append(time);
      }

      res = sb.toString();
    }

    return res;
  }
  public void init() {
    // 添加按钮
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okButton);

    mainPanel.setLayout(new GridLayout(0, 3));
    mainWin.add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField0 =
        new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
              protected DocumentFilter getDocumentFilter() {
                return new NumberFilter();
              }
            });
    intField0.setValue(100);
    addRow("只接受数字的文本框", intField0);

    JFormattedTextField intField1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField1.setValue(new Integer(100));
    // 添加输入校验器
    intField1.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("带输入校验器的文本框", intField1);

    // 创建自定义格式器对象
    IPAddressFormatter ipFormatter = new IPAddressFormatter();
    ipFormatter.setOverwriteMode(false);
    // 以自定义格式器对象创建格式化文本框
    JFormattedTextField ipField = new JFormattedTextField(ipFormatter);
    ipField.setValue(new byte[] {(byte) 192, (byte) 168, 4, 1});
    addRow("IP地址格式", ipField);

    mainWin.add(buttonPanel, BorderLayout.SOUTH);
    mainWin.pack();
    mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWin.setVisible(true);
  }