Topic: 程序有问题,请各位帮忙解答

  Print this page

1.程序有问题,请各位帮忙解答 Copy to clipboard
Posted by: jasonwing27
Posted on: 2004-10-14 12:53

运行后我输入菜名后回车,输出的菜名和号码都是菜名
在输入号码后回车,则输出的都是号码
不知道程序错在哪了,请各位指点

import java.applet.Applet;
import java.awt.*;
public class GainedApp2 extends Applet
{
Label words ;
TextField inputwords ;
Label outputwords ;
Label OID ;
TextField inputnumber ;
Label outputnumber ;

public void init()
{
setLayout(new GridLayout(8, 4));

words = new Label ( "输入几个字符您要的菜名 :") ;
inputwords = new TextField(7) ;
outputwords = new Label (" ") ;

OID = new Label ( "输入您的取货号码 : ") ;
inputnumber = new TextField(3) ;
outputnumber = new Label (" ") ;

add (words) ;
add (inputwords) ;
add (outputwords) ;

add (OID) ;
add (inputnumber ) ;
add (outputnumber) ;
}

public boolean action (Event e, Object o)
{
outputwords.setText ("你需要的菜是 :"+o.toString()) ;
outputnumber.setText ("你的取货号是 :"+o.toString()) ;
return true ;
}
}

2.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-14 13:13

错误就在action里面,应该判断事件源,分类处理!
本人建议用添加事件监听器(ActionListener)的方法!

3.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: jasonwing27
Posted on: 2004-10-14 14:37

请高手指点我该怎么办一个是怎么分类
还有一个是怎么用ActionListener)的方法
谢谢

4.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-14 16:37

你确实输出的是同一个东西呀!
o.toString();

5.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-14 21:17

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class GainedApp2 extends Applet implements ActionListener
{
Label words ;
TextField inputwords ;
Label outputwords ;
Label OID ;
TextField inputnumber ;
Label outputnumber ;

Button button; //增加一个按钮

public void init()
{
setLayout(new GridLayout(8, 4));

words = new Label ( "输入几个字符您要的菜名 :") ;
inputwords = new TextField(7) ;
outputwords = new Label (" ") ;

OID = new Label ( "输入您的取货号码 : ") ;
inputnumber = new TextField(3) ;
outputnumber = new Label (" ") ;

add (words) ;
add (inputwords) ;
add (outputwords) ;

add (OID) ;
add (inputnumber ) ;
add (outputnumber) ;

button=new Button("确定");
button.addActionListener(this); //对按钮添加监听器
add(button);

}

public void actionPerformed (ActionEvent e)//事件处理器的方法
{
  if(e.getSource()==button)
  {
    outputwords.setText(inputwords.getText());
    outputnumber.setText(inputnumber.getText());
  }
}
}

6.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: jasonwing27
Posted on: 2004-10-14 22:40

谢谢,要是不用这个方法,还按照我的思路,那么应该怎么修改啊,小弟不材,再次发问

7.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-15 12:01

那就只需要改动action方法为:
public boolean action (Event e, Object o)
{
  if(e.target==inputwords){
outputwords.setText ("你需要的菜是 :"+o.toString()) ;
}
if(e.target==inputnumber){
outputnumber.setText ("你的取货号是 :"+o.toString()) ;
}
return true ;
}

8.Re:程序有问题,请各位帮忙解答 [Re: jasonwing27] Copy to clipboard
Posted by: jasonwing27
Posted on: 2004-10-15 12:04

谢谢啊,


   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