Results 1 to 2 of 2
  1. #1
    tigerfan2 is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default Help-would like JFRAME added to program

    I have the following driver and class and would like to add JFRAME to it but have no clue how. Essentially I would like to figure out how to place all "public void Display" in GUI frames instead of the boring defualt dispaly boxes. If not what can I do to dress this program up a little bit. Plus how do you line a text string up so it appears a little neater, see my menu. thanks.

    Here is the Driver:
    public class Main {
    /**
    *
    */
    public static void main (String[] args) {
    /**
    * Set up arrays and variables
    */
    double unitCost [] = {0.00, 2.50, 100.00, 75.00, 95.00, 15.00, 8.50, 65.00, 45.00, 12.00, 72.00 };
    String product [] = { "No item", "BaseBall", "Bat", "Infield Glove", "Catcher's Mit", "Batting Helmet", "Batting Glove", "Uniform with Team Logo", "Uniform without Team Logo", "Hat", "Cleats" };
    String custNames [] = new String [6];
    int quantityOrdered [] [] = new int [6] [11];
    double totalSales [] [] = new double [6] [11];

    double totalUnitCost = 0.0, totalCustSale = 0.0; //taxrate = 0.10, tax = 0.0;
    int quantity = 0, itemNumber = 0, maxCust = 0, custNumber = 0, maxC = 0, maxProd = 0; //
    String company = "Butch's Baseball Supplies", output, input;

    maxProd = product.length;
    WClass WEF = new WClass();
    // Obtain maximum number of Customers to process: call getMaxCust method
    do
    {
    maxCust = WEF.getMaxCust("Enter the number of customers:");
    }
    while (maxCust< 1 || maxCust >5);


    /**
    * Obtain the names of Customers to process: call getCustNames method
    */
    WEF.getCustNames(custNames, maxCust);

    /**
    * Use a loop to process each customer in the Customer array
    */

    for ( custNumber = 1; custNumber <=maxCust; ++custNumber)
    {
    do // process do-while for same customer until he enters '0' for item, which is exit
    {
    itemNumber = WEF.getChoice (custNumber); // Call method to get the item number
    if (itemNumber !=0 )
    {
    quantity = WEF.getQuantity (custNumber, itemNumber, product, custNames, quantityOrdered);
    WEF.itemPriceTotal (custNumber, itemNumber, quantityOrdered, unitCost, totalSales);
    }

    } while (itemNumber !=0);
    WEF.displays (custNumber, maxCust, product, custNames, itemNumber, quantityOrdered, unitCost, totalSales);
    }

    WEF.eodDisplay(maxProd, product, custNames, quantityOrdered, unitCost, totalSales);
    }
    }

    Here is mty working class:
    package javaapplication24;
    import javax.swing.*;
    import java.text.DecimalFormat;
    /**
    *
    * @author
    */
    public class WClass {

    DecimalFormat twoDigits = new DecimalFormat ("0.00"); //set decimal format for double

    int maxC = 0, c = 0;
    String input;

    public int getMaxCust (String message)
    {

    input = JOptionPane.showInputDialog(message);
    maxC=Integer.parseInt(input);
    if (maxC < 1 || maxC > 5)
    JOptionPane.showMessageDialog(null, "Invalid Number - choose between 1 and 5 customers");
    return maxC;
    }
    public void getCustNames(String custNames[], int maxC)
    {
    for (int c=1; c <=maxC; ++c)
    {custNames [c]=JOptionPane.showInputDialog("Enter customer #"+c+"'s name:");
    }
    }

    public int getChoice (int c)
    {
    String menu,input;
    int choice = 0;

    menu = " WELCOME TO BUTCH'S BASEBALL SUPPLIES\n";
    menu += " PLEASE SELECT FROM THE ITEMS BELOW AND PLACE THE ITEM NUMBER IN THE BOX \n\n";
    menu += " 1 - BaseBall------------------- $2.50\n";
    menu += " 2 - Bat------------------------ $100.00\n";
    menu += " 3 - Infield Glove-------------- $75.00\n";
    menu += " 4 - Catcher's Mit-------------- $95.00\n";
    menu += " 5 - Batting Helmet------------- $15.00\n";
    menu += " 6 - Batting Glove-------------- $8.50\n";
    menu += " 7 - Uniform with Team Logo----- $65.00\n";
    menu += " 8 - Uniform without Team Logo-- $45.00\n";
    menu += " 9 - Hat------------------------ $12.00\n";
    menu += " 10- Cleats--------------------- $72.00\n";
    menu += "ENTER ZERO (0) WHEN ALL NEEDED ITEMS HAVE BEEN SELECTED.\n";

    input = JOptionPane.showInputDialog(menu);
    choice = Integer.parseInt(input);
    return choice;
    }
    public int getQuantity (int custNumber, int itemNumber, String product[], String custNames[], int quantityOrdered[][])
    {
    int quantity = 0;
    String qtyPrompt;
    do
    {
    qtyPrompt = custNames[custNumber]+", You have ordered from Butch's Baseball Supplies\n";
    qtyPrompt += custNames[custNumber]+", You have order item number "+itemNumber+" for "+product[itemNumber]+"\n";
    qtyPrompt += "Enter the quantity of "+product[itemNumber]+ "s you would like to order:\n";

    input = JOptionPane.showInputDialog(qtyPrompt);
    quantity = Integer.parseInt(input);
    } while (quantity == 0);
    //End "do while loop"

    quantityOrdered[custNumber][itemNumber] = quantity;
    quantityOrdered[0][0] += quantity;
    quantityOrdered[0][itemNumber] += quantity;
    return quantity;
    }

    public void itemPriceTotal (int custNumber, int itemNumber, int quantityOrdered[][], double unitCost[], double totalSales[][])
    {
    totalSales[custNumber][itemNumber] = quantityOrdered[custNumber][itemNumber]*unitCost[itemNumber];

    totalSales[0][0] += totalSales[custNumber][itemNumber];

    totalSales[0][itemNumber] += totalSales[custNumber][itemNumber];

    totalSales[custNumber][0] += totalSales[custNumber][itemNumber];
    }

    public void displays (int custNumber, int maxCust, String product[], String custNames[], int itemNumber, int quantityOrdered[][],
    double unitCost[], double totalSales[][])
    {
    String output;

    if (totalSales [custNumber][0] >0)
    {output = "BUTCH'S BASEBALL SUPPLIES\n\n";
    output += "CUSTOMER: "+custNames[custNumber]+"\n\n";
    output += "ORDER:\n\n";
    output += "ITEM\tQuantity\tUnit Price\tTotal\n\n";

    for (int i=1; i <= 10; i++)
    {
    if (quantityOrdered[custNumber][i] > 0)
    {
    output+= product[i]+"\t"+quantityOrdered[custNumber][i]+ "\t$" +twoDigits.format(unitCost[i])+"\t$" +twoDigits.format(totalSales[custNumber][i])+"\n";
    }
    } // End of 'for' loop
    output += "\nTotal:\t\t\t$"+twoDigits.format(totalSales[custNumber][0])+"\n\n";
    JTextArea outputArea = new JTextArea();
    outputArea.setText(output);
    JOptionPane.showMessageDialog(null, outputArea, "Total Sales Invoice", JOptionPane.PLAIN_MESSAGE);
    }// endif
    }

    public void eodDisplay (int maxProd, String product[], String custNames[], int quantityOrdered[][], double unitCost[], double totalSales[][])
    {
    String output;
    output = "Item #"+"\t"+"Quantity"+"\t"+"Unit Price"+"\t"+"Total\n\n";
    for ( int p=1; p <= maxProd-1; p++) //this loop processes through each item's EOD quantity sold and its total revenue
    {
    if (quantityOrdered [0][p] > 0) //Skip items with zero quantity sold
    {
    output += p+"\t"+quantityOrdered[0][p]+"\t$"+twoDigits.format(unitCost[p])+"\t$"+twoDigits.format (totalSales[0][p])+"\n";
    }
    //endif
    }
    // After the loop processes the toatals for each individual item sold, display the overall day's total quantity & sales
    output += "\nTotal items sold today = "+"\t"+ quantityOrdered [0][0]+"\n";
    output += "\nTotal Sales today ="+"\t$"+twoDigits.format(totalSales[0][0])+"\n\n";
    JTextArea outputArea = new JTextArea ();
    outputArea.setText(output);
    JOptionPane.showMessageDialog(null, outputArea, "Butch's Baseball Supplies End of Day Report",JOptionPane.PLAIN_MESSAGE);
    }
    }

  2. #2
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    About the JFrame.
    You can use this format. i just created a simple Structure that can guide you.

    Java Code:
    import javax.swing.*;
     //Loads the Swing Components which contains the Jframe and others
    import java.awt.*; 
    //Loads AWT which contains the container and Listener if you like to use buttons
    
    //Global Declarations
           private JLabel A = new Jbutton();
    
    //adding extends & implements enable the use of Jframe and Action Listeners
    public class Main extends JFrame implements ActionListener{ 
    JFrame Samp = new JFrame();
            Samp.setTitle("This is my Program"); //Set the Title
            Samp.setBounds(100, 100, 800, 600); //(set the location and size)
            Samp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            Samp.setVisible(true); //This will open your Gui
            Container container = this.getContentPane(); //set a container
            // From this on you can add buttons, textfields, etc to the container
            
           container.setLayout(null); 
           //setting it to null but you can use other layouts on this
    
           //Example of adding a button
           Jbutton A = new Jbutton("Compute Quantity");
           A.addActionListener(this); //adds a listener
           A.setBounds(x,y,Width,Ht); //This are in pixels, set the location
           container.add(A); // finally adds the button to the container
    
           //Example of adding a label
           A.setText("Output Here");
           A.addActionListener(this); //adds a listener
           A.setBounds(x,y,Width,Ht); //This are in pixels, set the location
           container.add(A); // finally adds the button to the container
      
           //Add more components here see tutorials for other components =)
    
    }
    
    
    //Here is were the action listener awaits =)
       public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
        if(command.equalsIgnoreCase("Compute Quantity")){
            //Quantity equation here
            //Display the output to a textfield/Label/TextArea
            A.setText(Output Here); //Change the display of A (Jlabel)
        } else if (command.equalsIgnoreCase("Other buttons")){
        
        } else if (command.equalsIgnoreCase("Other buttons2")){
        
        }
    
    }
    
    }
    I hope this Helps. =P
    Last edited by Eku; 05-13-2008 at 05:01 AM.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  2. No datas in added to arraylist
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 12:07 PM
  3. Tree with Listener not working when added to ScrollPane
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2008, 08:06 AM
  4. Replies: 4
    Last Post: 01-05-2008, 08:39 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •