tags:

views:

72

answers:

2

I have been working on a simple Java app. I had it compiling and running. I made the changes to turn it into an Applet. However, when I finished, the applet portion of the HTML came up blank... No errors.

I decided I'd revert the changes so that I could compile it and run it in my IDE. Once I did that, the program would compile successfully, but then wouldn't produce my JFrame.

Can anyone see any reason why this isn't working...

//Import packages for GUI development.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.io.*;

public class Powell_5 extends JFrame
{
  //Left Panel of GUI.
      JButton btnSnickers = new JButton("Snickers");
      JButton btnButterFinger = new JButton("Butterfinger");
      JButton btnLays = new JButton("Lays Chips");
      JButton btnCola = new JButton("Coca-Cola");
      JButton btnDCola = new JButton("Diet Coke");
      JLabel  lblQuantity1 = new JLabel(" Quantity:");
      JLabel  lblQuantity2 = new JLabel(" Quantity:");
      JLabel  lblQuantity3 = new JLabel(" Quantity:");
      JLabel  lblQuantity4 = new JLabel(" Quantity:");
      JLabel  lblQuantity5 = new JLabel(" Quantity:");
      JTextField txtSnickers = new JTextField("0",3);
      JTextField txtButterFinger = new JTextField("0",3);
      JTextField txtLays = new JTextField("0",3); 
      JTextField txtCola = new JTextField("0",3); 
     JTextField txtDCola = new JTextField("0",3); 

     //Right Panel of GUI
      JButton btnClear = new JButton("Clear");
      JButton btnOrder = new JButton("Order");
      JLabel  lblEmployee = new JLabel("Employee Number:");
      JLabel  lblItems = new JLabel("Total Items Selected");
      JLabel  lblAmount = new JLabel("Total Amount of Order:");
      JTextField txtEmployee = new JTextField(5);
      JTextField txtItems = new JTextField("0",5);
      JTextField txtAmount = new JTextField("$0.00",8);

       //Variables for holding dollar amounts.
      int intSnickers;
      int intButterFinger;
      int intLays;
      int intCola;
      int intDCola;
      String strEmployee;
      int intTotalItems;
      double dblTotalAmount;   
      StringBuilder BuiltOrder = new StringBuilder();

        //Declare variables for main frame
      int WINDOW_WIDTH = 640;
       int WINDOW_HEIGHT = 235;

      //Declare some error string   
      String strTooMany = "The maximum number or each item that can be selected is 3.";
       //Set up to format dollar
     DecimalFormat dollar = new DecimalFormat("0.00");


     public void Powell_5()
     { 
       //create a window
      JFrame window = new JFrame();

      //set title of JFrame
      window.setTitle("");

      //assign constants to window size
      window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

      //specify "X" button property
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      //Set up Border Layout here
      window.setLayout(new BorderLayout());


      //Mnemonic for buttons
     btnSnickers.setMnemonic(KeyEvent.VK_S);
      btnLays.setMnemonic(KeyEvent.VK_L);
      btnButterFinger.setMnemonic(KeyEvent.VK_B);
      btnCola.setMnemonic(KeyEvent.VK_C);
      btnDCola.setMnemonic(KeyEvent.VK_D);
      btnClear.setMnemonic(KeyEvent.VK_E);
      btnOrder.setMnemonic(KeyEvent.VK_O);

      //Set Tooltip
      btnClear.setToolTipText("Click here to erase any pending order, and start over");
      btnOrder.setToolTipText("Click here to place your order");   

      //Make quantity, amount and items all read only
      txtItems.setEditable(false);
      txtSnickers.setEditable(false);
      txtButterFinger.setEditable(false);
      txtLays.setEditable(false);
        txtCola.setEditable(false);
        txtDCola.setEditable(false);
      txtAmount.setEditable(false);

      //Right align all numeric and currency fields.
      txtSnickers.setHorizontalAlignment(JTextField.RIGHT);
      txtLays.setHorizontalAlignment(JTextField.RIGHT);
      txtButterFinger.setHorizontalAlignment(JTextField.RIGHT);
      txtCola.setHorizontalAlignment(JTextField.RIGHT); 
      txtDCola.setHorizontalAlignment(JTextField.RIGHT); 
      txtAmount.setHorizontalAlignment(JTextField.RIGHT);
      txtItems.setHorizontalAlignment(JTextField.RIGHT); 


       //Create Products Type Panel
        JPanel Products = new JPanel();
      Products.setLayout(new GridLayout(6,3));

        Products.add(btnSnickers);
      Products.add(lblQuantity1);
      Products.add(txtSnickers); 
      Products.add(btnButterFinger);
      Products.add(lblQuantity2);
      Products.add(txtButterFinger); 
      Products.add(btnLays);
      Products.add(lblQuantity3);
      Products.add(txtLays);  
      Products.add(btnCola);
      Products.add(lblQuantity4);
      Products.add(txtCola);  
      Products.add(btnDCola);
      Products.add(lblQuantity5);
      Products.add(txtDCola);

     JPanel Details = new JPanel();
     Details.setLayout(new GridLayout(5,2));

     //add controls to panel
     Details.add(lblEmployee);
     Details.add(txtEmployee);
     Details.add(lblItems);
     Details.add(txtItems);
     Details.add(lblAmount);
     Details.add(txtAmount);
     Details.add(btnClear);
     Details.add(btnOrder);

     //Create button listeners
     btnOrder.addActionListener(new btnOrder());
     btnClear.addActionListener(new btnClearListener());
     btnSnickers.addActionListener(new btnProduct());
     btnButterFinger.addActionListener(new btnProduct());
     btnLays.addActionListener(new btnProduct());
     btnCola.addActionListener(new btnProduct());
     btnDCola.addActionListener(new btnProduct());

     //Add panels to window
       window.add(Products, BorderLayout.WEST);
       window.add(Details, BorderLayout.EAST);

       //Pack the contents and set visible to true
         pack(); 
       window.setVisible(true);            
}

