我完成了这个项目的代码。但是我遇到按钮问题。出于某种原因,当我按下按钮时,我的速度和刹车都不起作用。 *速度文本框应显示速度为0,当我按下加速按钮它应该增加7,当我按下制动按钮它应该减少5.我不知道我做错了什么。这是我的课程 的 Car.Java
dic = {}
fruit = 'apple'
vegetable = 'potato'
dic['fruit'] = fruit
dic['vegetable'] = vegetable
这是我的其他课 CarView ,我假设按钮不起作用
public class Car
{
private String make; //holds car make
private int speed; //holds car speed
private int yearModel; //holds year model
//Costructor that will accept the year model as
//an argument
public Car(String mk, int ym)
{
this.yearModel = ym;
this.make = mk;
this.speed = 0;
}
//getYearModel accesor
public int getYearModel()
{
return yearModel;
}
//getSpeed accesor
public int getSpeed()
{
return speed;
}
//getMake accessor
public String getMake()
{
return make;
}
//method for accelerate, this method is also additng 7(mph/km)
public void accelerate()
{
speed += 7; // accelerate the speed counter by 7
}
//Method for brake, this method should subtract 5
//from the current speed
public void brake()
{
speed = speed - 5;
}
}
任何帮助都将非常感谢
答案 0 :(得分:0)
您实际上需要从Car
获取状态值,并在更改时更新JTextField
...
accelerate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//accelerating the car
car.accelerate();
speedText.setText(Integer.toString(car.getSpeed()));
}
});
现在做同样的事情打破
答案 1 :(得分:0)
因为在更新回调中的支持字段时,您不会更新speedText
。
你想做的事情如下:
accelerate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//accelerating the car
car.accelerate();
speedText.setText(Integer.toString(car.getSpeed()));
}
});
brake.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//Apply brakes to the car
car.brake();
speedText.setText(Integer.toString(car.getSpeed()));
}
});