Topic: 各位大侠请帮忙看看我这个小程序

  Print this page

1.各位大侠请帮忙看看我这个小程序 Copy to clipboard
Posted by: babyone
Posted on: 2004-09-22 21:34

呵呵 。。。 好久没上网了,主要是我的显示器坏了 ,现向各位大侠报个到!

import java.awt.*;
import java.awt.event.*;

public class FormatMonth implements TextListener,ActionListener extends MouseAdapter
{
  Frame f;
  Label lb;
  TextField tf;
  Button b1,b2,b3;
  String sInput;
  
  public static void main(String[] args){
    new FormatMonth();
  }
  
  public FormatMonth(){
    f=new Frame("年月日转换小程序");
    lb=new Label("输入简写的年月日如(07/12/2004),(按enter键清除)");
    
    b1=new Button("开始");
    b1.setActionCommand("b1");
    b1.addActionListener(this);
    
    b2=new Button("转换");
    b2.setActionCommand("b2");
    b2.addActionListener(this);
    
    b3=new Button("退出");
    b3.setActionCommand("b3");
    b3.addActionListener(this);
    
    tf=new TextField("",20);
    tf.addTextListener(this);
    tf.addActionListener(this);
    
    f.add(tf,BorderLayout.NORTH);
    f.add(b1,BorderLayout.WEST);
    f.add(b2,BorderLayout.CENTER);
    f.add(b3,BorderLayout.EAST);
    f.add(lb,BorderLayout.SOUTH);
    
    f.pack();
    f.setVisible(true);
  }
  
  public void textValueChanged(TextEvent e){
    la.setText(change(tf.getText()));
  }
    
  public void actionPerformed(ActionEvent e){
    String cmd=e.getActionCommand();
    
    b1.setEnabled(true);
    b2.setEnabled(true);
    b3.setEnabled(true);
    
    if(cmd.equals("b1")){
        tf.setText("");
        b2.setEnabled(false);
        b3.setEnabled(false);
    }
    else
      if(cmd.equals("b2")){
        lb.setText(change(tf.getText()));
        b1.setEnabled(false);
        b3.setEnabled(false);
     }
      else(cmd.equals("b3")){
        System.exit(0);
      }
      
    tf.setText("");
}
  
public String change(sInput){
    int x,y;
    String str1,str2;
    
    x=sInput.indexof("/");
    
    str1=sInput.subString(0,x);
    
    switch(str1){
    case "01":str2="January"; break;
    case "02":str2="February"; break;
    case "03":str2="March"; break;
    case "04":str2="April"; break;
    case "05":str2="May"; break;
    case "06":str2="june"; break;
    case "07":str2="july"; break;
    case "08":str2="August"; break;
    case "09":str2="September"; break;
    case "10":str2="Octorber"; break;
    case "11":str2="November"; break;
    case "12":str2="December"; break;
    default: break;
    }
    y=sInput.lastIndexof("/");
    
    str3=sInput.subString(s,y);
    
    str4=sInput.subSring(y);
    
    return str2+str3+sr4;
  }
}

程序调试的时候显示:

---------------- JDK Debug Build ------------------
Compiling C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\FormatMonth.java
Command line: "C:\j2sdk1.4.1\bin\javac.exe" -deprecation -g -classpath C:\DOCUME~1\user\MYDOCU~1\MODELW~1\MYPROJ~1 "C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\FormatMonth.java"
The current directory is: C:\Documents and Settings\user\My Documents\Modelworks\MyProjects
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\FormatMonth.java:4: '{' expected
public class FormatMonth implements TextListener,ActionListener extends MouseAdapter
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\FormatMonth.java:106: '}' expected
}
^
C:\Documents and Settings\user\My Documents\Modelworks\MyProjects\FormatMonth.java:4: FormatMonth should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in FormatMonth
public class FormatMonth implements TextListener,ActionListener extends MouseAdapter
^
3 errors
Finished

小虾我不怎么明白 ,请教!

2.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: ghuang
Posted on: 2004-09-23 00:08

ActionListener 的 abstrct actionPerformed()没有实现。
(因为你implements 了ActionListener, 所以必须实现这个接口的方法
actionPerformed 除非你建的是abstract class)

3.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-23 09:57

是啊,我没注意到这点 ,谢谢大侠了!!

4.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-26 17:13

不对, TextListener 中的所定义的只有textValue这个方法,而且我已经实现了,同样,ActionListener 中的 actionPerformed 方法我也实现了,可以这么说,我把两个listener 中的方法都实现了, 怎么显示我还要将FormatMonth定义成 abstract 类呢 ?? 求 大侠们解释! 小弟 在此 苦等了 !

(对了, why 老兄,您能帮我解释一下吗 ?^_^!)

5.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-09-28 09:28

指名请教好象是不可以的Black Eye

我用你的上边的程序调试了一下,发现一些简单的语法错误,(也许这些天你自己可能已经修改好了)如果没有的话你先改一下好吗Smile

6.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-28 23:59

呵呵 。。。对不起各位大侠,小虾我是新手,不懂规矩 !

7.Re:各位大侠请帮忙看看我这个小程序 [Re: littledeer1974] Copy to clipboard
Posted by: babyone
Posted on: 2004-09-29 00:09

我是刚开始学java 的, 对java 编程不是十分熟悉,请教,到底是哪出了毛病了阿 ? 小弟我急等 !在此叩谢了 !

8.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: karsking
Posted on: 2004-09-29 10:37

