Topic: 请问这个代码该怎么改?(事件处理有点问题)

  Print this page

1.请问这个代码该怎么改?(事件处理有点问题) Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-06-10 13:00

这是一个小应用程序,可以显示出界面,但就是事件处理有点问题,计算结果不对!
请问该怎么改??
代码:
...................................................................................

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Shop extends Applet
{
Label labela,labelb;
TextField tf;
Button buttona;
Checkbox check1,check2,check3;
static int tv_price=0;
static int icebox_price=0;
static int air_price=0;
public void init()
{
labela=new Label("select shop");
labelb=new Label(" count");
tf=new TextField(15);
tf.setEditable(false);
buttona=new Button("computer");
check1=new Checkbox("tv 2100");
check2=new Checkbox("icebox 2500");
check3=new Checkbox("air_condition 1600");
add(labela);
add(check1);
add(check2);
add(check3);add(buttona);
add(labelb);add(tf);
HandleListener h1=new HandleListener();
check1.addItemListener(h1);
check2.addItemListener(h1);
check3.addItemListener(h1);

}
private class HandleListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==check1&&check1.getState()==true)
tv_price=2100;
else tv_price=0;
if(e.getItemSelectable()==check2&&check2.getState()==true)
icebox_price=2500;
else icebox_price=0;
if(e.getItemSelectable()==check3&&check3.getState()==true)
air_price=1600;
else air_price=0;
HandleListener1 h2=new HandleListener1();
buttona.addActionListener(h2);
}
}
private class HandleListener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttona)
{
//tf.getvalue(tv_price+icebox_price+air_price);
int sum=tv_price+icebox_price+air_price;
String str=String.valueOf(sum);
tf.setText(str);
}
}
}

2.Re:请问这个代码该怎么改???? [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: Skybus
Posted on: 2006-06-10 13:26

在最后补上一个大括号运行就没有问题了

3.Re:请问这个代码该怎么改???? [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-06-10 13:33

这个代码最后的大括号是我复制的 时候弄错了
加上后就没有语法的问题了
但还是计算的结果有问题!
这才是我要问的
请问怎么改啊才使计算的结果正确啊!!
谢谢了

4.Re:请问这个代码该怎么改?(事件处理有点问题) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: stone2030
Posted on: 2006-06-11 00:27

我也是初学者,我试了一下,我想这样应该能得出你想要的结果
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Shop extends Applet{
  Label labela,labelb;
  TextField tf; Button buttona;
  Checkbox check1,check2,check3;
  static int tv_price=0;
  static int icebox_price=0;
  static int air_price=0;
  public void init() {
    labela=new Label("select shop");
    labelb=new Label(" count");
    tf=new TextField(15);
    tf.setEditable(false);
    buttona=new Button("computer");
    check1=new Checkbox("tv 2100");
    check2=new Checkbox("icebox 2500");
    check3=new Checkbox("air_condition 1600");
    add(labela);
    add(check1);
    add(check2);
    add(check3);
    add(buttona);
    add(labelb);
    add(tf);
    HandleListener h1=new HandleListener();
    check1.addItemListener(h1);
    check2.addItemListener(h1);
    check3.addItemListener(h1);
    
  }
  private class HandleListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
      if(check1.getState()==true)
        tv_price=2100;
      else tv_price=0;
      if(check2.getState()==true)
        icebox_price=2500;
      else
        icebox_price=0;
      if(check3.getState()==true)
        air_price=1600;
      else
        air_price=0;
      HandleListener1 h2=new HandleListener1();
      buttona.addActionListener(h2);
      }
    }
  private class HandleListener1 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if(e.getSource()==buttona) {
        //tf.getvalue(tv_price+icebox_price+air_price);
        int sum=tv_price+icebox_price+air_price;
        String str=String.valueOf(sum);
        tf.setText(str);
        }
      }
    }
  
  }

5.Re:请问这个代码该怎么改?(事件处理有点问题) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-06-11 22:37

我象这样改过
但好象还是不行!

6.Re:请问这个代码该怎么改?(事件处理有点问题) [Re: 晒太阳的牙齿] Copy to clipboard
Posted by: 晒太阳的牙齿
Posted on: 2006-06-11 23:02

谢谢
你的是对的!!!!!!!


   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