JButtons stopped performing actions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Strife1817
    New Member
    • Mar 2010
    • 1

    JButtons stopped performing actions

    Hey guys...

    Sorry to bother you with this, however, I am stumped. The project that I built - builds clean, and executes, however only one out of three buttons in working. Could someone take a look and see what is going on, because if I keep going around in circles, I am liable to delete the whole project and start again. (Deleted the old version last night when I couldn't figure it out.) It was 2 am, and I was exhausted.....

    Thanks in advance.

    Ian

    import java.awt.Border Layout;
    import java.awt.Dimens ion;
    import java.awt.GridBa gConstraints;
    import java.awt.GridBa gLayout;
    //import java.awt.GridLa yout;
    import java.awt.Insets ;
    import java.awt.event. ActionEvent;
    import java.awt.event. ActionListener;
    import java.text.Decim alFormat;

    import javax.swing.JBu tton;
    import javax.swing.JFr ame;
    import javax.swing.JLa bel;
    import javax.swing.JOp tionPane;
    import javax.swing.JPa nel;
    import javax.swing.JSc rollPane;
    import javax.swing.JTe xtArea;
    import javax.swing.JTe xtField;


    public class FuelCalc {
    JFrame displayPane = new JFrame("Trip Report");
    JLabel olabel = new JLabel("Odomete r Reading");
    JTextField otext = new JTextField();
    JLabel glabel = new JLabel("Gallons Filled");
    JTextField gtext = new JTextField();
    JLabel clabel = new JLabel("Cost for fill up");
    JTextField ctext = new JTextField();
    JButton quit = new JButton("Quit") ;
    JButton enter = new JButton("Enter" );
    JButton report = new JButton("Run Report");
    JTextArea text = new JTextArea();

    Double[][] FuelArray;
    int fai = 0; /*declare variable to use within array to keep track of
    current and prior entries*/


    public FuelCalc(){

    // backend
    FuelArray = new Double[3][7];

    // GUI
    otext.setColumn s(10);
    gtext.setColumn s(10);
    ctext.setColumn s(10);
    quit.addActionL istener(new quit());
    enter.addAction Listener(new EnterListener() );
    report.addActio nListener(new runreport());

    BorderLayout border1 = new BorderLayout();
    border1.setHgap (20);
    border1.setVgap (20);
    displayPane.get ContentPane().s etLayout(border 1);

    JPanel panelA = new JPanel();
    panelA.setLayou t(new GridBagLayout() );
    GridBagConstrai nts gc1 = new GridBagConstrai nts();
    gc1.insets = new Insets(10, 10, 10, 10);
    gc1.gridx = 0;
    gc1.gridy = 0;
    panelA.add(olab el, gc1);
    gc1.gridx = 1;
    gc1.gridy = 0;
    panelA.add(otex t, gc1);
    gc1.gridx = 0;
    gc1.gridy = 1;
    panelA.add(glab el, gc1);
    gc1.gridx = 1;
    gc1.gridy = 1;
    panelA.add(gtex t, gc1);
    gc1.gridx = 0;
    gc1.gridy = 2;
    panelA.add(clab el, gc1);
    gc1.gridx = 1;
    gc1.gridy = 2;
    panelA.add(ctex t, gc1);

    JScrollPane scrollPane = new JScrollPane(tex t);
    scrollPane.setV erticalScrollBa rPolicy(JScroll Pane.VERTICAL_S CROLLBAR_ALWAYS );
    scrollPane.setP referredSize(ne w Dimension(500, 200));

    JPanel center = new JPanel();
    BorderLayout bl2 = new BorderLayout();
    bl2.setHgap(20) ;
    bl2.setVgap(20) ;
    center.setLayou t(bl2);
    center.add(pane lA, BorderLayout.WE ST);
    center.add(scro llPane, BorderLayout.SO UTH);
    displayPane.get ContentPane().a dd(center, BorderLayout.CE NTER);

    JPanel right = new JPanel();
    right.setLayout (new GridBagLayout() );
    GridBagConstrai nts c = new GridBagConstrai nts();
    c.gridy = 0;
    c.insets = new Insets(10, 10, 10, 10);
    right.add(enter , c);
    c.gridy = 1;
    right.add(repor t, c);
    c.gridy = 2;
    right.add(quit, c);
    displayPane.get ContentPane().a dd(right, BorderLayout.EA ST);
    }
    public static void main(String[] args) {

    FuelCalc gui = new FuelCalc();
    gui.showcalc();

    }

    public void showcalc(){
    displayPane.set DefaultCloseOpe ration(JFrame.E XIT_ON_CLOSE);
    displayPane.pac k();
    displayPane.set Visible(true);
    }

    public class quit implements ActionListener{
    public void actionPerformed (ActionEvent a){
    System.exit(0);
    }
    }



    public class EnterListener implements ActionListener{
    public void actionPerformed (ActionEvent e){
    for(int i = 0; i<=fai-1; i++){
    double mileage = FuelArray[i][0];
    double gallons = 0;
    double cost = 0;
    double mileagetotal = FuelArray[fai - 1][3];
    double fueltotal = FuelArray[fai - 1][4];
    double costtotal = FuelArray[fai - 1][5];
    double mpgavg = FuelArray[fai -1][6];

    try {
    mileage = Double.parseDou ble(otext.getTe xt());
    } catch (NumberFormatEx ception e1) {
    JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }

    try {
    gallons = Double.parseDou ble(gtext.getTe xt());
    } catch (NumberFormatEx ception e1) {
    JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }

    try {
    cost = Double.parseDou ble(ctext.getTe xt());
    } catch (NumberFormatEx ception e1) {
    JOptionPane.sho wMessageDialog( null, "Please try again", "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }

    if(mileage<=(Fu elArray[fai-1][0])){
    JOptionPane.sho wMessageDialog( null, "The entered odometer reading must greater than the previous odometer reading",
    "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }
    if(mileage <= 0){
    JOptionPane.sho wMessageDialog( null, "The entered odometer reading must greater than zero",
    "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }
    else{
    // proceed

    if(gallons<=0){
    JOptionPane.sho wMessageDialog( null, "The wage must greater than 0",
    "Invalid Input",JOptionP ane.ERROR_MESSA GE);
    }
    else{

    // Write parsed values from textfields to array
    FuelArray[fai][0] = mileage;
    FuelArray[fai][1] = gallons;
    FuelArray[fai][2] = cost;
    //Update totals for mileage, fuel and cost
    FuelArray[fai][3] = mileagetotal = mileage - FuelArray[fai-1][0];
    FuelArray[fai][4] = fueltotal = gallons + FuelArray[fai-1][4];
    FuelArray[fai][5] = costtotal = cost + FuelArray[fai-1][4];
    FuelArray[fai][6] = mpgavg = FuelArray[fai][3] / FuelArray[fai][4];
    fai++; // Advance row count in array to write next row on "EnterListe ner"

    otext.setText(" ");
    gtext.setText(" ");
    ctext.setText(" ");
    }
    }
    }
    }
    }
    public class runreport implements ActionListener{
    public void actionPerformed (ActionEvent b){
    text.setText("" ); // clear previous report
    String newline = "\n";

    // for each entry...
    for(int i = 0; i<=fai-1; i++){
    double mileage = FuelArray[i][0];
    double gallons = FuelArray[i][1];
    double cost = FuelArray[i][2];
    double mileagetotal = FuelArray[i][3];
    double fueltotal = FuelArray[fai][4];
    double costtotal = FuelArray[fai][5];
    double mpgavg = FuelArray[fai][6];
    double lowmpg = 25;
    double highmpg = 35;

    text.append("Od ometer= " + mileage + " Gallons= " + gallons + " Cost= $" + cost + newline);


    DecimalFormat df = new DecimalFormat(" ######.##");

    text.append(new line + newline + newline + "************** ************" + newline);
    text.append("Tr ip Statistics");
    text.append("To tal miles traveled= " + df.format(milea getotal) + newline);
    text.append("Av erage Miles Per Gallon= " + df.format(mpgav g) + newline + newline);
    text.append("To tal spent on gas: $" + df.format(costt otal) + newline);

    if(mpgavg<lowmp g)
    text.append("Yo ur miles per gallon is below average");
    else if(mpgavg>lowmp g && mpgavg < highmpg)
    text.append("Yo ur miles per gallon is average");
    else if(highmpg >= mpgavg)
    text.append("Yo ur miles per gallon is above average");
    }
    }
    }
    }
    Attached Files
    Last edited by Strife1817; Mar 21 '10, 11:53 PM. Reason: showing text...
Working...