Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
73 views

Import Import Import Import Class Public Static Void New True

This document contains the code for a simple Swing-based calculator application in Java. It defines classes for the calculator frame, number and operator buttons, and calculator operations. The Calculator class extends JFrame and sets up the user interface with number buttons, operator buttons, and a display text field. It also defines inner classes for the button listeners and calculator operations.

Uploaded by

Aravinda Gowda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Import Import Import Import Class Public Static Void New True

This document contains the code for a simple Swing-based calculator application in Java. It defines classes for the calculator frame, number and operator buttons, and calculator operations. The Calculator class extends JFrame and sets up the user interface with number buttons, operator buttons, and a display text field. It also defines inner classes for the button listeners and calculator operations.

Uploaded by

Aravinda Gowda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import import import import

java.awt.*; java.awt.event.*; javax.swing.*; javax.swing.event.*;

class SwingCalculator { public static void main(String[] args) { JFrame frame = new Calculator(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

import import import import

java.awt.*; java.awt.event.*; javax.swing.*; javax.swing.event.*;

class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private CalculatorOp op = new CalculatorOp(); public Calculator() { textfield = new JTextField("0", 12); textfield.setHorizontalAlignment(JTextField.RIGHT); textfield.setFont(BIGGER_FONT); ActionListener numberListener = new NumberListener(); String buttonOrder = "1234567890 "; JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 4, 4, 4)); for (int i = 0; i < buttonOrder.length(); i++) { String key = buttonOrder.substring(i, i+1); if (key.equals(" ")) { buttonPanel.add(new JLabel("")); } else { JButton button = new JButton(key); button.addActionListener(numberListener); button.setFont(BIGGER_FONT); buttonPanel.add(button); } } ActionListener operatorListener = new OperatorListener(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 4, 4, 4)); String[] opOrder = {"+", "-", "*", "/","=","C"}; for (int i = 0; i < opOrder.length; i++) { JButton button = new JButton(opOrder[i]); button.addActionListener(operatorListener); button.setFont(BIGGER_FONT); panel.add(button);

} JPanel pan = new JPanel(); pan.setLayout(new BorderLayout(4, 4)); pan.add(textfield, BorderLayout.NORTH ); pan.add(buttonPanel , BorderLayout.CENTER); pan.add(panel , BorderLayout.EAST ); this.setContentPane(pan); this.pack(); this.setTitle("Calculator"); this.setResizable(false); } private void action() { number = true; textfield.setText("0"); equalOp = "="; op.setTotal("0"); } class OperatorListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (number) { action(); textfield.setText("0"); } else { number = true; String displayText = textfield.getText(); if (equalOp.equals("=")) { op.setTotal(displayText); } else if (equalOp.equals("+")) { op.add(displayText); } else if (equalOp.equals("-")) { op.subtract(displayText); } else if (equalOp.equals("*")) { op.multiply(displayText); } else if (equalOp.equals("/")) { op.divide(displayText); } textfield.setText("" + op.getTotalString()); equalOp = e.getActionCommand(); } } } class NumberListener implements ActionListener { public void actionPerformed(ActionEvent event) { String digit = event.getActionCommand(); if (number) { textfield.setText(digit); number = false; } else { textfield.setText(textfield.getText() + digit); } } } public class CalculatorOp { private int total; public CalculatorOp() { total = 0;

} public String getTotalString() return ""+total; } public void setTotal(String n) total = convertToNumber(n); } public void add(String n) { total += convertToNumber(n); } public void subtract(String n) total -= convertToNumber(n); } public void multiply(String n) total *= convertToNumber(n); }

{ {

{ { public void divide(String n) {

total /= convertToNumber(n); } private int convertToNumber(String n) { return Integer.parseInt(n); } } }

You might also like