表/数组问题

时间:2017-04-27 00:08:39

标签: java swing

我制作了一个旨在解决物理问题的程序。但每次我尝试进入下一帧时,通过单击按钮,程序完全崩溃(CONT。这是表/数组的问题.PS 这个问题是唯一的因为表/数组正在崩溃整个程序。单击按钮时还需要显示表格 。)

我的完整程序有更多的框架,但我只是因为尺寸原因而包括一个,但是我将完整的动作执行方法。

例外:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4
    at GuiInter.setInelas(GuiInter.java:321)
    at GuiInter.actionPerformed(GuiInter.java:522)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的节目(部分):

                    //Table
                    Object[][] inelasData = new Object[4][1];
                    String[] inelasTitles = new String[4];
                    Object[][] elasData = new Object[4][1];
                    String[] elasTitles = new String[4];
                    Object[][] currentData = new Object[3][2];
                    String[] currentTitles = new String[3];
                    Object[][] resistanceData = new Object[3][2];
                    String[] resistanceTitles = new String[3];




        public void setInelas()
        {
            inelasticFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            inelasticFrame.setBounds(500,300,600,700);
            inelasticFrame.setLayout(new BorderLayout());
            inelasticFrame.getContentPane().setBackground(inelasColor);

            buttonPanelInelas.setBackground(inelasColor);

            inelasEnterButtonM1.addActionListener(this);
            inelasReturnButton.addActionListener(this);
            inelasEnterButtonM2.addActionListener(this);
            //inelasEnterButtonV1i.addActionListener(this);
            //inelasEnterButtonV2i.addActionListener(this);
            inelasEnterButtonVf.addActionListener(this);

            buttonPanelInelas.add(inelasEnterButtonM1);
            buttonPanelInelas.add(inelasEnterButtonM2);
            buttonPanelInelas.add(inelasReturnButton);
            //buttonPanelInelas.add(inelasEnterButtonV1i);
            //buttonPanelInelas.add(inelasEnterButtonV2i);
            buttonPanelInelas.add(inelasEnterButtonVf);

            picturePanelInelas.add(inelasIcon);

            textPanelInelas.add(inelasM1);
            textPanelInelas.add(m1InElas);
            textPanelInelas.add(inelasM2);
            textPanelInelas.add(m2InElas);
            textPanelInelas.add(inelasV1i);
            textPanelInelas.add(v1iInElas);
            textPanelInelas.add(inelasV2i);
            textPanelInelas.add(v2iInElas);
            textPanelInelas.add(inelasVf);
            textPanelInelas.add(vfInElas);

            inelasTitles[0]="M1";
            inelasTitles[1]="M2";
            inelasTitles[2]="V1i";
            inelasTitles[3]="V2i";
            inelasTitles[4]="Vf";

            inelasData[0][1]=M1InElas;
            inelasData[1][1]=M2InElas;
            inelasData[2][1]=v1iInElas;
            inelasData[3][1]=v2iInElas;
            inelasData[4][1]=VFInElas;

            inelasticFrame.add(buttonPanelInelas, BorderLayout.CENTER);
            inelasticFrame.add(textPanelInelas, BorderLayout.NORTH);
            inelasticFrame.add(picturePanelInelas, BorderLayout.WEST);
            inelasticFrame.setVisible(true);
        }
