我在调用构造函数时遇到错误(我认为)不知道如何解决它

时间:2016-03-30 15:19:28

标签: java

我在Java课程中有这个项目,我选择制作超市送货服务,它提供以下内容:
- 产品及其价格。
- 选择产品数量的能力。
- 用户提供他的电话号码,因此送货员可以联系他们以获取指示。
- 用户可以输入折扣代码。
- 用户可以取消订单或确认订单,当用户确认时,帐单上印有订单即将到达的备注。
- 用户可以输入他们将交付的金额,因此送货员可以带来变更。

我修了这个代码一百万次,我仍然犯了愚蠢的错误,因为我对这些错误视而不见

这次我的错误就像netBeans告诉我的那样

Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Insets.<init>(Insets.java:103)
    at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
    at sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:580)
    at java.awt.Window.init(Window.java:498)
    at java.awt.Window.<init>(Window.java:536)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:180)
    at supermarket.SuperMarket.<init>(SuperMarket.java:61)
    at supermarket.SuperMarket.<init>(SuperMarket.java:132)
package supermarket;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class SuperMarket extends JFrame implements ActionListener {

        double juice = 1, tuna = 4, bread = 7, shampoo = 25;
        double discount = 0, subTotal = 0, total = 0, d=0;
        double jnum=0, tnum=0, bnum=0, snum=0;
        double shN, brN, tuN, juN;
        double shd, brd, tud, jud;

        private JTextField shField;
        private JTextField brField;
        private JTextField tuField;
        private JTextField juField;

    private JButton cancelButton;
    private JButton confirmButton;

    private JTextField nmField;

        private JLabel shLabel;
        private JLabel brLabel;
        private JLabel tuLabel;
        private JLabel juLabel;

        private JLabel shAmount;
        private JLabel brAmount;
        private JLabel tuAmount;
        private JLabel juAmount;

        private JPanel buttonPanel;

        private JLabel shError;
        private JLabel brError;
        private JLabel tuError;
        private JLabel juError;

        private SuperMarket mainFrm;
        private SuperMarket childFrm;

    /**
     * Create the order form.
     */


    public SuperMarket() {

            shLabel=new JLabel ("Shampoo    Price: 25   ",SwingConstants.LEFT);
            shField = new JTextField(10);
            shAmount = new JLabel(shLabel+"Amount:  "+shField);

            try{
                shd = Double.parseDouble(shField.getText());
            }
            catch(NumberFormatException shE){
                shField.setToolTipText("You must enter integers.");
                shField.setText("");
            }

            shN = (int) shd;
            snum=shN*shampoo;          


            brLabel=new JLabel ("Bread  Price: 7    ",SwingConstants.LEFT);
            brField = new JTextField(10);            
            brAmount= new JLabel(brLabel+"Amount:  "+brField);            
            try{
                brd = Double.parseDouble(brField.getText());        
            }
            catch(NumberFormatException shE){
                brField.setToolTipText("You must enter integers.");
                brField.setText("");
            }
            brN = (int) brd;
            bnum=brN*bread;


            tuLabel=new JLabel ("Tuna   Price: 4    ",SwingConstants.LEFT);
            tuField = new JTextField(10);            
            tuAmount = new JLabel(tuLabel+"Amount:  "+tuField);

            try{
                tud = Double.parseDouble(tuField.getText());        
            }
            catch(NumberFormatException shE){
                tuField.setToolTipText("You must enter integers.");
                tuField.setText("");
            }
            tuN = (int) tud;
            tnum=tuN*tuna;


            juLabel=new JLabel ("Juice  Price: 1    ",SwingConstants.LEFT);
            juField = new JTextField(10);            
            juAmount = new JLabel(juLabel+"Amount:  "+juField);

            try{
                jud = Double.parseDouble(juField.getText());        
            }
            catch(NumberFormatException shE){
                juField.setToolTipText("You must enter integers.");
                juField.setText("");
            }            

            juN = (int) jud;
            jnum=juN*juice;


            // Order and cancel buttons
        buttonPanel = new JPanel();
        confirmButton = new JButton("Confirm");
        buttonPanel.add(confirmButton);

        cancelButton = new JButton("Cancel order");
                buttonPanel.add(cancelButton);

            mainFrm=new SuperMarket();

            confirmButton.addActionListener(this);
            cancelButton.addActionListener(this);
           add(shLabel);
            add(shAmount);
            add(tuLabel);
            add(tuAmount);
            add(brLabel);
            add(brAmount);
            add(juLabel);
        add(juAmount);
            add(buttonPanel);            
            setVisible(true);   
            pack();
    }


public static void main(String[] args) {

            SuperMarket market=new SuperMarket();
            market.setLayout(new FlowLayout());
            market.setTitle("AHL Supermarket Delivery Service");        
            market.setSize(800, 700);
            market.setDefaultCloseOperation(EXIT_ON_CLOSE);
            market.setVisible(true);


    }


        @Override
    public void actionPerformed(ActionEvent buttonEvent) {
        if (buttonEvent.getSource() == confirmButton) {
                    childFrm=new SuperMarket();
            JButton dnBtn=new JButton("Done");
                        childFrm.setLayout(new FlowLayout());
                        childFrm.setSize(200, 200);
                        JPanel nmPanel = new JPanel();
                        nmPanel.add(new JLabel("Name:  "));
                        nmField = new JTextField(10);
                        nmPanel.add(nmField);
                        childFrm.add(nmPanel);                               
                        childFrm.add(dnBtn);
                        dnBtn.addActionListener(this);
                        childFrm.setVisible(true);
        }

        clearForm();
    }

    /** 
     * Clear all entries in the order form.
     */
    private void clearForm() {
        shField.setText("");
        tuField.setText("");
        juField.setText("");
    }

}

我仍然没有添加输出,也没有添加总和折扣,但是当我当前的问题得到解决时,我会处理这个问题。
如果有人能帮助我,我会非常感激,因为我完全感到沮丧。
(你可以从不必要的变量数量来判断:[)

1 个答案:

答案 0 :(得分:10)

您正在构造函数中创建同一个类的新实例。

mainFrm=new SuperMarket();

这意味着你将再次调用构造函数,这将创建一个新实例,它将再次调用构造函数,依此类推,直到你用完了#34; memory&#34; (在这种情况下是堆栈空间,因此是StackOverflowError)。