This app has standard calculator GUI interface.

The app source is as follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calc2
{
public static void main(String[] args)
{
SwingApp app = new SwingApp(250, 250); // set app window size
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SwingApp extends JFrame implements ActionListener
{
private final DrawPanel panel; // class variable
private final JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
private final JButton add, sub, mul, div, equ, dot; // operation buttons
private final JTextField tf; // text field
private String op; // storage for operation
private double n1, n2; // storage for operands
public SwingApp(int width, int height) // class constructor
{
super(); // call to super class constructor
Container pane = super.getContentPane(); // create layout, set colors
panel = new DrawPanel(); // add GUI components
panel.setLayout(new BorderLayout(10, 10));
panel.setBackground(Color.yellow);
Font font = new Font("Arial", Font.BOLD, 24); // text field font
panel.setBackground(new Color(3, 163, 227));
tf = new JTextField("0"); // text field init
tf.setFont(font);
tf.setHorizontalAlignment(JTextField.RIGHT);
panel.add(tf, BorderLayout.NORTH);
JPanel jp = new JPanel(); // JPanel with buttons
jp.setLayout(new GridLayout(4, 4, 10, 10));
b7 = new JButton("7"); // create buttons
b8 = new JButton("8");
b9 = new JButton("9");
add = new JButton("+");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
sub = new JButton("-");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
mul = new JButton("*");
b0 = new JButton("0");
dot = new JButton(".");
equ = new JButton("=");
div = new JButton("/");
jp.add(b7); // add buttons to the panel
jp.add(b8);
jp.add(b9);
jp.add(add);
jp.add(b4);
jp.add(b5);
jp.add(b6);
jp.add(sub);
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(mul);
jp.add(b0);
jp.add(dot);
jp.add(equ);
jp.add(div);
jp.setBackground(Color.ORANGE);
panel.add(jp, BorderLayout.CENTER);
b0.addActionListener(this); // register buttons for
b1.addActionListener(this); // the event listener
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
equ.addActionListener(this);
dot.addActionListener(this);
pane.add(panel);
Toolkit toolkit = Toolkit.getDefaultToolkit(); // optionally position JFrame
Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen
super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);
super.setTitle("Swing app"); // set desired window title
super.setSize(width, height); // set desired window size
super.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == add || e.getSource() == sub || // process operation
e.getSource() == mul || e.getSource() == div) // button
{
n1 = Double.parseDouble(tf.getText()); // get 1st operand
tf.setText("0"); // zero the text field
op = ((JButton) e.getSource()).getText(); // save operation
}
else if (e.getSource() == equ) // the "=" button processing
{
n2 = Double.parseDouble(tf.getText()); // get 2nd operand
String result = "";
if (op.equals("+")) // process "+" button
{
result = (n1 + n2) + ""; // compute the result
if (result.charAt(result.length()-2) == '.' && // get rid of tailing
result.charAt(result.length()-1) == '0') // .0 for integer numbers
result = result.substring(0, result.length()-2);
tf.setText(result); // show the result in text field
}
else if (op.equals("-")) // similarly process the "-"
{
result = (n1 - n2) + "";
if (result.charAt(result.length()-2) == '.' &&
result.charAt(result.length()-1) == '0')
result = result.substring(0, result.length()-2);
tf.setText(result);
}
else if (op.equals("*")) // process the "*" operation
{
result = (n1 * n2) + "";
if (result.charAt(result.length()-2) == '.' &&
result.charAt(result.length()-1) == '0')
result = result.substring(0, result.length()-2);
tf.setText(result);
}
else if (op.equals("/")) // process the "/" operation
{
result = (n1 / n2) + "";
if (result.charAt(result.length()-2) == '.' &&
result.charAt(result.length()-1) == '0')
result = result.substring(0, result.length()-2);
tf.setText(result);
}
}
else // process the digit and "."
{ // buttons
String dig = ((JButton) e.getSource()).getText(); // get digit
String num = tf.getText(); // get text in text field
if (num.equals("0")) // append the pressed
num = dig; // digit to the number in
else // text field
num += dig;
tf.setText(num);
}
}
}
class DrawPanel extends JPanel // main window panel
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // must be the 1st line
// add here more code for drawing on panel
}
}
|