我仔细的看了看,我觉得你还是最好写字母写的正确点,变量定义一下,方法名称也要写对。public void textValueChanged(TextEvent e){
la.setText(change(tf.getText()));
}中的la是那块地的葱?public String change(sInput){
int x,y;
String str1,str2;
x=sInput.indexof("/");
str1=sInput.subString(0,x);中sInput是什么类型的?sInput.indexof("/");方法名称的大小写对吗?(sInput.indexOf("/"))str1=sInput.subString(0,x);方法名称对吗?(str1=sInput.substring(0,x))还有下面的str3,str4类型写了吗?还有一个方法如上一样,大小写写错了,我建议你用一个好的编译软件,最好是JBUILDER或JCreator(JDK),把基础大牢了。许多都是语法的错误

9.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-10-21 16:35

谢谢 各位大侠的教导,最近一段时间,我 猛补了一阵基础知识,现在我的程序基本上已经可以运行了, 不过还是存在一个小问题,就是,输出有错误,

附上我修改后的代码:
import java.awt.*;
import java.awt.event.*;

public class FormatMonth extends MouseAdapter implements TextListener,ActionListener {
  Frame f;
  Label lb;
  TextField tf;
  Button b1,b2,b3;
  
  
  public static void main(String[] args){
    new FormatMonth();
  }
  
  public FormatMonth(){
    f=new Frame("年月日转换小程序");
    lb=new Label("输入简写的年月日如(07/12/2004),(按enter键清除)");
    
    b1=new Button("开始");
    b1.setActionCommand("b1");
    b1.addActionListener(this);
    
    b2=new Button("转换");
    b2.setActionCommand("b2");
    b2.addActionListener(this);
    
    b3=new Button("退出");
    b3.setActionCommand("b3");
    b3.addActionListener(this);
    
    tf=new TextField("",20);
    tf.addTextListener(this);
    tf.addActionListener(this);
    
    f.add(tf,BorderLayout.NORTH);
    f.add(b1,BorderLayout.WEST);
    f.add(b2,BorderLayout.CENTER);
    f.add(b3,BorderLayout.EAST);
    f.add(lb,BorderLayout.SOUTH);
    
    f.pack();
    f.setVisible(true);
  }
  
  public void textValueChanged(TextEvent e){
    lb.setText(change(tf.getText()));
  }
    
  public void actionPerformed(ActionEvent e){
    String cmd=e.getActionCommand();
    
    b1.setEnabled(true);
    b2.setEnabled(true);
    b3.setEnabled(true);
    
    if(cmd.equals("b1")){
        tf.setText(" ");
        b2.setEnabled(false);
        b3.setEnabled(false);
    }
    else
      if(cmd.equals("b2")){
        lb.setText(change(tf.getText()));
        b1.setEnabled(false);
        b3.setEnabled(false);
     }
      else{
        System.exit(0);
      }
      
    tf.setText("");
}
  
private String change(String sInput){
    int x,y,flag;
    String str1,str2="",str3,str4;
    
    x=sInput.indexOf("/");
    
    str1=sInput.substring(0,x-1);
    flag=Integer.valueOf(str1).intValue();
    
    switch(flag){
    case 1:str2="January"; break;
    case 2:str2="February"; break;
    case 3:str2="March"; break;
    case 4:str2="April"; break;
    case 5:str2="May"; break;
    case 6:str2="june"; break;
    case 7:str2="july"; break;
    case 8:str2="August"; break;
    case 9:str2="September"; break;
    case 10:str2="Octorber"; break;
    case 11:str2="November"; break;
    case 12:str2="December"; break;
    default: break;
    }
    y=sInput.lastIndexOf("/");
    
    str3=sInput.substring(x,y-1);
    
    str4=sInput.substring(y);
    
    return (str2+ str3 +str4);
  }
}

本意是如果是输入07/12/2004
就应改输出:December 07 2004
是不是最后一个return语句有问题啊 ?请教~~~~~

在线等答案~~~~

10.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-21 17:13

你的逻辑是乱的,懒得解释太多了:
private String change(String sInput){
int x,y,flag;
String str1="",str2="",str3,str4;
if(sInput.length()<10)return "";
x=sInput.indexOf("/");

str1=sInput.substring(0,x);

y=sInput.lastIndexOf("/");

str3=sInput.substring(x+1,y);
flag=Integer.valueOf(str3).intValue();

switch(flag){
case 1:str3="January"; break;
case 2:str3="February"; break;
case 3:str3="March"; break;
case 4:str3="April"; break;
case 5:str3="May"; break;
case 6:str3="june"; break;
case 7:str3="july"; break;
case 8:str3="August"; break;
case 9:str3="September"; break;
case 10:str3="Octorber"; break;
case 11:str3="November"; break;
case 12:str3="December"; break;
default: break;
}

str4=sInput.substring(y+1);

return (str3+" "+ str1+" " +str4);
}

11.Re:各位大侠请帮忙看看我这个小程序 [Re: babyone] Copy to clipboard
Posted by: babyone
Posted on: 2004-10-21 21:15

谢谢了, 其实,我也刚刚解决了输出错误的问题了 ~~

我的代码是这样的:
private String change(String sInput){
    int x,y,flag;
    String str1,str2="",str3,str4;
    
    x=sInput.indexOf("/");
    
    str1=sInput.substring(0,x);
    y=sInput.lastIndexOf("/");
    str3=sInput.substring(x+1,y);
    flag=Integer.valueOf(str3).intValue();
    
    switch(flag){
    case 1:str2="January"; break;
    case 2:str2="February"; break;
    case 3:str2="March"; break;
    case 4:str2="April"; break;
    case 5:str2="May"; break;
    case 6:str2="june"; break;
    case 7:str2="july"; break;
    case 8:str2="August"; break;
    case 9:str2="September"; break;
    case 10:str2="Octorber"; break;
    case 11:str2="November"; break;
    case 12:str2="December"; break;
    default: break;
    }
    
    str4=sInput.substring(y+1);
    
    return (str2 + str1 + str4);
  }
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923