import javax.swing.*;
import java.awt.*;

public class Calculator {
    public static void main(String[] args) {
        new Calculator();
    }
    public Calculator()
    {
        JFrame f = new JFrame(" ");
        JPanel textfieldpanel1 = new JPanel();
        JPanel textfieldpanel2 = new JPanel();
        JPanel row1panel = new JPanel();
        JPanel row2pane1 = new JPanel();
        JPanel row3pane1 = new JPanel();
        JPanel row4pane1 = new JPanel();
        JTextField mainpanel = new JTextField(16);

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b10 = new JButton("0");
        JButton divide = new JButton("/");
        JButton multiply = new JButton("*");
        JButton subtract = new JButton("-");
        JButton add = new JButton("+");
        JButton equals = new JButton("=");
        JButton dot = new JButton(". ");
        JButton clear = new JButton("C");
        textfieldpanel1.setLayout(new GridLayout(1,1));
        textfieldpanel1.add(textfieldpanel1);
        textfieldpanel2.add(textfieldpanel1);

        row1panel.setLayout(new GridLayout(1,3));

        row2panel.setLayout(new GridLayout(1,3));

        row3panel.setLayout(new GridLayout(1,3));

        row4panel.setLayout(new GridLayout(1,3));

        row1panel.add(b7);
        row1panel.add(b8);
        row1panel.add(b9);
        row1panel.add(divide);

        row2panel.add(b4);
        row2panel.add(b5);
        row2panel.add(b6);
        row2panel.add(multiply);
        
        row3panel.add(b1);
        row3panel.add(b2);
        row3panel.add(b3);
        row3panel.add(subtract);

        row4panel.add(b10);
        row4panel.add(dot);
        row4panel.add(clear);
        row4panel.add(add);

            mainpanel.setLayout(new GridLayout(5,1));
            mainpanel.add(textfieldpanel2);
            mainpanel.add(row1panel);
            mainpanel.add(row2panel);
            mainpanel.add(row3panel);
            mainpanel.add(row4panel);
            f.setContentPane(mainpanel);
            f.setSize(200,200);
            f.show();
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

It won't work.
Please help.

theo19, for a start you have confused the names on lines 22 to 24, you have written:

JPanel row2pane1 = new JPanel();
JPanel row3pane1 = new JPanel();
JPanel row4pane1 = new JPanel();

instead of

JPanel row2panel = new JPanel();
JPanel row3panel = new JPanel();
JPanel row4panel = new JPanel();

i.e. you have written row2pane1 instead of row2panel. This file will compile when you change those three lines, but I am guessing you are going to have some more issues with this program, for example line 37 where you add a panel to itself.

Edited 7 Years Ago by majestic0110: n/a

Comments
Nice catch. It took me about 10 seconds of staring to see the difference in the lines.

whew, thanks. i didn't notice that.
still it won't work. -.-
help.

import javax.swing.*;
import java.awt.*;

public class Calculator {
    public static void main(String[] args) {
        new Calculator();
    }
    public Calculator()
    {
        JFrame f = new JFrame(" ");
        JPanel textfieldpanel1 = new JPanel();
        JPanel textfieldpanel2 = new JPanel();
        JPanel row1panel = new JPanel();
        JPanel row2panel = new JPanel();
        JPanel row3panel = new JPanel();
        JPanel row4panel = new JPanel();
        JTextField mainpanel = new JTextField(16);

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton b10 = new JButton("0");
        JButton divide = new JButton("/");
        JButton multiply = new JButton("*");
        JButton subtract = new JButton("-");
        JButton add = new JButton("+");
        JButton equals = new JButton("=");
        JButton dot = new JButton(". ");
        JButton clear = new JButton("C");
        textfieldpanel1.setLayout(new GridLayout(1,1));
        textfieldpanel1.add(textfieldpanel1);
        textfieldpanel2.add(tf);

        row1panel.setLayout(new GridLayout(1,3));

        row2panel.setLayout(new GridLayout(1,3));

        row3panel.setLayout(new GridLayout(1,3));

        row4panel.setLayout(new GridLayout(1,3));

        row1panel.add(b7);
        row1panel.add(b8);
        row1panel.add(b9);
        row1panel.add(divide);

        row2panel.add(b4);
        row2panel.add(b5);
        row2panel.add(b6);
        row2panel.add(multiply);
        
        row3panel.add(b1);
        row3panel.add(b2);
        row3panel.add(b3);
        row3panel.add(subtract);

        row4panel.add(b10);
        row4panel.add(dot);
        row4panel.add(clear);
        row4panel.add(add);

            mainpanel.setLayout(new GridLayout(5,1));
            mainpanel.add(textfieldpanel2);
            mainpanel.add(row1panel);
            mainpanel.add(row2panel);
            mainpanel.add(row3panel);
            mainpanel.add(row4panel);
            f.setContentPane(mainpanel);
            f.setSize(200,200);
            f.show();
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

look at line 37, you are adding a panel to itself. This will cause an Illegal Argument Exception. Comment out line 37 and think about what it is you are trying to do. It will run (maybe not quite as you might expect) when you comment out line 37. Also, on line 76 you invoke method show(), which, as of Java version 1.5 has been deprecated. Please change this line from:

f.show();

to :

f.setVisible(true);

Edited 7 Years Ago by majestic0110: n/a

This article has been dead for over six months. Start a new discussion instead.