-1

I'm having trouble on how to make this program be put on a JFrame. I'm not sure exactly how to do it. It would be great if anyone gave me some help. Thank you very much I appreciate it. Help anyone :/?

   package VendMach;

import java.util.Scanner;

import javax.swing.JComponent;
 import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class VendMachMain extends JComponent {

public static class groceryshopping{
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);

        int choice = 1;
        double subtotal = 0;
        double price = 0;
        double discount = 0;

    do {    
        System.out.println( "Quick Deli" + "\n" +"1. Lunchables             $ 1.99 per   box" + 
    "\n" + "2. Chips                $ 0.99 per bag" + "\n" + "3. Chocolate Bars     $ 0.99 each" + "\n" + 
    "4. Soda                $ 0.99 a can" + "\n" + "5. Cold Cut Sandwiches  $ 2.99 each" + "\n" + "6. Apple Slices          $ 0.75 per bag" + 
    "\n" + "7. Juice Boxes          $ 0.99 each" + "\n" + "8. Cinnabons         $ 0.99 each" + "\n" + "9. Cookies               $ 1.99 per bag" +
    "\n" + "10.Gum                  $ 1.15 per pack" + "\n" + " " + "\n" + "0. Quit" + "\n" + " " + "\n" + "Your subtotal is $ " +(int)(subtotal * 100) / 100.0 
    + "\n" );

        System.out.println("What would you like to purchase?  \nIf you have completed your checkout, enter 0.");
            choice = input.nextInt();


    if (choice == 0)
        break;

        System.out.println("How many do you want?");
        int qty = input.nextInt();

        switch (choice)    {
            case 1:
                price = 1.99;
                break;
            case 2:
                price = 0.99;                        
                break;
            case 3:
                price = 0.99;
                break;
            case 4:
                price = 0.99;
                break;
            case 5:
                price = 2.99;
                break;
            case 6:
                price = 0.75;
                break;
            case 7:
                price = 0.99;
                break;
            case 8:
                price = 0.99;
                break;
            case 9:
                price = 1.99;
                break;
            case 10:
                price = 1.15;
        }        
            subtotal = subtotal + (qty * price);
        }

        while(choice > 0);

        System.out.println("Do you have the special discount card? (Y/N)");
            String discountInput = input.next();
        char discountClub = discountInput.charAt(0);    

        if (discountClub == 'y' || discountClub == 'Y'){
            discount = subtotal * .20;
        }    

        double tax = ((subtotal - discount) * 0.075); 
        double finalCost = subtotal - discount + tax;

        JOptionPane.showMessageDialog(null,"The subtotal is $ " + (int)(subtotal * 100) / 100.0 + "\n" + "Discount $ " + (int) (discount * 100) / 100.0 
+ "\n" + "Tax $ " + (int)(tax* 100) / 100.0 + "\n" + "Total price $ " + (int)(finalCost * 100) /  100.0);
    }
}
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    You can't. Your "program" is nothing more than a static main method with console input. In order to create a GUI you must first extract the logic out into real honest to goodness OOP classes. I suggest that you do this first. I would scrap this code, and think of the logic of the program, and then create my GUI from scratch. – Hovercraft Full Of Eels Jun 01 '14 at 22:14
  • Start by reading through [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) – MadProgrammer Jun 02 '14 at 00:32
  • In fact you asked the same type of question [here](http://stackoverflow.com/questions/21025171/how-to-open-this-program-on-a-window). This is the height of laziness, and I suggest that you stop doing this, asking similar questions without thought or effort. – Hovercraft Full Of Eels Jun 02 '14 at 00:57

3 Answers3

0

There are no shortcuts except using something like "windowbuilder" in eclipse fx to make them with a graphical interface. But you still need to understand the logic of many items you use to manipulate them the way you want.

I recommended you go through a tutorial and learn the basics at least (there are no shortcuts in programming).

Like this fx: http://www.fortystones.com/creating-simple-gui-beginners-java-tutorial/

ObedMarsh
  • 433
  • 4
  • 19
-1

I am pretty sure you can do it like this:

public class vendMachMain extends Jpanel;

and then later

    public static void main(String[] args){
    true means that the jpanel will be fullscreen, false means it will not.
    makeJPanel(true);
    } 

    public static void makeJPanel(boolean FullScreen){
    JFrame fr = new JFrame();
    //what are the dimensions of this screen?
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    //STATIC VARS
    CanvasWidth = (int) dim.getWidth();
    CanvasHeight = (int) dim.getHeight();
    fr.setSize(CanvasWidth, CanvasHeight );
    fr.setExtendedState(JFrame.MAXIMIZED_BOTH);
    fr.setUndecorated(FullScreen);
    //get a 'device' object--whatever that is...
    GraphicsDevice device     GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
    device.setFullScreenWindow(fr);
    vendMachMain vmm = new vendMachMain();
    vmm.setSize(CanvasWidth, CanvasHeight);
    fr.add(vmm);
    fr.setVisible(true);
    fr.setResizable(!FullScreen);
    }

and then put all of the code in this part, after the super.addnotify

     public void addNotify(){
    super.addNotify();
    //All of your code goes here. The best part is since it is not in main, 
    //you can use NON STATIC VARIABLES YAY

     } 
     }
EKW
  • 2,059
  • 14
  • 24
  • `addNotify` is not really a viable solution for constructing your interface, a better solution would be to use the constructor or even setters to change the state of the interface. Components may need to accessible before the interface is constructed. A component may also be added to multiple containers over it's life cycle... – MadProgrammer Jun 02 '14 at 00:32
-2

Just simply make a JFrame,set it up and add the class itself which is a JComponent to the JFrame like:

JFrame frame = new JFrame();
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(True);
frame.getContentPane().add(this);

Saying that there are still a lot of errors in your program that you need to sort out first. If you want output and input on a JFrame then System.out.println() wont work neither will a Scanner, you'll have to add some GUI for the user (e.g JTextField for input or JLabel for output).

Basim Khajwal
  • 946
  • 6
  • 6
  • 1
    All this does is to create a GUI that does nothing. – Hovercraft Full Of Eels Jun 01 '14 at 22:19
  • If you are using Netbeans IDE, you COULD (but be warned that many others would say DO NOT) use its GUI Builder to get a quick intro to making a GUI. For your app, the drag-and-drop capability might be just what you need to get motivated to do a GUI tutorial somewhere. Ultimately and in any event, you might follow [SplungeBob's answer at this link](http://stackoverflow.com/questions/21010799/looking-for-general-method-for-gridbaglayout-component-creation/21031737#21031737) to see that `GridBagLayout` isn't really impossible. But the bad news is that you have a fairly long road ahead. – DSlomer64 Jun 01 '14 at 22:24
  • You will "pair" a `JLabel` with an instruction or prompt with an editable `JTextField` for input and pair a `JLabel` with identification with an uneditable `JTextField` (or maybe a `JTextArea`) for output. You might want to use a `JCheckBox` for questions such as "Do you have a...". All of those go into a `JFrame`. You might subdivide input and output items into separate `JPanels`. In any case, Google "java swing tutorial". – DSlomer64 Jun 01 '14 at 22:43