buttons display on screen

This commit is contained in:
Mikayla Dobson
2023-01-12 13:53:22 -06:00
parent 1c5f51d702
commit 29ac5ffffb
5 changed files with 217 additions and 11 deletions

View File

@@ -0,0 +1,34 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.javacalculator;
import javax.swing.JButton;
import javax.swing.JPanel;
/**
*
* @author mikayladobson
*/
public class ButtonRow extends JPanel {
public ButtonRow() {}
public ButtonRow(JButton btn) {
addToRow(btn);
}
public ButtonRow(JButton[] buttons) {
addToRow(buttons);
}
public void addToRow(JButton btn) {
this.add(btn);
}
public void addToRow(JButton[] buttons) {
for (JButton btn : buttons) {
this.add(btn);
}
}
}

View File

@@ -4,44 +4,102 @@
package com.mycompany.javacalculator; package com.mycompany.javacalculator;
import java.awt.Color;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import com.mycompany.javacalculator.Operands.Operand;
import java.util.Collection;
import javax.swing.JButton;
/** /**
* *
* @author mikayladobson * @author mikayladobson
*/ */
public class JavaCalculator implements ActionListener { public class JavaCalculator implements ActionListener {
private float storedNum = 0;
private float receivedNum = 0;
private String receivedData = "";
private Operand operand = null;
private final JFrame frame; private final JFrame frame;
private final JLabel label;
private final JPanel mainPanel; private final JPanel mainPanel;
private final JPanel calculatorScreen;
private final JLabel screenText;
private final NumButton btn1; private final NumButton btn1;
private final NumButton btn2; private final NumButton btn2;
private final NumButton btn3; private final NumButton btn3;
private final NumButton btn4;
private final NumButton btn5;
private final NumButton btn6;
private final NumButton btn7;
private final NumButton btn8;
private final NumButton btn9;
private final NumButton btn0;
private final OpButton btnPlus;
private final OpButton btnMinus;
private final OpButton btnMultiply;
private final OpButton btnDivide;
private final OpButton btnExponent;
private final OpButton btnEquals;
private final OpButton btnDecimal;
public JavaCalculator() { public JavaCalculator() {
frame = new JFrame(); frame = new JFrame();
label = new JLabel("Initial value"); screenText = new JLabel("Initial value");
mainPanel = new JPanel(); mainPanel = new JPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(100,100,100,100)); mainPanel.setBorder(BorderFactory.createEmptyBorder(100,100,100,100));
mainPanel.setLayout(new GridLayout(4, 4)); mainPanel.setLayout(new GridLayout(4, 4));
mainPanel.add(label);
calculatorScreen = new JPanel();
calculatorScreen.add(screenText);
calculatorScreen.setBackground(Color.GRAY);
mainPanel.add(calculatorScreen);
btn1 = new NumButton(1, this); btn1 = new NumButton(1, this);
btn2 = new NumButton(2, this); btn2 = new NumButton(2, this);
btn3 = new NumButton(3, this); btn3 = new NumButton(3, this);
btn4 = new NumButton(4, this);
btn5 = new NumButton(5, this);
btn6 = new NumButton(6, this);
btn7 = new NumButton(7, this);
btn8 = new NumButton(8, this);
btn9 = new NumButton(9, this);
btn0 = new NumButton(0, this);
mainPanel.add(btn1); btnPlus = new OpButton(Operand.ADD, this);
mainPanel.add(btn2); btnMinus = new OpButton(Operand.SUBTRACT, this);
mainPanel.add(btn3); btnMultiply = new OpButton(Operand.MULTIPLY, this);
btnDivide = new OpButton(Operand.DIVIDE, this);
btnExponent = new OpButton(Operand.EXPONENT, this);
btnEquals = new OpButton(Operand.EQUALS, this);
btnDecimal = new OpButton(Operand.DECIMAL, this);
NumButton[] numBtns = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0 };
OpButton[] opBtns = { btnPlus, btnMinus, btnMultiply, btnDivide, btnExponent, btnEquals, btnDecimal };
RowFactory rowFactory = new RowFactory(numBtns);
Collection<ButtonRow> btnRows = rowFactory.getRows();
for (ButtonRow row : btnRows) {
mainPanel.add(row);
}
rowFactory.setRows(opBtns);
btnRows = rowFactory.getRows();
for (ButtonRow row : btnRows) {
mainPanel.add(row);
}
frame.add(mainPanel); frame.add(mainPanel);
frame.setTitle("Calculator"); frame.setTitle("Calculator");
@@ -57,7 +115,7 @@ public class JavaCalculator implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
NumButton source = (NumButton)e.getSource(); JButton source = (JButton)e.getSource();
label.setText(source.getText()); screenText.setText(source.getText());
} }
} }

View File

@@ -0,0 +1,22 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.javacalculator;
import javax.swing.JButton;
import com.mycompany.javacalculator.Operands.Operand;
/**
*
* @author mikayladobson
*/
public class OpButton extends JButton {
private final Operand operand;
public OpButton(Operand operand, JavaCalculator calculator) {
this.operand = operand;
this.addActionListener(calculator);
this.setText(Operands.toLabel(operand));
}
}

View File

@@ -0,0 +1,45 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.javacalculator;
/**
*
* @author mikayladobson
*/
public class Operands {
public enum Operand {
ADD, SUBTRACT, MULTIPLY, DIVIDE, EXPONENT,
EQUALS, DECIMAL
}
public static String toLabel(Operand input) {
switch (input) {
case ADD -> {
return "+";
}
case SUBTRACT -> {
return "-";
}
case MULTIPLY -> {
return "*";
}
case DIVIDE -> {
return "/";
}
case EXPONENT -> {
return "^";
}
case DECIMAL -> {
return ".";
}
case EQUALS -> {
return "=";
}
default -> {
throw new Error("Invalid operand input");
}
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.javacalculator;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JButton;
/**
*
* @author mikayladobson
*/
public class RowFactory {
private Collection<ButtonRow> allRows;
public RowFactory() {
allRows = new ArrayList<ButtonRow>();
}
public RowFactory(JButton[] btns) {
setRows(btns);
}
public void setRows(JButton[] btns) {
allRows = new ArrayList<ButtonRow>();
int remaining = 0;
while (remaining < btns.length) {
ButtonRow product = new ButtonRow();
int i = 0;
while (remaining < btns.length && i < 3) {
product.add(btns[remaining]);
remaining++;
i++;
}
allRows.add(product);
}
}
public Collection<ButtonRow> getRows() {
return this.allRows;
}
}