Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java calculator program with the following requirements: Please explain how to add an icon to the title bar if the photo is already

Write a java calculator program with the following requirements:

Please explain how to add an icon to the title bar if the photo is already in the same folder.

As well as, how to change the layout of the calculator to look like a real calculator.

Add the following properties to your app:

? All digits, 4 operators (+,-,*,/), and the equal sign (=) should be added and they must be working correctly.

Text size in the text-field is very small!

? Use a larger font. Buttons are ugly!

? Change the color of the button. User friendly calculator.

? Keep the buttons in the order that it looks like a real calculator.

Doesn't look like your own app?

? Add a small icon to the title-bar of your frame. Make it yours!

Easy to get an error?

? User should not be able to start with an operator.

(Erase if entered before the input.)

? User should not be able to end with an operator. (Erase if entered at the end of the input.)

? User should not be able to use more than one operator for each calculation. (Keep the first operation only.)

? Preceding zeros should be erased.

? Still user can get some error?

? Create a small text area at the bottom of your app, and whenever the input is bad, show a message like "Bad Input!" and erase the user's input.

My code so far:

Calc

import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea;

public class calc extends JFrame { JButton btn_zero = new JButton("0"); JButton btn_one = new JButton("1"); JButton btn_two = new JButton("2"); JButton btn_three = new JButton("3"); JButton btn_four = new JButton("4"); JButton btn_five = new JButton("5"); JButton btn_six = new JButton("6"); JButton btn_seven = new JButton("7"); JButton btn_eight = new JButton("8"); JButton btn_nine = new JButton("9");

JButton btn_plus = new JButton(" + "); JButton btn_minus = new JButton(" - "); JButton btn_equals = new JButton(" = ");

JTextArea txt_Area = new JTextArea(3, 5);

char operator;

public calc() { this.createGUI(); this.showFrame();

}

private void createGUI() { add(txt_Area, BorderLayout.NORTH);

JPanel buttonpanel = new JPanel(); buttonpanel.setLayout(new FlowLayout());

buttonpanel.add(btn_seven); buttonpanel.add(btn_eight); buttonpanel.add(btn_nine); buttonpanel.add(btn_four); buttonpanel.add(btn_five); buttonpanel.add(btn_six); buttonpanel.add(btn_one); buttonpanel.add(btn_two); buttonpanel.add(btn_three); buttonpanel.add(btn_zero); buttonpanel.add(btn_plus); buttonpanel.add(btn_equals); buttonpanel.add(btn_minus);

add(buttonpanel, BorderLayout.CENTER);

txt_Area.setForeground(Color.BLACK); txt_Area.setBackground(Color.WHITE); txt_Area.setEditable(true); txt_Area.setFont(txt_Area.getFont().deriveFont(20f)); txt_Area.setLineWrap(true); txt_Area.setWrapStyleWord(true);

btn_zero.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("0".toString()); System.out.println(txt_Area.getText());

} });

btn_one.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("1".toString()); System.out.println(txt_Area.getText()); } });

btn_two.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("2".toString()); System.out.println(txt_Area.getText());

} });

btn_three.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("3".toString()); System.out.println(txt_Area.getText()); } });

btn_four.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("4".toString()); System.out.println(txt_Area.getText());

} });

btn_five.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("5".toString()); System.out.println(txt_Area.getText()); } });

btn_six.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("6".toString()); System.out.println(txt_Area.getText());

} });

btn_seven.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("7".toString()); System.out.println(txt_Area.getText()); } });

btn_eight.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("8".toString()); System.out.println(txt_Area.getText());

} });

btn_nine.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("9".toString()); System.out.println(txt_Area.getText()); } });

btn_plus.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("+".toString()); operator = '+'; } }); btn_minus.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { txt_Area.append("-".toString()); operator = '-'; } });

btn_equals.addActionListener(new ActionListener() {

@Override public void actionPerformed(ActionEvent e) { String LeftOperand; String RightOperand; String inputText; String outputText;

inputText = txt_Area.getText(); System.out.println(inputText); int operationIndex = inputText.indexOf(operator);

LeftOperand = txt_Area.getText().substring(0, operationIndex); RightOperand = txt_Area.getText().substring(operationIndex + 1, txt_Area.getText().length());

switch (operator) { case '+': outputText = " = " + (Double.parseDouble(LeftOperand) + Double.parseDouble(RightOperand)); System.out.println(outputText); txt_Area.append(outputText); case '-': outputText = " =";

} } }); }

private void showFrame() { this.setSize(230, 340); setTitle("Calculator"); this.setVisible(true); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

}

Main

import calculator.calc;

public class Main {

public static void main(String [] args) {

new calc();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago