Hi,

I'm trying to make a simple calculator. I don't know how to make the display screen and my buttons are not aligned properly.

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

public class Calculator extends JFrame{

	private JButton btn0 , btn1, btn2, btn3, btn4, btn5, btn6,
        btn7, btn8, btn9, btnDecimal, btnPlus, btnMinus, btnMultiply, 
        btnDivide, btnEquals, btnCE;
	private JPanel calculatorPanel = new JPanel();
	private JPanel mainPanel;
	private Container c;
	
	
	 public Calculator() {

    	setTitle("Calculator");
    	setSize(250, 250);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension minSize = new Dimension(250,250);
		setMinimumSize(minSize);
		setLocationRelativeTo(null);
    	//setLocation()
		btnDecimal = new JButton(" . ");
		btnEquals = new JButton(" = ");
		btnCE = new JButton("CE");
    	btn0 = new JButton (" 0 ");
		btn1 = new JButton(" 1 ");
		btn2 = new JButton(" 2 ");
		btn3 = new JButton(" 3 ");
		btn4 = new JButton(" 4 ");
		btn5 = new JButton(" 5 ");
		btn6 = new JButton(" 6 ");
		btn7 = new JButton(" 7 ");
		btn8 = new JButton(" 8 ");
		btn9 = new JButton(" 9 ");
		btnPlus = new JButton("+");
		btnMinus = new JButton("-");
		btnMultiply = new JButton("*");
		btnDivide = new JButton("/");
                c = getContentPane();
		setVisible(true);
		
		JPanel mainPanel = new JPanel(new GridBagLayout());
		GridBagConstraints d = new GridBagConstraints();
    	//mainPanel = new JPanel()
		
		d.gridx = 2;
		d.gridy = 1;
    	mainPanel.add(btn1, d);
		d.gridx = 2;
		d.gridy = 2;
		mainPanel.add(btn4, d);
		d.gridx = 2;
		d.gridy = 3;
		mainPanel.add(btn7, d);
		d.gridx = 3;
		d.gridy = 1;
		mainPanel.add(btn2, d);
		d.gridx = 3;
		d.gridy = 2;
		mainPanel.add(btn5, d);
		d.gridx = 3;
		d.gridy = 3;
		mainPanel.add(btn8, d);
		d.gridx = 4;
		d.gridy = 1;
		mainPanel.add(btn3, d);
		d.gridx = 4;
		d.gridy = 2;
		mainPanel.add(btn6, d);
		d.gridx = 4;
		d.gridy = 3;
		mainPanel.add(btn9, d);
		d.gridx = 2;
		d.gridy = 4;
		mainPanel.add(btnDecimal, d);
		d.gridx = 3;
		d.gridy = 4;
		mainPanel.add(btn0, d);
		d.gridx = 4;
		d.gridy = 4;
		mainPanel.add(btnCE, d);
		
		
		
		//d/.gridx = 5;
		//d.gridy = 1;
		//mainPanel.add(btnPlus, d);
		//d.gridx = 5;
		//d.gridy = 2;
		//mainPanel.add(btnMinus, d);
		d.gridx = 5;
		d.gridy = 3;
		//mainPanel.add(btnMultiply, d);
		//d.gridx = 6;
		//d/.gridy = 1;
		//mainPanel.add(btnDivide, d);
		//d.gridx = 6;
		//d.gridy = 2;
		
		c.add(mainPanel);

    	setVisible(true);
		}
		
	public static void main(String[ ] args) {
	
	Calculator cl = new Calculator();
	}


}

have you tried to use different layouts? on the swing objects there are a lot of methods that can help you out, for instance the setBounds.
but how they work will depend on wheter or not you use the right layoutmanager.

Perhaps specifying a size for the GridBagLayout would help. Also, calling the pack() method before the setVisible(true) might also be useful? Its been awhile since I worked with Swing and I don't have the JRE set up right now on this computer to test, but I believe this is where to start looking.

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.