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

public class Calc extends JFrame implements ActionListener{
        
        JFrame frame;
        JPanel textfield1,textfield2,rowpanel,calcb,mainpanel;
        JTextField display;
        JButton[] b;
        
    public Calc(){
        frame=new JFrame("Calculator");
        textfield1=new JPanel();
        textfield2=new JPanel();
 
        display = new JTextField(16);
        mainpanel = new JPanel();

        textfield1.setLayout(new GridLayout(4,6));
        textfield1.add(display);
        textfield2.add(textfield1);

            //array
            String[] cbuttons = {"7","8", "9","/", "4", "5", "6","*", "1", "2", "3","+", "0", ".", "=","-"};
            b = new JButton[16];
            rowpanel= new JPanel();
            rowpanel.setLayout(new GridLayout(4,4));

            for(int c=0;c<16;c++)
            {
                	b[c] = new JButton(cbuttons[c]);
			//b[c].addActionListener(this);
			rowpanel.add(b[c]);

            }

            //calcb.setLayout(new GridLayout(4,4));
           /* for(int c=0;c<=3;c++)
            {
                	b[c] = new JButton(cbuttons[c]);
			//b[c].addActionListener(this);
			row1panel.add(b[c]);

            }
            for(int c=4;c<=7;c++)
            {
                	b[c] = new JButton(cbuttons[c]);
			//b[c].addActionListener(this);
			row2panel.add(b[c]);

            }
           for(int c=8;c<=11;c++)
            {
                	b[c] = new JButton(cbuttons[c]);
			//b[c].addActionListener(this);
			row3panel.add(b[c]);

            }
           for(int c=12;c<=15;c++)
            {
                	b[c] = new JButton(cbuttons[c]);
			//b[c].addActionListener(this);
			row4panel.add(b[c]);

            }*/

        mainpanel.setLayout(new GridLayout(5,1));
        mainpanel.add(textfield2);
        mainpanel.add(rowpanel);
        frame.setContentPane(mainpanel);
        frame.setSize(200,200);
        frame.show();
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        Calc x = new Calc();
    }
}

How can I Resize the BUTTONS?
It won't run because of

public class Calc extends JFrame implements ActionListener

Help.
Thanks :icon_confused:

Recommended Answers

All 2 Replies

Replace

// frame.setSize(200, 200);
        frame.pack();

now debug program.
why you have so much space?

What do you mean it won't run because of "public class Calc extends JFrame implements ActionListener"? It looks to me like that is a correct class header, although I haven't written a Java program in awhile. What is the error message the compiler is giving you?

Also, you declared your class as extending JFrame, but then you have a JFrame variable and the first line in the constructor is "frame=new JFrame("Calculator");" That means that the Object itself is-a JFrame, but it also has-a JFrame. In other words, "Calc" itself is a JFrame (because it extends JFrame), but then you created another JFrame (with your frame variable). So you have two JFrames - you can get rid of the variable "frame".

If neither of those two explanations made sense, try deleting your frame variable, and wherever it says "frame.anything", replace the "frame" with "this".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.