init()
method that lays out the calculator applet.
void init () {
GridBagLayout gbl = new GridBagLayout();
this.setLayout(gbl);
// Add the display to the top four cells
GridBagConstraints displayConstraints = new GridBagConstraints();
displayConstraints.gridx = 0;
displayConstraints.gridy = 0;
displayConstraints.gridwidth = 4;
displayConstraints.gridheight = 1;
displayConstraints.fill = GridBagConstraints.HORIZONTAL;
// add the text field
TextField display = new TextField(12);
gbl.setConstraints(display, displayConstraints);
this.add(display);
// Add the clear button
GridBagConstraints clearConstraints = new GridBagConstraints();
clearConstraints.gridx = 0;
clearConstraints.gridy = 1;
clearConstraints.gridwidth = 1;
clearConstraints.gridheight = 1;
clearConstraints.fill = GridBagConstraints.BOTH;
clearConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button clear = new Button("C");
gbl.setConstraints(clear, clearConstraints);
this.add(clear);
// Add the equals button
GridBagConstraints equalsConstraints = new GridBagConstraints();
equalsConstraints.gridx = 1;
equalsConstraints.gridy = 1;
equalsConstraints.gridwidth = 1;
equalsConstraints.gridheight = 1;
equalsConstraints.fill = GridBagConstraints.BOTH;
equalsConstraints.insets = new Insets(3, 3, 3, 3);
// add the = button
Button equals = new Button("=");
gbl.setConstraints(equals, equalsConstraints);
this.add(equals);
// Add the / button
GridBagConstraints slashConstraints = new GridBagConstraints();
slashConstraints.gridx = 2;
slashConstraints.gridy = 1;
slashConstraints.gridwidth = 1;
slashConstraints.gridheight = 1;
slashConstraints.fill = GridBagConstraints.BOTH;
slashConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button slash = new Button("/");
gbl.setConstraints(slash, slashConstraints);
this.add(slash);
// Add the * button
GridBagConstraints timesConstraints = new GridBagConstraints();
timesConstraints.gridx = 3;
timesConstraints.gridy = 1;
timesConstraints.gridwidth = 1;
timesConstraints.gridheight = 1;
timesConstraints.fill = GridBagConstraints.BOTH;
timesConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button star = new Button("*");
gbl.setConstraints(star, timesConstraints);
this.add(star);
// Add the 7 key
GridBagConstraints sevenConstraints = new GridBagConstraints();
sevenConstraints.gridx = 0;
sevenConstraints.gridy = 2;
sevenConstraints.gridwidth = 1;
sevenConstraints.gridheight = 1;
sevenConstraints.fill = GridBagConstraints.BOTH;
sevenConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b7 = new Button("7");
gbl.setConstraints(b7, sevenConstraints);
this.add(b7);
// Add the 8 key
GridBagConstraints eightConstraints = new GridBagConstraints();
eightConstraints.gridx = 1;
eightConstraints.gridy = 2;
eightConstraints.gridwidth = 1;
eightConstraints.gridheight = 1;
eightConstraints.fill = GridBagConstraints.BOTH;
eightConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b8 = new Button("8");
gbl.setConstraints(b8, eightConstraints);
this.add(b8);
// Add the 9 key
GridBagConstraints nineConstraints = new GridBagConstraints();
nineConstraints.gridx = 2;
nineConstraints.gridy = 2;
nineConstraints.gridwidth = 1;
nineConstraints.gridheight = 1;
nineConstraints.fill = GridBagConstraints.BOTH;
nineConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b9 = new Button("9");
gbl.setConstraints(b9, nineConstraints);
this.add(b9);
// Add the - key
GridBagConstraints minusConstraints = new GridBagConstraints();
minusConstraints.gridx = 3;
minusConstraints.gridy = 2;
minusConstraints.gridwidth = 1;
minusConstraints.gridheight = 1;
minusConstraints.fill = GridBagConstraints.BOTH;
minusConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button minus = new Button("-");
gbl.setConstraints(minus, minusConstraints);
this.add(minus);
// Add the 4 key
GridBagConstraints fourConstraints = new GridBagConstraints();
fourConstraints.gridx = 0;
fourConstraints.gridy = 3;
fourConstraints.gridwidth = 1;
fourConstraints.gridheight = 1;
fourConstraints.fill = GridBagConstraints.BOTH;
fourConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b4 = new Button("4");
gbl.setConstraints(b4, fourConstraints);
this.add(b4);
// Add the 5 key
GridBagConstraints fiveConstraints = new GridBagConstraints();
fiveConstraints.gridx = 1;
fiveConstraints.gridy = 3;
fiveConstraints.gridwidth = 1;
fiveConstraints.gridheight = 1;
fiveConstraints.fill = GridBagConstraints.BOTH;
fiveConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b5 = new Button("5");
gbl.setConstraints(b5, fiveConstraints);
this.add(b5);
// Add the 6 key
GridBagConstraints sixConstraints = new GridBagConstraints();
sixConstraints.gridx = 2;
sixConstraints.gridy = 3;
sixConstraints.gridwidth = 1;
sixConstraints.gridheight = 1;
sixConstraints.fill = GridBagConstraints.BOTH;
sixConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b6 = new Button("6");
gbl.setConstraints(b6, sixConstraints);
this.add(b6);
// Add the + key
GridBagConstraints plusConstraints = new GridBagConstraints();
plusConstraints.gridx = 3;
plusConstraints.gridy = 3;
plusConstraints.gridwidth = 1;
plusConstraints.gridheight = 1;
plusConstraints.fill = GridBagConstraints.BOTH;
plusConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button plus = new Button("+");
gbl.setConstraints(plus, plusConstraints);
this.add(plus);
// Add the 1 key
GridBagConstraints oneConstraints = new GridBagConstraints();
oneConstraints.gridx = 0;
oneConstraints.gridy = 4;
oneConstraints.gridwidth = 1;
oneConstraints.gridheight = 1;
oneConstraints.fill = GridBagConstraints.BOTH;
oneConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b1 = new Button("1");
gbl.setConstraints(b1, oneConstraints);
this.add(b1);
// Add the 2 key
GridBagConstraints twoConstraints = new GridBagConstraints();
twoConstraints.gridx = 1;
twoConstraints.gridy = 4;
twoConstraints.gridwidth = 1;
twoConstraints.gridheight = 1;
twoConstraints.fill = GridBagConstraints.BOTH;
twoConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b2 = new Button("2");
gbl.setConstraints(b2, twoConstraints);
add(b2);
// Add the 3 key
GridBagConstraints threeConstraints = new GridBagConstraints();
threeConstraints.gridx = 2;
threeConstraints.gridy = 4;
threeConstraints.gridwidth = 1;
threeConstraints.gridheight = 1;
threeConstraints.fill = GridBagConstraints.BOTH;
threeConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b3 = new Button("3");
gbl.setConstraints(b3, threeConstraints);
this.add(b3);
// Add the = key
GridBagConstraints bigEqualsConstraints = new GridBagConstraints();
bigEqualsConstraints.gridx = 3;
bigEqualsConstraints.gridy = 4;
bigEqualsConstraints.gridwidth = 1;
bigEqualsConstraints.gridheight = 2;
bigEqualsConstraints.fill = GridBagConstraints.BOTH;
bigEqualsConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button bigEquals = new Button("=");
gbl.setConstraints(bigEquals, bigEqualsConstraints);
this.add(bigEquals);
// Add the 0 key
GridBagConstraints zeroConstraints = new GridBagConstraints();
zeroConstraints.gridx = 0;
zeroConstraints.gridy = 5;
zeroConstraints.gridwidth = 2;
zeroConstraints.gridheight = 1;
zeroConstraints.fill = GridBagConstraints.BOTH;
zeroConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button b0 = new Button("0");
gbl.setConstraints(b0, zeroConstraints);
this.add(b0);
// Add the . key
GridBagConstraints decimalConstraints = new GridBagConstraints();
decimalConstraints.gridx = 2;
decimalConstraints.gridy = 5;
decimalConstraints.gridwidth = 1;
decimalConstraints.gridheight = 1;
decimalConstraints.fill = GridBagConstraints.BOTH;
decimalConstraints.insets = new Insets(3, 3, 3, 3);
// add the button
Button bdecimal = new Button(".");
gbl.setConstraints(bdecimal, decimalConstraints);
this.add(bdecimal);
}
The full source code just contains a few additional methods to handle the window.