首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

这个程序错在哪?(Java核心技术卷1P324例题)

2011-11-19 
这个程序错在哪?(Java核心技术卷一P324例题)package com.corejava.L93/*** 第七个数--短日期格式*/DateFo

这个程序错在哪?(Java核心技术卷一P324例题)
package com.corejava.L93;



  /**
  * 第七个数--短日期格式
  */
  DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
  format.setLenient(false);
  JFormattedTextField dateField2 = new JFormattedTextField(format);
  dateField2.setValue(new Date());
  addRow("Date(short,not lenient):", dateField2);

  try {
  DefaultFormatter formatter = new DefaultFormatter();
  formatter.setOverwriteMode(false);
  JFormattedTextField urlField = new JFormattedTextField(formatter);
  urlField.setValue(new URL("http://java.sun.com"));
  addRow("URL:", urlField);
  } catch (MalformedURLException e) {
  e.printStackTrace();
  }

  try {
  MaskFormatter formatter = new MaskFormatter("###-##-####");//错误1
  formatter.setPlaceholderCharacter('0');
  JFormattedTextField ssnField = new JFormattedTextField(formatter);
  ssnField.setValue("078-05-1120");
  addRow("SSN Mask:", ssnField);
  } catch (ParseException exception) {
  exception.printStackTrace();
  }

  JFormattedTextField ipField = new JFormattedTextField(
  new IPAddressFormatter());
  ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
  addRow("IP Address:", ipField);
  }

  public void addRow(String labelText, final JFormattedTextField field) {
  mainPanel.add(new JLabel(labelText));
  mainPanel.add(field);
  final JLabel valueLabel = new JLabel();
  mainPanel.add(valueLabel);
  okButton.addActionListener(new ActionListener() {//错误2
  public void acctionPerformed(ActionEvent event) {
  Object value = field.getValue();
  if (value.getClass().isArray()) {
  StringBuilder builder = new StringBuilder();
  builder.append('{');
  for (int i = 0; i < Array.getLength(value); i++) {
  if (i > 0)
  builder.append(',');
  builder.append(Array.get(value, i).toString());
  }
  builder.append('}');
  valueLabel.setText(builder.toString());
  } else
  valueLabel.setText(value.toString());
  }
  });
  }
}

class IntFilter extends DocumentFilter {
  public void insertString(FilterBypass fb, int offset, String string,
  AttributeSet attr) throws BadLocationException {
  StringBuilder builder = new StringBuilder(string);
  for (int i = builder.length() - 1; i >= 0; i--) {
  int cp = builder.codePointAt(i);
  if (!Character.isDigit(cp) && cp != '=') {
  builder.deleteCharAt(i);
  if (Character.isSupplementaryCodePoint(cp)) {
  i--;
  builder.deleteCharAt(i);
  }
  }
  }
  super.insertString(fb, offset, builder.toString(), attr);
  }

  public void replace(FilterBypass fb, int offset, int length, String string,


  AttributeSet attr) throws BadLocationException {
  if (string != null) {
  StringBuilder builder = new StringBuilder(string);
  for (int i = builder.length() - 1; i >= 0; i--) {
  int cp = builder.codePointAt(i);
  if (!Character.isDigit(cp) && cp != '-') {
  builder.deleteCharAt(i);
  if (Character.isSupplementaryCodePoint(cp)) {
  i--;
  builder.deleteCharAt(i);
  }
  }
  }
  string = builder.toString();
  }
  super.replace(fb, offset, length, string, attr);
  }
}

class FormattedTextFieldVerifier extends InputVerifier {
  public boolean verify(JComponent component) {
  JFormattedTextField field = (JFormattedTextField) component;
  return field.isEditValid();
  }
}

class IPAddressFormatter extends DefaultFormatter {
  public String valueToString(Object value) throws ParseException {
  if (!(value instanceof byte[]))
  throw new ParseException("Not a byte[]", 0);
  byte[] a = (byte[]) value;
  if (a.length != 4)
  throw new ParseException("Length !=4", 0);
  StringBuilder builder = new StringBuilder();
  for (int i = 0; i < 4; i++) {
  int b = a[i];
  if (b < 0)
  b += 256;
  builder.append(String.valueOf(b));
  if (i < 3)
  builder.append('.');
  }
  return builder.toString();
  }

  public Object stringToValue(String text) throws ParseException {
  StringTokenizer tokenizer = new StringTokenizer(text, ".");
  byte[] a = new byte[4];

  for (int i = 0; i < 4; i++) {
  int b = 0;
  if (!tokenizer.hasMoreTokens())
  throw new ParseException("Too few bytes", 0);
  try {
  b = Integer.parseInt(tokenizer.nextToken());
  } catch (NumberFormatException e) {
  throw new ParseException("Not an integer", 0);
  }
  if (b < 0 || b >= 256)
  throw new ParseException("Byte out of range", 0);
  a[i] = (byte) b;
  }
  if (tokenizer.hasMoreTokens())
  throw new ParseException("Too many bytes", 0);
  return a;
  }
}

错误1:Unhandled exception type ParseException
错误2:The type new ActionListener(){} must implement the inherited abstract method 
ActionListener.actionPerformed(ActionEvent)
这是在eclipse3.3中编译时出现的问题,折腾半天未果....

[解决办法]
编译器报编译错误,没有行数?

第二个问题,在你的编译器显示错误的地方,按CTRL+1,是可以直接补全的
[解决办法]
问题1: 引入的ParseException类的路径应该是:import java.text.ParseException;
问题2: public void acctionPerformed(ActionEvent event) acctionPerformed中多了一个c

clean,compile

热点排行