How to implement GUI to my code? I know how to make the buttons and etc i just dont know how to "connect them together"
thanks

`import java.util.Scanner;

public class Pizza {

public static void main(String[] args) {
    Scanner inScan = new Scanner(System.in);
    int choice, choiceEnd, howMany, pizzaNumber;
    double totalCost, sPizzaCost, eCheese, pep, gPeppers, mushrooms, sausage, onion;
    String pizzaName, pizzaSize;
    totalCost = 0;
    sPizzaCost = 0;

     double thin , thick;

    System.out.println("Would you like to order a pizza?");
    System.out.println("1. Yes");
    System.out.println("2. No");
    System.out.println();
    choiceEnd = inScan.nextInt();
    while (choiceEnd < 2)
        if (choiceEnd == 1) {
            System.out.println("Which pizza size do you want?");
            System.out.println("1. Thin  (7.99)");
            System.out.println("2. Thick (9.99)");

            pizzaName = "";
            choice = inScan.nextInt();
            if (choice == 1) {
                sPizzaCost = 7.99;
                pizzaNumber = 1;
                pizzaName += " Thin ";
            }
            if (choice == 2) {
                sPizzaCost = 9.99;
                pizzaNumber = 1;
                pizzaName += "Thick ";
            }

            System.out.println("Would you like Pepperoni on your pizza?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            choice = inScan.nextInt();
            if (choice == 1) {
                pep = 2.25;
                sPizzaCost = sPizzaCost + pep;
                pizzaName += "with Pepperoni ";
            }
            System.out
                    .println("Would you like Green Peppers on your pizza?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            choice = inScan.nextInt();
            if (choice == 1) {
                gPeppers = 2.37;
                sPizzaCost = sPizzaCost + gPeppers;
                pizzaName += "with Green Peppers ";
            }
            System.out.println("Would you like Mushrooms on your pizza?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            choice = inScan.nextInt();
            if (choice == 1) {
                mushrooms = 2.05;
                sPizzaCost = sPizzaCost + mushrooms;
                pizzaName += "with Mushrooms ";
            }
            System.out.println("Would you like Sausages on your pizza?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            choice = inScan.nextInt();
            if (choice == 1) {
                sausage = 2.75;
                sPizzaCost = sPizzaCost + sausage;
                pizzaName += "with Sausage ";
            }
            System.out.println("Would you like Onions on your pizza?");
            System.out.println("1. Yes");
            System.out.println("2. No");
            choice = inScan.nextInt();
            if (choice == 1) {
                onion = 1.50;
                sPizzaCost = sPizzaCost + onion;
                pizzaName += "with Onions ";
            }


            System.out.println();
            System.out.print("Pizza Order");
            System.out.println("Amount Due");
        }

}`

Step one is to disentagle your current GUI from the code about pizzas that you will need in the GUI version. eg at lines 23-35 you could create new method eg choseSize(int size) and move the code from lines 24-35 into it, so the code now reads:

choice = inScan.nextInt();
choseSize(choice);

After you have done that for all th existing code it will be straight forward to call those same methods from a JButton or whatever GUI design you chose.

This is an example of separation of "model" and "view", which is a key architectural pattern seen throught Java

Edited 3 Years Ago by JamesCherrill

somehow I think the question is more related to: how do I add an action to my buttons, and call it by clicking on them?

Hi
thanks
I know how to create the buttons and make them do something.
Just not sure how for example

System.out.println("Would you like to order a pizza?");
System.out.println("1. Yes");
System.out.println("2. No");

create a button to "yes" and one for "no" and when i click on them it will do what the code tells it to do.

as i said i know how to create the buttons i just dont know how to link it to my code.

Looks like stultuske was tight (again)

Here's a tutorial on how to make things happen when someone clicks a button. Once you've got that, follow the advice in my earlier post, putting the calls to your new methods in the actionPerformed.

This article has been dead for over six months. Start a new discussion instead.