如何将此程序放在Jframe上?

时间:2014-06-01 22:09:50

标签: java swing jframe jpanel joptionpane

我在如何将这个程序放在JFrame上时遇到了麻烦。我不确定该怎么做。如果有人给我一些帮助,那就太好了。非常感谢,我很感激。帮助任何人:/?

   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);
    }
}
}

3 个答案:

答案 0 :(得分:0)

除了使用" windowbuilder"之外,没有捷径可用。在eclipse fx中使用图形界面制作它们。但是你仍然需要理解你用来按照自己的方式操纵它们的许多项目的逻辑。

我建议你阅读一本教程并至少学习基础知识(编程中没有快捷方式)。

喜欢这个fx: http://www.fortystones.com/creating-simple-gui-beginners-java-tutorial/

答案 1 :(得分:-1)

我很确定你可以这样做:

public class vendMachMain extends Jpanel;

然后再

    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);
    }

然后将所有代码放在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

     } 
     }

答案 2 :(得分:-2)

只需简单地设置一个JFrame,然后设置它并将JComponent类本身添加到JFrame,如:

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

说您的程序中仍有很多错误需要先排序。如果您想要JFrame上的输出和输入,那么System.out.println()也不会Scanner工作,您必须为用户添加一些GUI(例如JTextField输入或JLabel输出。)