Question: I am creating a Simple Java GUI Calculator. I need to figure out how to solve this errors. - User should not be able to
I am creating a Simple Java GUI Calculator.
I need to figure out how to solve this errors.
- 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.
I am also trying to add an icon to the title bar and it's not working.
This is my code so far.
Calculator class:
package calculator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
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_clear = new JButton("C");
JButton btn_plus = new JButton(" + ");
JButton btn_minus = new JButton(" - ");
JButton btn_divide = new JButton(" / ");
JButton btn_times = new JButton(" * ");
JButton btn_equals = new JButton(" = ");
JTextArea txt_Area = new JTextArea(2, 5);
JTextArea txt_Area2 = new JTextArea(2, 1);
char operator;
public calc() {
this.createGUI();
this.showFrame();
}
private void createGUI() {
add(txt_Area, BorderLayout.NORTH);
add(txt_Area2, BorderLayout.SOUTH);
JPanel buttonpanel = new JPanel();
buttonpanel.setBackground(Color.GRAY);
buttonpanel.setLayout(new GridLayout(4,3));
btn_zero.setBackground(Color.DARK_GRAY);
btn_zero.setOpaque(true);
btn_one.setBackground(Color.DARK_GRAY);
btn_one.setOpaque(true);
btn_two.setBackground(Color.DARK_GRAY);
btn_two.setOpaque(true);
btn_three.setBackground(Color.DARK_GRAY);
btn_three.setOpaque(true);
btn_four.setBackground(Color.DARK_GRAY);
btn_four.setOpaque(true);
btn_five.setBackground(Color.DARK_GRAY);
btn_five.setOpaque(true);
btn_six.setBackground(Color.DARK_GRAY);
btn_six.setOpaque(true);
btn_seven.setBackground(Color.DARK_GRAY);
btn_seven.setOpaque(true);
btn_eight.setBackground(Color.DARK_GRAY);
btn_eight.setOpaque(true);
btn_nine.setBackground(Color.DARK_GRAY);
btn_nine.setOpaque(true);
btn_plus.setBackground(Color.ORANGE);
btn_plus.setOpaque(true);
btn_minus.setBackground(Color.ORANGE);
btn_minus.setOpaque(true);
btn_equals.setBackground(Color.DARK_GRAY);
btn_equals.setOpaque(true);
btn_divide.setBackground(Color.ORANGE);
btn_divide.setOpaque(true);
btn_times.setBackground(Color.ORANGE);
btn_times.setOpaque(true);
btn_clear.setBackground(Color.DARK_GRAY);
btn_clear.setOpaque(true);
buttonpanel.add(btn_seven);
buttonpanel.add(btn_eight);
buttonpanel.add(btn_nine);
buttonpanel.add(btn_times);
buttonpanel.add(btn_four);
buttonpanel.add(btn_five);
buttonpanel.add(btn_six);
buttonpanel.add(btn_divide);
buttonpanel.add(btn_one);
buttonpanel.add(btn_two);
buttonpanel.add(btn_three);
buttonpanel.add(btn_minus);
buttonpanel.add(btn_zero);
buttonpanel.add(btn_clear);
buttonpanel.add(btn_equals);
buttonpanel.add(btn_plus);
add(buttonpanel, BorderLayout.CENTER);
txt_Area.setForeground(Color.white);
txt_Area.setBackground(Color.GRAY);
txt_Area.setEditable(true);
txt_Area.setFont(txt_Area.getFont().deriveFont(32f));
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_divide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("/".toString());
operator = '/';
}
});
btn_times.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt_Area.append("*".toString());
operator = '*';
}
});
btn_clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == btn_clear) {
txt_Area.setText(null);
System.out.println(txt_Area.getText());
}
}
});
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);
break;
case '-':
outputText = " = " + (Double.parseDouble(LeftOperand) - Double.parseDouble(RightOperand));
System.out.println(outputText);
txt_Area.append(outputText);
break;
case '*':
outputText = " = " + (Double.parseDouble(LeftOperand) * Double.parseDouble(RightOperand));
System.out.println(outputText);
txt_Area.append(outputText);
break;
case '/':
outputText = " = " + (Double.parseDouble(LeftOperand) / Double.parseDouble(RightOperand));
System.out.println(outputText);
txt_Area.append(outputText);
break;
}
}
});
}
private void showFrame() {
this.setSize(230, 340);
setTitle("Calculator");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setIcon() {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("calculator.png")));
}
}
Main class:
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
Get step-by-step solutions from verified subject matter experts
