Java
Java
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import java.text.DecimalFormat;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
JCheckBoxMenuItem history;
JPanel panel;
JTextArea historyText;
JScrollPane historyScroller;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, comma, plus, minus,
JTextField textField;
if (!textField.getText().isEmpty()) {
switch (button) {
case "symbol":
switch (lastCharacter) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'e':
case 'π':
textField.setText(textField.getText() + "×");
break;
case '.':
textField.setText(textField.getText() + "0×");
break;
break;
case "number":
switch (lastCharacter) {
case 'e':
case 'π':
textField.setText(textField.getText() + "×");
break;
case '0':
str.equals("0") ||
str.endsWith("+0") ||
str.endsWith("-0") ||
str.endsWith("×0") ||
str.endsWith("÷0") ||
str.endsWith("%0") ||
str.endsWith("^0") ||
str.endsWith("√0") ||
str.endsWith("(0") ||
str.endsWith("cos0") ||
str.endsWith("sin0") ||
str.endsWith("tan0")
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
break;
break;
case "operand":
switch (lastCharacter) {
case '+':
case '-':
case '×':
case '÷':
case '%':
case '.':
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
break;
}
break;
case "point":
switch(lastCharacter) {
case '+':
case '-':
case '×':
case '÷':
case '%':
case '(':
case '√':
case 'π':
case 's':
case 'n':
case '^':
textField.setText(textField.getText() + "0");
break;
case ')':
textField.setText(textField.getText() + "×0");
break;
case '.':
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
break;
break;
}
}
void nextChar() {
nextChar();
if (ch == charToEat) {
nextChar();
return true;
return false;
double parse() {
nextChar();
double x = parseExpression();
}
return x;
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) {
x += parseTerm();
else if (eat('-')) {
x -= parseTerm();
else {
return x;
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('×')) {
x *= parseFactor();
else if (eat('÷')) {
x /= parseFactor();
else if (eat('%')) {
x %= parseFactor();
}
else {
return x;
double parseFactor() {
if (eat('+')) {
return parseFactor();
if (eat('-')) {
return -parseFactor();
double x;
if (eat('(')) {
x = parseExpression();
eat(')');
else if (eat('e')) {
x = Math.E;
else if (eat('π')) {
x = Math.PI;
nextChar();
}
x = Double.parseDouble(str.substring(startPos, this.pos));
nextChar();
x = parseFactor();
switch (func) {
case "√":
x = Math.sqrt(x);
break;
case "sin":
x = Math.sin(Math.toRadians(x));
break;
case "cos":
x = Math.cos(Math.toRadians(x));
break;
case "tan":
x = Math.tan(Math.toRadians(x));
break;
default:
else {
}
if (eat('^')) {
x = Math.pow(x, parseFactor());
return x;
}.parse();
autoAddOrRemove("number");
textField.setText(textField.getText() + "0");
autoAddOrRemove("number");
textField.setText(textField.getText() + "1");
autoAddOrRemove("number");
textField.setText(textField.getText() + "2");
autoAddOrRemove("number");
textField.setText(textField.getText() + "3");
}
private void b4_isClicked(){
autoAddOrRemove("number");
textField.setText(textField.getText() + "4");
autoAddOrRemove("number");
textField.setText(textField.getText() + "5");
autoAddOrRemove("number");
textField.setText(textField.getText() + "6");
autoAddOrRemove("number");
textField.setText(textField.getText() + "7");
autoAddOrRemove("number");
textField.setText(textField.getText() + "8");
autoAddOrRemove("number");
textField.setText(textField.getText() + "9");
}
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "sin");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "cos");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "tan");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "√");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "e");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "π");
autoAddOrRemove("operand");
textField.setText(textField.getText() + "+");
autoAddOrRemove("operand");
textField.setText(textField.getText() + "-");
if (!textField.getText().isEmpty()) {
autoAddOrRemove("operand");
textField.setText(textField.getText() + "×");
if (!textField.getText().isEmpty()) {
autoAddOrRemove("operand");
textField.setText(textField.getText() + "÷");
if (!textField.getText().isEmpty()) {
autoAddOrRemove("operand");
textField.setText(textField.getText() + "%");
if(textField.getText().matches(".*[0-9eπ)]$")) {
textField.setText(textField.getText() + "^");
if(textField.getText().matches(".*[^ns√(]$")) {
for(char c: textField.getText().toCharArray()) {
if( c == '(')
LparentesesCounter++;
else if ( c == ')')
RparentesesCounter++;
textField.setText(textField.getText() + ")");
autoAddOrRemove("symbol");
textField.setText(textField.getText() + "(");
}
if (textField.getText().isEmpty()) {
textField.setText("0.");
else {
autoAddOrRemove("point");
textField.setText(textField.getText() + ".");
if (!textField.getText().isEmpty()) {
if (answer.isInfinite()) {
else if (answer.isNaN()) {
textField.setText("Error");
historyNewText += "Error";
else {
textField.setText(format.format(answer));
historyNewText += format.format(answer);
textField.setText("Error");
historyNewText += "Error";
historyText.setText(historyNewText+"\n\n");
else if (!temp.isEmpty()) {
if (temp.length() >= 2) {
case "co":
case "si":
case "ta":
break;
textField.setText(temp);
textField.setText("");
try {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
menuBar.add(view);
menuBar.add(edit);
menuBar.add(help);
view.add(history);
edit.add(copy);
edit.add(paste);
edit.addSeparator();
edit.add(copyHistory);
edit.add(clearHistory);
help.add(keyboardShortcuts);
help.add(about);
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
pi = new JButton("π");
view.setFont(font3);
edit.setFont(font3);
help.setFont(font3);
history.setFont(font3);
copy.setFont(font3);
paste.setFont(font3);
copyHistory.setFont(font3);
clearHistory.setFont(font3);
about.setFont(font3);
keyboardShortcuts.setFont(font3);
b0.setFont(font1);
b1.setFont(font1);
b2.setFont(font1);
b3.setFont(font1);
b4.setFont(font1);
b5.setFont(font1);
b6.setFont(font1);
b7.setFont(font1);
b8.setFont(font1);
b9.setFont(font1);
comma.setFont(font1);
equal.setFont(font1);
plus.setFont(font1);
minus.setFont(font1);
multiple.setFont(font1);
divide.setFont(font1);
cos.setFont(font2);
sin.setFont(font2);
tan.setFont(font2);
sqrt.setFont(font1);
power.setFont(font1);
modulo.setFont(font1);
exponential.setFont(font1);
pi.setFont(font1);
parentesesLeft.setFont(font1);
parentesesRight.setFont(font1);
clear.setFont(font1);
back.setFont(font1);
textField.setHorizontalAlignment(JTextField.RIGHT);
b1.setForeground(Color.white);
b2.setForeground(Color.white);
b3.setForeground(Color.white);
b4.setForeground(Color.white);
b5.setForeground(Color.white);
b6.setForeground(Color.white);
b7.setForeground(Color.white);
b8.setForeground(Color.white);
b9.setForeground(Color.white);
comma.setForeground(Color.white);
equal.setForeground(Color.white);
plus.setForeground(Color.white);
minus.setForeground(Color.white);
multiple.setForeground(Color.white);
divide.setForeground(Color.white);
cos.setForeground(Color.white);
sin.setForeground(Color.white);
tan.setForeground(Color.white);
sqrt.setForeground(Color.white);
power.setForeground(Color.white);
modulo.setForeground(Color.white);
exponential.setForeground(Color.white);
pi.setForeground(Color.white);
parentesesLeft.setForeground(Color.white);
parentesesRight.setForeground(Color.white);
clear.setForeground(Color.white);
back.setForeground(Color.white);
textField.setBackground(Color.lightGray);
b0.setBackground(Color.darkGray);
b1.setBackground(Color.darkGray);
b2.setBackground(Color.darkGray);
b3.setBackground(Color.darkGray);
b4.setBackground(Color.darkGray);
b5.setBackground(Color.darkGray);
b6.setBackground(Color.darkGray);
b7.setBackground(Color.darkGray);
b8.setBackground(Color.darkGray);
b9.setBackground(Color.darkGray);
comma.setBackground(Color.darkGray);
plus.setBackground(Color.black);
minus.setBackground(Color.black);
multiple.setBackground(Color.black);
divide.setBackground(Color.black);
cos.setBackground(Color.black);
sin.setBackground(Color.black);
tan.setBackground(Color.black);
sqrt.setBackground(Color.black);
power.setBackground(Color.black);
modulo.setBackground(Color.black);
exponential.setBackground(Color.black);
pi.setBackground(Color.black);
parentesesLeft.setBackground(Color.black);
parentesesRight.setBackground(Color.black);
equal.setBackground(Color.black);
historyText.setBackground(Color.lightGray);
getContentPane().setBackground(Color.black);
textField.setFocusable(false);
historyText.setFocusable(false);
b0.setFocusable(false);
b1.setFocusable(false);
b2.setFocusable(false);
b3.setFocusable(false);
b4.setFocusable(false);
b5.setFocusable(false);
b6.setFocusable(false);
b7.setFocusable(false);
b8.setFocusable(false);
b9.setFocusable(false);
comma.setFocusable(false);
plus.setFocusable(false);
minus.setFocusable(false);
multiple.setFocusable(false);
divide.setFocusable(false);
cos.setFocusable(false);
sin.setFocusable(false);
tan.setFocusable(false);
sqrt.setFocusable(false);
power.setFocusable(false);
modulo.setFocusable(false);
exponential.setFocusable(false);
pi.setFocusable(false);
parentesesLeft.setFocusable(false);
parentesesRight.setFocusable(false);
equal.setFocusable(false);
clear.setFocusable(false);
back.setFocusable(false);
panel.add(b0);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(comma);
panel.add(equal);
panel.add(plus);
panel.add(multiple);
panel.add(minus);
panel.add(divide);
panel.add(cos);
panel.add(sin);
panel.add(tan);
panel.add(sqrt);
panel.add(power);
panel.add(modulo);
panel.add(exponential);
panel.add(pi);
panel.add(parentesesRight);
panel.add(parentesesLeft);
panel.add(clear);
panel.add(back);
panel.add(textField);
history.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
copyHistory.addActionListener(this);
clearHistory.addActionListener(this);
keyboardShortcuts.addActionListener(this);
about.addActionListener(this);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
comma.addActionListener(this);
cos.addActionListener(this);
sin.addActionListener(this);
tan.addActionListener(this);
sqrt.addActionListener(this);
power.addActionListener(this);
modulo.addActionListener(this);
exponential.addActionListener(this);
pi.addActionListener(this);
parentesesLeft.addActionListener(this);
parentesesRight.addActionListener(this);
plus.addActionListener(this);
multiple.addActionListener(this);
divide.addActionListener(this);
minus.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
back.addActionListener(this);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
add(panel);
add(historyScroller);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Scientific Calculator");
setSize(262, 380);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
setVisible(true);
}
@Override
if (e.getSource() == b0)
b0_isClicked();
b1_isClicked();
b2_isClicked();
b3_isClicked();
b4_isClicked();
b5_isClicked();
b6_isClicked();
b7_isClicked();
b9_isClicked();
sin_isClicked();
cos_isClicked();
tan_isClicked();
sqrt_isClicked();
exponential_isClicked();
pi_isClicked();
plus_isClicked();
minus_isClicked();
else if (e.getSource() == multiple)
multiple_isClicked();
divide_isClicked();
modulo_isClicked();
power_isClicked();
parentesesRight_isClicked();
parentesesLeft_isClicked();
comma_isClicked();
equal_isClicked();
back_isClicked();
clear_isClicked();
else if (e.getSource() == history)
if(history.isSelected())
this.setSize(520, 380);
else
this.setSize(262, 380);
textField.selectAll();
textField.copy();
textField.setCaretPosition(textField.getText().length());
textField.paste();
historyText.selectAll();
historyText.copy();
textField.setCaretPosition(textField.getText().length());
}
else if (e.getSource() == clearHistory )
historyText.setText("");
String str
= "<html>"
+ "<ul>"
+ "</ul>"
+ "<html>";
String str
= "<html>"
+ "<big>Scientific Calculator</big><br><br>"
// + "<u>Note</u><br>"
+ "<html>";
@Override
switch(e.getKeyChar()) {
case '0':
b0_isClicked();
break;
case '1':
b1_isClicked();
break;
case '2':
b2_isClicked();
break;
case '3':
b3_isClicked();
break;
case '4':
b4_isClicked();
break;
case '5':
b5_isClicked();
break;
case '6':
b6_isClicked();
break;
case '7':
b7_isClicked();
break;
case '8':
b8_isClicked();
break;
case '9':
b9_isClicked();
break;
case 's':
sin_isClicked();
break;
case 'c':
cos_isClicked();
break;
case 't':
tan_isClicked();
break;
case 'v':
sqrt_isClicked();
break;
case 'e':
exponential_isClicked();
break;
case 'p':
pi_isClicked();
break;
case '+':
plus_isClicked();
break;
case '-':
minus_isClicked();
break;
case '*':
case '×':
multiple_isClicked();
break;
case '/':
case '÷':
divide_isClicked();
break;
case '%':
modulo_isClicked();
break;
case '^':
power_isClicked();
break;
case ')':
parentesesRight_isClicked();
break;
case '(':
parentesesLeft_isClicked();
break;
case '.':
comma_isClicked();
break;
case '=':
case '\n':
equal_isClicked();
break;
if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE )
back_isClicked();
if(e.getKeyCode() == KeyEvent.VK_DELETE )
clear_isClicked();
@Override
@Override
}
public Main() {
createAndShowGUI();
SwingUtilities.invokeLater(new Runnable() {
@Override
new Main();
});