Problems accessing methods in an object.
Once again I have a problem!!
I was able to create an instance of a class (highlighted in red) in one action event and was able to access the variables speed and hours in other action events. I'm having a problem accessing the new object in my analyzeButtonActionPerformed section (underlined and highlighted in blue). Any reason why I can't here, but could elsewhere?
Code:
private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {
[COLOR="Red"]SpeedHours sh = new SpeedHours(speed, hours);[/COLOR]
this.resultField.setText("The distance is " + sh.getDistance() + " miles.");
}
private void hourFieldActionPerformed(java.awt.event.ActionEvent evt) {
try {
hours = Double.parseDouble(this.hourField.getText());
if (hours < 1) {
JOptionPane.showMessageDialog(this, "Number must be greater"
+ " than or equal to 1!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Cannot be a letter!", "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void speedFieldActionPerformed(java.awt.event.ActionEvent evt) {
try {
speed = Double.parseDouble(this.speedField.getText());
if (speed < 0) {
JOptionPane.showMessageDialog(this, "Number must be greater"
+ " than or equal to 0!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Cannot be a letter!", "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
speedField.setText("");
hourField.setText("");
resultField.setText("");
}
private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextArea1.setText("Hour Distance Travelled");
this.jTextArea1.append(SEPARATOR);
// Loop that calculates miles travelled per hour
int count = 1;
[U][COLOR="Blue"]while(count <= sh.getHours()){
double milesPerHour = sh.getSpeed() * count;[/COLOR][/U] count++;
}
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SpeedHoursDemo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton analyzeButton;
private javax.swing.JButton calcButton;
private javax.swing.JButton clearButton;
private javax.swing.JButton exitButton;
private javax.swing.JTextField hourField;
private javax.swing.JLabel hourLabel;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField resultField;
private javax.swing.JLabel resultLable;
private javax.swing.JTextField speedField;
private javax.swing.JLabel speedLabel;
// End of variables declaration
double speed;
double hours;
final String SEPARATOR = "...................................";
final String SPACE = " ";
}