I am going to make a calculator and I am having some Issues arranging the buttons. You should be able to see the layout when you run the code. Some clarification though there will be a blank spot under the "3" for a "+/-" button (switches between positive and negative). Here is my code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calc implements ActionListener{
JTextField disp;
double total;
String number;
public Calc (){
JFrame f = new JFrame ("Calculator"); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel (new GridBagLayout ());
GridBagConstraints c = new GridBagConstraints ();
disp = new JTextField (25);disp.setText ("0");
c.gridx = 1;
c.gridy = 1;
c.gridheight = 1;
c.gridwidth = 17;
pane.add (disp,c);
JButton btn0 = new JButton ("0"); btn0.addActionListener(this); btn0.setActionCommand ("0");
c.gridheight = 4;
c.gridwidth = 8;
c.gridx = 1;
c.gridy = 17;
pane.add (btn0,c);
JButton btn1 = new JButton ("1"); btn1.addActionListener(this); btn1.setActionCommand ("1");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 13;
pane.add (btn1, c);
JButton btn2 = new JButton ("2"); btn2.addActionListener(this); btn2.setActionCommand ("2");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 5;
c.gridy = 13;
pane.add (btn2,c);
JButton btn3 = new JButton ("3"); btn3.addActionListener(this); btn3.setActionCommand ("3");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 9;
c.gridy = 13;
pane.add (btn3,c);
JButton btn4 = new JButton ("4"); btn4.addActionListener(this); btn4.setActionCommand ("4");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 9;
pane.add (btn4,c);
JButton btn5 = new JButton ("5"); btn5.addActionListener(this); btn5.setActionCommand ("5");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 5;
c.gridy = 9;
pane.add (btn5,c);
JButton btn6 = new JButton ("6"); btn6.addActionListener(this); btn6.setActionCommand ("6");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 9;
c.gridy = 9;
pane.add (btn6,c);
JButton btn7 = new JButton ("7"); btn7.addActionListener(this); btn7.setActionCommand ("7");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 1;
c.gridy = 5;
pane.add (btn7,c);
JButton btn8 = new JButton ("8"); btn8.addActionListener(this); btn8.setActionCommand ("8");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 5;
c.gridy = 5;
pane.add (btn8,c);
JButton btn9 = new JButton ("9"); btn9.addActionListener(this); btn9.setActionCommand ("9");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 9;
c.gridy = 5;
pane.add (btn9,c);
JButton btnPlus = new JButton ("+"); btnPlus.addActionListener(this); btnPlus.setActionCommand ("+");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 13;
c.gridy = 13;
pane.add (btnPlus,c);
JButton btnMin = new JButton ("-"); btnMin.addActionListener(this); btnMin.setActionCommand ("-");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 17;
c.gridy = 13;
pane.add (btnMin,c);
JButton btnMult = new JButton ("x"); btnMult.addActionListener(this); btnMult.setActionCommand ("*");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 13;
c.gridy = 9;
pane.add (btnMult,c);
JButton btnDiv = new JButton ("/"); btnDiv.addActionListener(this); btnDiv.setActionCommand ("/");
c.gridheight = 4;
c.gridwidth = 4;
c.gridx = 17;
c.gridy = 9;
pane.add (btnDiv,c);
JButton btnEq = new JButton ("="); btnEq.addActionListener(this); btnEq.setActionCommand ("=");
c.gridheight = 4;
c.gridwidth = 8;
c.gridx = 13;
c.gridy = 17;
pane.add (btnEq,c);
JButton btnClr = new JButton ("CE"); btnClr.addActionListener(this); btnClr.setActionCommand ("clear");
c.gridheight = 4;
c.gridwidth = 8;
c.gridx = 13;
c.gridy = 5;
pane.add (btnClr,c);
f.add (pane);
f.setSize (300,350);
f.setVisible (true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calc c = new Calc ();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("1")){
System.out.println ("1 Pressed");
}
}
}
Please tell me how to fix this issue.
Thanks for any help.