Question
Write a program based on a code given below using Java Programming Language. All details are on the second image at the very end of
Write a program based on a code given below using Java Programming Language. All details are on the second image at the very end of this post.
Code:
CalculatorGUI.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
public class CalculatorGUI {
public static final String[][] BUTTON_TEXTS = {
{"7", "8", "9", "+"},
{"4", "5", "6", "-"},
{"1", "2", "3", "*"},
{"0", ".", "=", "/"}
};
public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
private static void createAndShowGUI() {
JTextField field = new JTextField(15);
field.setFont(BTN_FONT.deriveFont(Font.PLAIN));
JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length, BUTTON_TEXTS[0].length));
for (int i = 0; i
for (int j = 0; j
JButton btn = new JButton(BUTTON_TEXTS[i][j]);
btn.setFont(BTN_FONT);
btnPanel.add(btn);
}
}
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(field, BorderLayout.PAGE_START);
mainPanel.add(btnPanel, BorderLayout.CENTER);
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.*; public class CalculatorGUI public static final String 111 BUTTON TEXTS public static final Font BTN_FONT new Font (Font.SANS SERIF, Font.BOLD, 24); private static void createAndShowGUI() TextField field new JTextField(15); field.setFont (BTN_FONT.deriveFont (Font.PLAIN)); Panel btnPanel new JPanel (new GridLayout (BUTTON TEXTS.length, BUTTON TEXTS for (int i 0; ?Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started