public void actionPerformed(ActionEvent e)
    {
        //Elastic
        String v1fEStr;
        double v1fENo;
        String v1iEStr;
        double v1iENo;
        String v2iEStr;
        double v2iENo;
        String m1EStr;
        double m1ENo;
        String m2EStr;
        double m2ENo;

        //Inelastic
        String m1IeStr;
        double m1IeNo;
        String m2IeStr;
        double m2IeNo;
        String v1iIeStr;
        double v1iIeNo;
        String v2iIeStr;
        double v2iIeNo;
        String vfIeStr;
        double vfIeNo;
        String v1IeStr;
        double v1IeNo;

        //Current
        String currentCStr;
        double currentCNo;
        String voltageCStr;
        double voltageCNo;
        String powerCStr;
        double powerCNo;

        //Resistance
        String resistanceRStr;
        double resistanceRNo;
        String voltageRStr;
        double voltageRNo;
        String currentRStr;
        double currentRNo;

        String buttonCommand = e.getActionCommand();

        if(buttonCommand.equals("Inelastic Collision"))
            {
                System.out.println("You chose Inelastic Collision");
                welcomeFrame.dispose();
                setInelas();
            }
        else if(buttonCommand.equals("Calculate M1"))
        {
            GuiCalc CalcIeM1 = new GuiCalc();

            m2IeStr = m2InElas.getText();
            m2IeNo = Double.parseDouble(m2IeStr);
            v1iIeStr = v1iInElas.getText();
            v1iIeNo = Double.parseDouble(v1iIeStr);
            v2iIeStr = v2iInElas.getText();
            v2iIeNo = Double.parseDouble(v2iIeStr);
            vfIeStr = vfInElas.getText();
            vfIeNo = Double.parseDouble(vfIeStr);

            m1IeNo = CalcIeM1.CalcInelasM1(m2IeNo, v1iIeNo, v2iIeNo, vfIeNo);
            m1IeStr = Double.toString(m1IeNo);

            JTable inelasTableM1 = new JTable(inelasData,inelasTitles);
            JScrollPane inelasScrollPanelM1 = new JScrollPane(inelasTableM1);
            inelasTableM1.setFillsViewportHeight(true);
            inelasTableM1.setBackground(Color.RED);
            inelasticFrame.add(inelasScrollPanelM1, BorderLayout.SOUTH);

            M1InElas = m1IeNo;

            inelasticFrame.validate();
            inelasticFrame.repaint();
            System.out.println("M1 pressed");
        }
        else if(buttonCommand.equals("Calculate M2"))
        {
            GuiCalc CalcIeM2 = new GuiCalc();

            m1IeStr = m1InElas.getText();
            m1IeNo = Double.parseDouble(m1IeStr);
            v1iIeStr = v1iInElas.getText();
            v1iIeNo = Double.parseDouble(v1iIeStr);
            v2iIeStr = v2iInElas.getText();
            v2iIeNo = Double.parseDouble(v2iIeStr);
            vfIeStr = vfInElas.getText();
            vfIeNo = Double.parseDouble(vfIeStr);

            m2IeNo = CalcIeM2.CalcInelasM1(m1IeNo, v1iIeNo, v2iIeNo, vfIeNo);
            m2IeStr = Double.toString(m2IeNo);

            JTable inelasTableM2 = new JTable(inelasData,inelasTitles);
            JScrollPane inelasScrollPanelM2 = new JScrollPane(inelasTableM2);
            inelasTableM2.setFillsViewportHeight(true);
            inelasTableM2.setBackground(Color.RED);
            inelasticFrame.add(inelasScrollPanelM2, BorderLayout.SOUTH);

            M2InElas = m2IeNo;

            inelasticFrame.validate();
            inelasticFrame.repaint();
            System.out.println("M2 pressed");
        }
        else if(buttonCommand.equals("Calculate Vf"))
            {
            GuiCalc CalcVf = new GuiCalc();

            m1IeStr = m1InElas.getText();
            m1IeNo = Double.parseDouble(m1IeStr);
            v1iIeStr = v1iInElas.getText();
            v1iIeNo = Double.parseDouble(v1iIeStr);
            v2iIeStr = v2iInElas.getText();
            v2iIeNo = Double.parseDouble(v2iIeStr);
            m2IeStr = m2InElas.getText();
            m2IeNo = Double.parseDouble(m2IeStr);

            vfIeNo = CalcVf.CalcInelasVf(m1IeNo, v1iIeNo, v2iIeNo, m2IeNo, v1iIeNo);
            vfIeStr = Double.toString(vfIeNo);


            JTable inelasTableVf = new JTable(inelasData,inelasTitles);
            JScrollPane inelasScrollPanelVf = new JScrollPane(inelasTableVf);
            inelasTableVf.setFillsViewportHeight(true);
            inelasTableVf.setBackground(Color.RED);
            inelasticFrame.add(inelasScrollPanelVf, BorderLayout.SOUTH);

            VFInElas = vfIeNo;

            inelasticFrame.validate();
            inelasticFrame.repaint();
            System.out.println("Vf pressed");
            }
            else if(buttonCommand.equals("Go Home"))
            {
                inelasticFrame.dispose();
                setGui();
            }

            if(buttonCommand.equals("Elastic Collision"))
                {
                    welcomeFrame.dispose();
                    setElas();
                }
            else if(buttonCommand.equals("Calculate V1f"))
                {
                    GuiCalc V1FECalc = new GuiCalc();
                    /*
                    double e3Elas;
                    double e4Elas;
                    double e5Elas;
                    double e6Elas;
                    double e7Elas;
                    */
                    v1iEStr = v1iElas.getText();
                    v1iENo = Double.parseDouble(v1iEStr);
                    v2iEStr = v2iElas.getText();
                    v2iENo = Double.parseDouble(v2iEStr);
                    m1EStr = m1Elas.getText();
                    m1ENo = Double.parseDouble(m1EStr);
                    m2EStr = m2Elas.getText();
                    m2ENo = Double.parseDouble(m2EStr);

                    v1fENo = V1FECalc.CalcElasVf(v1iENo, v2iENo, m1ENo, m2ENo);

                    JTable elasTableV1f = new JTable(elasData,elasTitles);
                    JScrollPane elasScrollPanelV1f = new JScrollPane(elasTableV1f);
                    elasTableV1f.setFillsViewportHeight(true);
                    elasTableV1f.setBackground(Color.RED);
                    elasticFrame.add(elasScrollPanelV1f, BorderLayout.SOUTH);

                    V1FElas = v1fENo;

                    elasticFrame.validate();
                    elasticFrame.repaint();
                    System.out.println("Vf1 pressed");
                }
                else if(buttonCommand.equals("Return Home"))
                    {
                        elasticFrame.dispose();
                        setGui();
                    }

                    if(buttonCommand.equals("Current"))
                        {
                            welcomeFrame.dispose();
                            setCurrent();
                        }
                else if(buttonCommand.equals("Calculate Current C"))
                    {
                        GuiCalc currentCCalc = new GuiCalc();

                        voltageCStr = voltageCur.getText();
                        voltageCNo = Double.parseDouble(voltageCStr);
                        powerCStr = powerCur.getText();
                        powerCNo = Double.parseDouble(powerCStr);

                        currentCNo = currentCCalc.CalcCurrentC(voltageCNo, powerCNo);

                        currentFrame.validate();
                        currentFrame.repaint();
                        System.out.println("Calc Current for Current was pressed");
                    }
                    else if(buttonCommand.equals("Calculate Power C"))
                    {
                        GuiCalc powerCCalc = new GuiCalc();

                        voltageCStr = voltageCur.getText();
                        voltageCNo = Double.parseDouble(voltageCStr);
                        currentCStr = currentCur.getText();
                        currentCNo = Double.parseDouble(currentCStr);

                        powerCNo = powerCCalc.CalcCurrentP(voltageCNo, currentCNo);

                        currentFrame.validate();
                        currentFrame.repaint();
                        System.out.println("Calc Power for Current was pressed");
                    }
                    else if(buttonCommand.equals("Calculate Voltage C"))
                    {
                        currentCStr = currentCur.getText();
                        currentCNo = Double.parseDouble(currentCStr);
                        powerCStr = powerCur.getText();
                        powerCNo = Double.parseDouble(powerCStr);

                        GuiCalc currentCCalc = new GuiCalc();

                        voltageCNo = currentCCalc.CalcCurrentV(currentCNo, powerCNo);

                        currentFrame.validate();
                        currentFrame.repaint();
                        System.out.println("Calc Voltage for Current was pressed");
                    }
                    else if(buttonCommand.equals("Exit Home"))
                    {
                        currentFrame.dispose();
                        setGui();
                    }
                    if(buttonCommand.equals("Resistance"))
                        {
                            welcomeFrame.dispose();
                            setResistance();
                        }
                        else if(buttonCommand.equals("Calculate Current R"))
                        {
                            GuiCalc currentRCalc = new GuiCalc();

                            resistanceRStr = currentRes.getText();
                            resistanceRNo = Double.parseDouble(resistanceRStr);
                            voltageRStr = voltageRes.getText();
                            voltageRNo = Double.parseDouble(voltageRStr);

                            currentRNo = currentRCalc.CalcResistanceC(resistanceRNo, voltageRNo);

                            resFrame.validate();
                            resFrame.repaint();
                            System.out.println("Calc Current for res was clicked");
                        }
                        else if(buttonCommand.equals("Calculate Resistance R"))
                        {
                            GuiCalc resistanceRCalc = new GuiCalc();

                            currentRStr = currentRes.getText();
                            currentRNo = Double.parseDouble(currentRStr);
                            voltageRStr = voltageRes.getText();
                            voltageRNo = Double.parseDouble(voltageRStr);

                            resistanceRNo = resistanceRCalc.CalcResistanceC(currentRNo, voltageRNo);

                            resFrame.validate();
                            resFrame.repaint();
                            System.out.println("Calc Resistance for res was clicked");
                        }
                        else if(buttonCommand.equals("Calculate Voltage R"))
                        {
                            GuiCalc voltageRCalc = new GuiCalc();

                            currentRStr = currentRes.getText();
                            currentRNo = Double.parseDouble(currentRStr);
                            resistanceRStr = resistanceRes.getText();
                            resistanceRNo = Double.parseDouble(resistanceRStr);

                            voltageRNo = voltageRCalc.CalcResistanceV(currentRNo, resistanceRNo);

                            resFrame.validate();
                            resFrame.repaint();
                            System.out.println("Calc Voltage for res was clicked");
                        }
                        else if(buttonCommand.equals("Home"))
                            {
                                resFrame.dispose();
                                setGui();
                            }
                //add tool tip text
                // add help menu 
    }

    public static void main(String [] args)
    {
        GuiInter mainGui = new GuiInter();
        mainGui.setGui();
    }
}   

0 个答案:

没有答案