patternjavaModerate
Simple Java calculator using Swing
Viewed 0 times
simplejavaswingusingcalculator
Problem
This is a Java calculator I made for my programming class. I know there must be vast room for improvement.
Please bear in mind the fact that this only accepts an integer, then an operator, then another integer, and finally the = button to function correctly. Also please bear in mind the fact that it prints the result using
```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaCalculator
{
public static boolean isA, isB;
public static int a, b, answer;
public static String operator;
public static void main (String[] args)
{
JButton button_one = new JButton ("1");
button_one.setBounds (6, 86, 100, 40);
button_one.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
if (!isA && !isB)
{
a = 1;
isA = true;
System.out.print (a);
}
else if (isA && !isB)
{
b = 1;
isB = true;
System.out.print (" " + b);
}
}
});
JButton button_two = new JButton ("2");
button_two.setBounds (106, 86, 100, 40);
button_two.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
if (!isA && !isB)
{
a = 2;
isA = true;
}
else if (isA && !isB)
{
b = 2;
isB = true;
}
}
});
JButton button_three = new JButton ("3");
button_three.setBounds (206, 86, 100, 40);
button_three.addActionListener (new ActionListener()
{
Please bear in mind the fact that this only accepts an integer, then an operator, then another integer, and finally the = button to function correctly. Also please bear in mind the fact that it prints the result using
System.out.print().```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaCalculator
{
public static boolean isA, isB;
public static int a, b, answer;
public static String operator;
public static void main (String[] args)
{
JButton button_one = new JButton ("1");
button_one.setBounds (6, 86, 100, 40);
button_one.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
if (!isA && !isB)
{
a = 1;
isA = true;
System.out.print (a);
}
else if (isA && !isB)
{
b = 1;
isB = true;
System.out.print (" " + b);
}
}
});
JButton button_two = new JButton ("2");
button_two.setBounds (106, 86, 100, 40);
button_two.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
if (!isA && !isB)
{
a = 2;
isA = true;
}
else if (isA && !isB)
{
b = 2;
isB = true;
}
}
});
JButton button_three = new JButton ("3");
button_three.setBounds (206, 86, 100, 40);
button_three.addActionListener (new ActionListener()
{
Solution
You don't need so many anonymous handlers, would be better to have one just track the source of the event and follow the logic you want. You haven't yet answered whether printing to the console is a requirement... so I'll hold off on expanding this until that's answered.
Also, you can streamline the way you create the buttons with an array, consider using a panel with a layout manager to house the numbers.
All you need to do at the end is
You can do the same thing for the operators, and may use a border to manage distance between them, adding in Border layout with your frame to control overall positioning.
Right now, I have an implementation that loops through an enum set with the operators and uses a lambda expression within to compute, but you've yet to answer my question about whether or not this has to be implemented on the console, so I'm not sure if that's within scope for your purposes. Though, I do think it makes more sense to also have your output be a
So at the end, the three things you'd add:
Also, you can streamline the way you create the buttons with an array, consider using a panel with a layout manager to house the numbers.
JPanel numberPanel = new JPanel(new GridLayout(0, 3));
JButton[] numbers = new JButton[10];
numbers[0] = new JButton("0"); // while you can do this
// Better to use a loop!
for (int i = 1; i < numbers.length; i++) {
numbers[i] = new JButton(Integer.toString(i));
numberPanel.add(numbers[i]);
}
// Of course we don't forget the 0
numberPanel.add(numbers[0]);All you need to do at the end is
frame.add(numberPanel)You can do the same thing for the operators, and may use a border to manage distance between them, adding in Border layout with your frame to control overall positioning.
JPanel operatorPanel = new JPanel(new GridLayout(0, 1));
operatorPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
JButton[] operators = new JButton[5];Right now, I have an implementation that loops through an enum set with the operators and uses a lambda expression within to compute, but you've yet to answer my question about whether or not this has to be implemented on the console, so I'm not sure if that's within scope for your purposes. Though, I do think it makes more sense to also have your output be a
JLabel that has its own space.For now, you can have this loop through a string of the symbols{"+", "-", "*", "/", "="} and add them the same way that's already been expressed.So at the end, the three things you'd add:
frame.add(result, BorderLayout.NORTH);
frame.add(numberPanel, BorderLayout.CENTER);
frame.add(operatorPanel, BorderLayout.EAST);Code Snippets
JPanel numberPanel = new JPanel(new GridLayout(0, 3));
JButton[] numbers = new JButton[10];
numbers[0] = new JButton("0"); // while you can do this
// Better to use a loop!
for (int i = 1; i < numbers.length; i++) {
numbers[i] = new JButton(Integer.toString(i));
numberPanel.add(numbers[i]);
}
// Of course we don't forget the 0
numberPanel.add(numbers[0]);JPanel operatorPanel = new JPanel(new GridLayout(0, 1));
operatorPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
JButton[] operators = new JButton[5];frame.add(result, BorderLayout.NORTH);
frame.add(numberPanel, BorderLayout.CENTER);
frame.add(operatorPanel, BorderLayout.EAST);Context
StackExchange Code Review Q#87787, answer score: 10
Revisions (0)
No revisions yet.