HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

First 4-function Swing calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
swingfirstfunctioncalculator

Problem

This is for a high school class I'm taking. Any tips/improvements?

```
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JavaCalculator
{
public static boolean isMemorized;
public static String memory;

public static void main (String[] args)
{
JTextField answerField = new JTextField();
answerField.setBounds (6, 6, 280, 48);
answerField.setFont (new Font ("Arial", Font.PLAIN, 26));
answerField.setColumns (1);
answerField.setHorizontalAlignment (JTextField.RIGHT);
answerField.setEditable (false);

JButton button_one = new JButton ("1");
button_one.setBounds (6, 92, 70, 38);
button_one.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "1");
}
});

JButton button_two = new JButton ("2");
button_two.setBounds (76, 92, 70, 38);
button_two.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "2");
}
});

JButton button_three = new JButton ("3");
button_three.setBounds (146, 92, 70, 38);
button_three.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
answerField.setText (answerField.getText() + "3");
}
});

JButton button_four = new JButton ("4");
button_four.setBounds (6, 128, 70, 38);
button_four.addActionListener (new ActionL

Solution

Avoid copy-paste coding

An easy first step is to generalize this action listener:

button_two.addActionListener (new ActionListener()
{
    public void actionPerformed (ActionEvent event)
    {
        answerField.setText (answerField.getText() + "2");
    }
});


For example by creating a dedicated inner class for this:

private class NumberActionListener implements ActionListener {
    private static final label;

    public NumberActionListener(String label) {
        this.label = label;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        answerField.setText(answerField.getText() + label);
    }
}


Using this class, you can simplify the addActionListener calls like this:

button_two.addActionListener(new NumberActionListener("2"));


On closer look, I see that the action listeners of the operators do almost the same thing as the number buttons, but using some padding with space.
You could improve on the NumberActionListener to handle that case too.

Find a formula to replace hardcoded numbers

The positioning of the buttons clearly follows some kind of logic,
but the values are hardcoded, for example:

button_one.setBounds (6, 92, 70, 38);
    button_two.setBounds (76, 92, 70, 38);
    button_three.setBounds (146, 92, 70, 38);
    button_four.setBounds (6, 128, 70, 38);
    button_five.setBounds (76, 128, 70, 38);
    button_six.setBounds (146, 128, 70, 38);


As it is, this is a maintenance nightmare.
If you want to resize the buttons,
or increase padding in between,
you'll have to recalculate and change in many places.
It would be better to come up with a formula,
and position these buttons using a loop.

Code Snippets

button_two.addActionListener (new ActionListener()
{
    public void actionPerformed (ActionEvent event)
    {
        answerField.setText (answerField.getText() + "2");
    }
});
private class NumberActionListener implements ActionListener {
    private static final label;

    public NumberActionListener(String label) {
        this.label = label;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        answerField.setText(answerField.getText() + label);
    }
}
button_two.addActionListener(new NumberActionListener("2"));
button_one.setBounds (6, 92, 70, 38);
    button_two.setBounds (76, 92, 70, 38);
    button_three.setBounds (146, 92, 70, 38);
    button_four.setBounds (6, 128, 70, 38);
    button_five.setBounds (76, 128, 70, 38);
    button_six.setBounds (146, 128, 70, 38);

Context

StackExchange Code Review Q#88490, answer score: 6

Revisions (0)

No revisions yet.