craigtb
02-07-2007, 12:55 AM
I am trying to write a program where the user inputs a number and presses a button and then it plugs the number into an equation and gives an answer. Here is the code I am working with. Im getting errors all over the place. any help would be great.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// panel contianing components
public class CalculatorPanel extends JPanel{
// declare the parts of the equasion
private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
private JTextField posInt;
private JButton calculateButton;
private double solution;
ImageIcon icon = new ImageIcon ("expression.GIF");
// set up the GUI
public CalculatorPanel() {
// create labels
valueLabel = new JLabel("Enter a positive value:");
resultLabel = new JLabel("Value of expression: ");
imageLabel = new JLabel(icon);
// add text field
posInt = new JTextField (10);
// add listener and button
calculateButton = new JButton("Calculate Expression");
calculateButton.addActionListener (new PositiveIntListener());
// add to panel
add (imageLabel);
add (valueLabel);
add (posInt);
add (calculateButton);
add (resultLabel);
add (answerLabel);
// set size and background colo
setPreferredSize(new Dimension(300, 150));
setBackground(Color.green);
}
//*******************************
// Listener functions
//*******************************
private class PositiveIntListener implements ActionListener
{
// plug in the int into the equation
public void actionPerformed(ActionEvent event)
{
int integer;
integer = Integer.parseInt (text);
double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));
answerLabel.setText(result);
}
}
}
I have the driver program and im pretty sure it works. the 1st error im getting is around the integer = Integer.parseInt....
ANy advice will be great.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// panel contianing components
public class CalculatorPanel extends JPanel{
// declare the parts of the equasion
private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
private JTextField posInt;
private JButton calculateButton;
private double solution;
ImageIcon icon = new ImageIcon ("expression.GIF");
// set up the GUI
public CalculatorPanel() {
// create labels
valueLabel = new JLabel("Enter a positive value:");
resultLabel = new JLabel("Value of expression: ");
imageLabel = new JLabel(icon);
// add text field
posInt = new JTextField (10);
// add listener and button
calculateButton = new JButton("Calculate Expression");
calculateButton.addActionListener (new PositiveIntListener());
// add to panel
add (imageLabel);
add (valueLabel);
add (posInt);
add (calculateButton);
add (resultLabel);
add (answerLabel);
// set size and background colo
setPreferredSize(new Dimension(300, 150));
setBackground(Color.green);
}
//*******************************
// Listener functions
//*******************************
private class PositiveIntListener implements ActionListener
{
// plug in the int into the equation
public void actionPerformed(ActionEvent event)
{
int integer;
integer = Integer.parseInt (text);
double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));
answerLabel.setText(result);
}
}
}
I have the driver program and im pretty sure it works. the 1st error im getting is around the integer = Integer.parseInt....
ANy advice will be great.