0% found this document useful (0 votes)
6 views

17 GridLayout

The document contains Java code for a simple calculator application using Swing. It sets up a JFrame with a text field and buttons for numbers and basic operations. The application is designed to handle user input through button actions, although the action handling logic is not implemented in the provided code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

17 GridLayout

The document contains Java code for a simple calculator application using Swing. It sets up a JFrame with a text field and buttons for numbers and basic operations. The application is designed to handle user input through button actions, although the action handling logic is not implemented in the provided code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No 17 :

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class SimpleCalculator implements ActionListener {

JFrame frame;

JTextField textField;

JButton[] numberButtons = new JButton[10];

JButton[] functionButtons = new JButton[8];

JPanel panel;

double num1, num2, result;

char operator;

SimpleCalculator() {

frame = new JFrame("Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(420, 550);

textField = new JTextField();

textField.setBounds(50, 25, 300, 50);

textField.setEditable(false);
String[] functions = {"+", "-", "*", "/", ".", "=", "Del", "Clr"};

for (int i = 0; i < 8; i++) {

functionButtons[i] = new JButton(functions[i]);

functionButtons[i].addActionListener(this);

for (int i = 0; i < 10; i++) {

numberButtons[i] = new JButton(String.valueOf(i));

numberButtons[i].addActionListener(this);

panel = new JPanel();

panel.setBounds(50, 100, 300, 300);

panel.setLayout(new GridLayout(4, 4, 10, 10));

String[] order = {"1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", ".", "0", "=", "/"};

for (String label : order) {

if (label.matches("\\d")) {

panel.add(numberButtons[Integer.parseInt(label)]);

} else {

for (JButton btn : functionButtons) {

if (btn.getText().equals(label)) {

panel.add(btn);

break;
}

frame.add(panel);

frame.add(textField);

frame.setVisible(true);

public static void main(String[] args) {

new SimpleCalculator();

@Override

public void actionPerformed(ActionEvent e) {

// Handle button actions here

Output:

You might also like