【问题标题】:How to put this program on a Jframe? [closed]如何把这个程序放在 Jframe 上? [关闭]
【发布时间】:2014-06-01 22:09:50
【问题描述】:

我在如何将这个程序放在 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);
    }
}
}

【问题讨论】:

  • 你不能。您的“程序”只不过是带有控制台输入的静态主要方法。为了创建一个 GUI,您必须首先将逻辑提取到真正诚实的 OOP 类中。我建议你先这样做。我会废弃这段代码,思考程序的逻辑,然后从头开始创建我的 GUI。
  • 事实上你问了同样类型的问题here。这是懒惰的高度,我建议你停止这样做,不假思索地问类似的问题。

标签: java swing jframe jpanel joptionpane


【解决方案1】:

除了在 eclipse fx 中使用“windowbuilder”之类的东西来使它们具有图形界面外,没有其他快捷方式。但是您仍然需要了解用于以您想要的方式操作它们的许多项目的逻辑。

我建议您阅读教程并至少学习基础知识(编程没有捷径)。

像这样的效果: http://www.fortystones.com/creating-simple-gui-beginners-java-tutorial/

【讨论】:

    【解决方案2】:

    我很确定你可以这样做:

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

    【讨论】:

    • addNotify 并不是构建界面的真正可行解决方案,更好的解决方案是使用构造函数甚至设置器来更改界面的状态。在构造接口之前,组件可能需要可访问。一个组件也可以在其生命周期内添加到多个容器中......
    【解决方案3】:

    只需简单地创建一个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 用于输出)。

    【讨论】:

    • 所有这些只是创建一个什么都不做的GUI。
    • 如果您使用的是 Netbeans IDE,您可以(但要注意许多其他人会说不要)使用它的 GUI Builder 来快速介绍制作 GUI。对于您的应用程序,拖放功能可能正是您在某处制作 GUI 教程所需的动力。最终,无论如何,您可能会关注SplungeBob's answer at this link 看到GridBagLayout 并不是真的不可能。但坏消息是,你还有很长的路要走。
    • 您将“配对”JLabel 与指令或提示与可编辑的JTextField 用于输入并将带有标识的JLabel 与不可编辑的JTextField 配对(或者可能是@987654336 @) 用于输出。您可能希望使用JCheckBox 来解决诸如“您有...”之类的问题。所有这些都进入JFrame。您可以将输入和输出项细分为单独的JPanels。无论如何,谷歌“java swing 教程”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多