I have a simple app to design. Two text fields, each for an operand, a compute button to compute the value of an operation applied to these 2 operands and 4 radio buttons, organised into two button groups: first group contains buttons A and B, second group contains buttons C and D
I need to implement a certain operation (ex. addition) for a combination of these 4 buttons.(A + C => operation <- addition) Only one button to be active in each group. I have to avoid IF statements.
This is what I have for now. I have to add new code to addListeners()
function.
package calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
private JFrame frame;
private JPanel mainPanel;
private JTextField firstField;
private JTextField secondField;
private JButton computeButton;
private JTextField resultField;
private JRadioButton aRadioButton;
private JRadioButton bRadioButton;
private JRadioButton cRadioButton;
private JRadioButton dRadioButton;
private ButtonGroup firstRadioButtonGroup;
private ButtonGroup secondRadioButtonGroup;
private IOperation operation;
private double firstOperand;
private double secondOperand;
private double result;
public Calculator() {
initialize();
adjustWindow();
applyStyle();
centerWindow();
addToFrame();
addListeners();
}
private void addListeners() {
computeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstOperand = Double.parseDouble(firstField.getText());
secondOperand = Double.parseDouble(secondField.getText());
result = operation.compute(firstOperand, secondOperand);
resultField.setText(Double.toString(result));
}
});
}
private void addToFrame() {
mainPanel.add(firstField);
mainPanel.add(secondField);
mainPanel.add(computeButton);
mainPanel.add(aRadioButton);
mainPanel.add(bRadioButton);
mainPanel.add(cRadioButton);
mainPanel.add(dRadioButton); firstRadioButtonGroup.add(aRadioButton);
firstRadioButtonGroup.add(bRadioButton);
secondRadioButtonGroup.add(cRadioButton);
secondRadioButtonGroup.add(dRadioButton);
mainPanel.add(aRadioButton);
mainPanel.add(bRadioButton);
mainPanel.add(cRadioButton);
mainPanel.add(dRadioButton);
firstRadioButtonGroup.clearSelection();
mainPanel.add(resultField);
frame.add(mainPanel);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void initialize() {
firstOperand = 0;
secondOperand = 0;
frame = new JFrame("Calculator");
mainPanel = new JPanel();
firstField = new JTextField();
secondField = new JTextField();
computeButton = new JButton("Compute");
resultField = new JTextField();
aRadioButton = new JRadioButton("A");
bRadioButton = new JRadioButton("B");
cRadioButton = new JRadioButton("C");
dRadioButton = new JRadioButton("D");
firstRadioButtonGroup = new ButtonGroup();
secondRadioButtonGroup = new ButtonGroup();
}
private void adjustWindow() {
frame.setSize(300, 300);
}
private void centerWindow() {
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screenSize = tool.getScreenSize();
double screenHeight = screenSize.height;
double screenWidth = screenSize.width;
double currentWindowHeight = frame.getHeight();
double currentWindowWidth = frame.getWidth();
int windowHeight = (int) ((screenHeight / 2) - (currentWindowHeight / 2));
int windowWidth = (int) ((screenWidth / 2) - (currentWindowWidth / 2));
frame.setLocation(windowWidth, windowHeight);
}
private void applyStyle() {
firstField.setPreferredSize(new Dimension(240, 30));
secondField.setPreferredSize(new Dimension(240, 30));
resultField.setPreferredSize(new Dimension(240, 30));
computeButton.setPreferredSize(new Dimension(240, 30));
}
}
I would like to hear more about the design. Is it possible to separate the GUI of the calculator from the operation?