任何人都可以向我解释为什么我的作业总是重新调整 1.0 的值,我问我的老师,他告诉我“语言非常相似,我不记得用这种语言 -.-..... 你不应该学习这个,因为你在 java 1 而不是 java 2" ......无论如何,我试图取得成功并自学
/**
* Author Rich Talcik, Do not Edit or modify
* Homework assignment2
* 09/11/13
* Problem 2.7 page 109
*
*/
private static final long serialVersionUID = 1L;
JLabel jlabel;
JFrame frame = new JFrame("home work");
JTextField tfAnswer = new JTextField("Click to find travel Time");
JTextField tfSpeed = new JTextField("Remvoe Text & Enter Speed here");
JTextField tfDistance = new JTextField("Remove Text & Enter Distance Here");
float dSpeed;
float dDistance;
String sTotal;
String sSpeed;
String sDistance;
public shapeDrawing() {
super(new BorderLayout());
JButton button = new JButton("Calculate my Travel Time");
//Converting the speed to an Integer from a String
button.addActionListener(this);
tfSpeed.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
tfSpeed.setText("");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
tfDistance.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
tfDistance.setText("");
tfAnswer.setText("");
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
add(tfSpeed, BorderLayout.AFTER_LINE_ENDS);
add(tfDistance, BorderLayout.LINE_START);
add(tfAnswer, BorderLayout.PAGE_END);
add(button, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void actionPerformed(ActionEvent e) {
sSpeed = tfSpeed.getText();
dSpeed = Integer.valueOf(sSpeed);
//Converting Distance to integer from string
sDistance = tfSpeed.getText();
dDistance = Integer.valueOf(sDistance);
final float fTotal = (dDistance / dSpeed);
tfAnswer.setText(String.valueOf(fTotal));
}
有人可以解释为什么 tfAnswer.setText 不断返回 1.0 的值