    private class btnClearListener implements ActionListener
     {
      public void actionPerformed(ActionEvent e)
      {

           intSnickers = 0;
            intButterFinger = 0;
         intLays = 0;
         intCola = 0;
         intDCola = 0;
         strEmployee = "";
         intTotalItems = 0;
       dblTotalAmount = 0;


      //Clear text fields
      txtSnickers.setText("0");
      txtButterFinger.setText("0");
      txtLays.setText("0");
      txtCola.setText("0");
      txtDCola.setText("0");
      txtEmployee.setText("0");
      txtDCola.setText("0");
      txtItems.setText("0");
      txtAmount.setText("$0.00");   

      }
     }

    private class btnProduct implements ActionListener
     {
      public void actionPerformed(ActionEvent e)
      {  
       if (e.getSource() == btnSnickers)
        { 
          intSnickers++;      
          if(intSnickers > 3)
           {
            intSnickers = 3;
            JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
            dblTotalAmount = dblTotalAmount - .75 ;
            intTotalItems--;

          }

          txtSnickers.setText(String.valueOf(intSnickers));         
        }

       else if  (e.getSource() == btnLays)
        {
         intLays++;
          if(intLays > 3)
           {
            intLays = 3;
            JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
            dblTotalAmount = dblTotalAmount - .75 ;
            intTotalItems--;

          }

          txtLays.setText(String.valueOf(intLays));  
        } 
       else if  (e.getSource() == btnButterFinger)
        {
         intButterFinger++;
             if(intButterFinger > 3)
           {
            intButterFinger = 3;
            JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
            dblTotalAmount = dblTotalAmount - .75 ;
            intTotalItems--;

          }

          txtButterFinger.setText(String.valueOf(intButterFinger));  
        }
       else if  (e.getSource() == btnCola)
        {
         intCola++;
         if(intCola > 3)
           {
            intCola = 3;
            JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
            dblTotalAmount = dblTotalAmount - .75 ;
            intTotalItems--;

          }

          txtCola.setText(String.valueOf(intCola));
         }
       else if  (e.getSource() == btnDCola)
        {
         intDCola++;
         if(intDCola > 3)
           {
            intDCola = 3;
            JOptionPane.showMessageDialog(null, strTooMany, "Exceeds Maximum Quantity",JOptionPane.WARNING_MESSAGE);
            dblTotalAmount = dblTotalAmount - .75 ;
            intTotalItems--;

          }

          txtDCola.setText(String.valueOf(intDCola));       } 

       intTotalItems++;
       txtItems.setText(String.valueOf(intTotalItems));

       dblTotalAmount = dblTotalAmount + .75 ;
       txtAmount.setText(String.valueOf(dollar.format(dblTotalAmount)));

      }
     }

    private class btnOrder implements ActionListener
     {
      public void actionPerformed(ActionEvent e)
      { 
       strEmployee = txtEmployee.getText();

       if (strEmployee.length() == 4 && Character.isLetter(strEmployee.charAt(0)) && Character.isDigit(strEmployee.charAt(1)) && Character.isDigit(strEmployee.charAt(2)) && Character.isDigit(strEmployee.charAt(3)))
        {
           if (intTotalItems > 0 )
           {

             if (intSnickers > 0)
           BuiltOrder.append( intSnickers + "Snickers" + "\n");

             if (intLays > 0)
           BuiltOrder.append("intLays + \"Lays\" + \"\\n\"");

           JOptionPane.showMessageDialog(null, "Order for Employee " + strEmployee + ": \n" + BuiltOrder  + "\n\n Total Items: " + intTotalItems
                       + "\n Total Price: $: " + dollar.format(dblTotalAmount));   
         }       
       else
        {
           JOptionPane.showMessageDialog(null, "At least 1 item must be selected.", "Invalid Order Quantity",JOptionPane.ERROR_MESSAGE);           
         }


        } 

       else
        {
          JOptionPane.showMessageDialog(null, "The employee code must be 4 digits long.", "Invalid Employee Entry",JOptionPane.ERROR_MESSAGE);
            }    
      }
}
    //Main method to create an instance of the class
public static void main(String[] args)
     {
         new Powell_5();

     } 

}
+1  A: 

Some things worth checking:

  • Any exceptions?
  • Use jstack, a debugger or something to see if there is a deadlock or infinite loop anywhere.
  • Use a debugger/printf to see whether code you expect to execute does.

Other advice:

  • Don't extend JFrame (or JPanel or similar), unless it is really necessary.
  • You have a JFrame you are extending and a different JFrame you are creating (window).
  • Stick to the AWT Event Dispatch Thread EDT when using Swing (java.awt.EventQueue.invokeLater).
  • Keep to standard coding standards (including variable naming).
  • Make field private (and final where possible).
  • Most of the fields could probably be made local variables.
Tom Hawtin - tackline
+1  A: 

Remove the "void" from the Powell_5() declaration:

public Powell_5()
 {  
     //etc
Fredrick Pennachi
Thanks... That was it
cinqoTimo