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();
}
}