Java Reference
In-Depth Information
import
javax.swing.*;
import
java.awt.*;
import
java.awt.event.*;
import
java.io.*;
public class
Calculator
extends
JFrame
implements
ActionListener
{
private
JTextField displayText =
new
JTextField(30);
private
JButton[] button =
new
JButton[16];
private
String[] keys = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"};
private
String numStr1 = "";
private
String numStr2 = "";
private char
op;
private boolean
firstInput =
true
;
public
Calculator()
{
setTitle("My Calculator");
setSize(230, 200);
Container pane = getContentPane();
pane.setLayout(
null
);
displayText.setSize(200,30);
displayText.setLocation(10,10);
pane.add(displayText);
1
1
int
x, y;
x = 10;
y = 40;
for
(
int
ind = 0; ind < 16; ind++)
{
button[ind] =
new
JButton(keys[ind]);
button[ind].addActionListener(
this
);
button[ind].setSize(50,30);
button[ind].setLocation(x, y);
pane.add(button[ind]);
x = x + 50;
if
((ind + 1) % 4 == 0)
{
x = 10;
y = y + 30;
}
}
Search WWH ::
Custom Search