Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help me with my GUI!

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me with my GUI!

    Guys please help me on my program, this is a GUI java program, and I'm having problem every time I run it. It doesn't give me any output on every given I enter, it got an error always. This is a Confidence Interval of know variance, that's the title of my program.. I want it to give me an output every time I press the calculate button. Here's the code..

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class MyProgram2 extends JFrame implements ActionListener{
        JLabel heading = new JLabel("Given:");
        JLabel sSize = new JLabel("Sample Size");
        JLabel sMean = new JLabel("Sample Mean");
        JLabel var = new JLabel("Variance"); 
        JLabel ConfInt = new JLabel("Confidence Interval");
        JLabel ans = new JLabel("Two sided");
     
        JTextField textA = new JTextField(7);
        JTextField textB = new JTextField(7);
        JTextField textC = new JTextField(7);
        JTextField textD = new JTextField(7);
        JTextField textE = new JTextField(7);
        JTextField textF = new JTextField(7);
        JTextField ConfInttext = new JTextField(7);
        JTextField resultA = new JTextField(7);
        JTextField resultB = new JTextField(7);
     
        JButton calculate = new JButton("Calculate");
     
        JPanel hedpanel = new JPanel();
        JPanel row1 = new JPanel();
        JPanel row2 = new JPanel();
        JPanel row3 = new JPanel();
        JPanel row4 = new JPanel();
        JPanel result = new JPanel();
        JPanel butpanel = new JPanel();
        double a;
        double b; 
        double c;
        double d;
        double e; 
        double f; 
        double g; 
        double finalans; 
        double h;
        double i; 
        double j;
        double k;
        double l;
        double m;
        double n;
        double finalans2;
     
        public MyProgram2(){
            setTitle("Confidence Interval of two Population Mean");
            resultA.setEditable(false);
            resultB.setEditable(false);
            setLayout(new FlowLayout());
     
            //Add component to Panel
     
            hedpanel.add(heading);
     
            row1.add(sSize);
            row1.add(textA);
            row1.add(textB);
     
            row2.add(sMean);
            row2.add(textC);
            row2.add(textD);
     
            row3.add(var);
            row3.add(textE);
            row3.add(textF);
            row4.add(ConfInt);
            row4.add(ConfInttext);
     
            result.add(ans);
            result.add(resultA);
            result.add(resultB);
     
            butpanel.add(calculate);
     
            //Frame
            add(hedpanel);
            add(row1);
            add(row2);
            add(row3);
            add(row4);
            add(result);
            add(calculate);      
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            calculate.addActionListener(this);
        }
            //the Application
            public void calcCIL(){
                finalans = (c-d)-g*Math.sqrt((Math.pow(e,2)/a)+(Math.pow(f,2)/b));
            }
            public void calcCIR(){
                finalans2 = (j-k)+n*Math.sqrt((Math.pow(l,2)/h)+(Math.pow(m,2)/i));
            }
            public void actionPerformed(ActionEvent evt){
                String userIn;
                userIn = textA.getText();
                a = Integer.parseInt(userIn);
                h = Integer.parseInt(userIn);
     
                userIn = textB.getText();
                b = Integer.parseInt(userIn);
                i = Integer.parseInt(userIn);
     
                userIn = textC.getText();
                c = Integer.parseInt(userIn);
                j = Integer.parseInt(userIn);
     
                userIn = textD.getText();
                d = Integer.parseInt(userIn);
                k = Integer.parseInt(userIn);
     
                userIn = textE.getText();
                e = Integer.parseInt(userIn);
                l = Integer.parseInt(userIn);
     
                userIn = textF.getText();
                f = Integer.parseInt(userIn);
                m = Integer.parseInt(userIn);
     
                userIn = ConfInt.getText();
                g = Integer.parseInt(userIn);
                n = Integer.parseInt(userIn);
     
                resultA.setText(finalans+"");
                repaint();
                calcCIL();
                resultB.setText(finalans2+"");
                repaint();
                calcCIR();
     
            }
            public static void main(String[] args){
                MyProgram2 prog = new MyProgram2();
                prog.setSize(350, 290);
                prog.setResizable(false);
                prog.setVisible(true);
     
            }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Please help me with my GUI!

    Hello decgaid06. Welcome to the Java Programming Forums.

    I have compiled this code and it runs fine. The issue is when the Calculate button is pressed.
    It looks like it is falling over with multiple java.lang.NumberFormatException's

    You need to look at

                String userIn;
                userIn = textA.getText();
                a = Integer.parseInt(userIn);
                h = Integer.parseInt(userIn);
     
                userIn = textB.getText();
                b = Integer.parseInt(userIn);
                i = Integer.parseInt(userIn);
     
                userIn = textC.getText();
                c = Integer.parseInt(userIn);
                j = Integer.parseInt(userIn);
     
                userIn = textD.getText();
                d = Integer.parseInt(userIn);
                k = Integer.parseInt(userIn);
     
                userIn = textE.getText();
                e = Integer.parseInt(userIn);
                l = Integer.parseInt(userIn);
     
                userIn = textF.getText();
                f = Integer.parseInt(userIn);
                m = Integer.parseInt(userIn);
     
                userIn = ConfInt.getText();
                g = Integer.parseInt(userIn);
                n = Integer.parseInt(userIn);
     
                resultA.setText(finalans+"");
                repaint();
                calcCIL();
                resultB.setText(finalans2+"");
                repaint();
                calcCIR();

    a, b, c, d, e... are all declared as double variables.

    I would try updating the code to:

                userIn = textA.getText();
                a = Double.parseDouble(userIn);
                h = Double.parseDouble(userIn);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers