From ad298f23e516ed366953568e4f7d70790f01a12c Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 5 Nov 2024 17:29:35 +0000 Subject: [PATCH] Add number inputs from 0 to 9 to GUI Co-Authored-By: td1223 --- src/main/java/ic/doc/RpnCalculatorGui.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/ic/doc/RpnCalculatorGui.java b/src/main/java/ic/doc/RpnCalculatorGui.java index 9310481..3ab32b6 100644 --- a/src/main/java/ic/doc/RpnCalculatorGui.java +++ b/src/main/java/ic/doc/RpnCalculatorGui.java @@ -3,6 +3,8 @@ package ic.doc; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.JButton; +import java.util.stream.IntStream; public class RpnCalculatorGui { private final int defaultWidth; @@ -10,6 +12,10 @@ public class RpnCalculatorGui { private static final int RESULT_FIELD_WIDTH = 10; + // Define the range of numbers that can be input to the calculator + private static final int MIN_INPUT_NUMBER = 0; + private static final int MAX_INPUT_NUMBER = 9; + public static RpnCalculatorGui withDimensions(int defaultWidth, int defaultHeight) { return new RpnCalculatorGui(defaultWidth, defaultHeight); } @@ -25,6 +31,12 @@ public class RpnCalculatorGui { JPanel panel = new JPanel(); + // Add buttons for each number input + IntStream.rangeClosed(MIN_INPUT_NUMBER, MAX_INPUT_NUMBER).forEach(n -> { + JButton button = new JButton(Integer.toString(n)); + panel.add(button); + }); + JTextField resultField = new JTextField(RESULT_FIELD_WIDTH); panel.add(resultField);