Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.sun.javafx.geom.AreaOp.AddOp;
import sun.tools.jar.resources.jar;
public class Calculator extends JFrame {
enum OperationEnum {
ADDITION("+"), SUBSTRACTION("-"), DIVISION("/"), MULTIPICATION("*");
String operator;
OperationEnum(String s1) {
operator = s1;
}
@Override
public String toString() {
return operator;
}
}
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,b11,b12,b13,b14,b15,b16,b17;
JTextField t1;
JPanel p1;
public Calculator() {
t1 = new JTextField(10);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("0");
JButton b11 = new JButton(OperationEnum.ADDITION.toString());
JButton b12 = new JButton(OperationEnum.SUBSTRACTION.toString());
JButton b13 = new JButton(OperationEnum.DIVISION.toString());
JButton b14 = new JButton(OperationEnum.MULTIPICATION.toString());
JButton b15 = new JButton("=");
JButton b16= new JButton("M+");
JButton b17= new JButton("M-");
add(t1);
JPanel p1 = new JPanel(new GridLayout(6, 6));
setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
p1.add(b17);
add(p1);
setSize(300, 300);
Operation o = new Operation();
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"1");
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"2");
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"3");
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"4");
}
});
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"5");
}
});
b6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"6");
}
});
b7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"7");
}
});
b8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"8");
}
});
b9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"9");
}
});
b10.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t1.getText()+"0");
}
});
b11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (o.op==null) {
o.n1 =Double.parseDouble(t1.getText());
o.op = "+";
}
else {
o.n2 =Double.parseDouble(t1.getText());
}
System.out.println(o);
t1.setText("");
//t1.setText(t1.getText()+b11.getText());
}});
b12.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (o.op==null) {
o.n1 =Double.parseDouble(t1.getText());
o.op = "-";
}
else {
o.n2 =Double.parseDouble(t1.getText());
}
t1.setText(t1.getText()+b12.getText());
}});
b13.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (o.op==null) {
o.n1 =Double.parseDouble(t1.getText());
o.op = "/";
}
else {
o.n2 =Double.parseDouble(t1.getText());
}
t1.setText(t1.getText()+b12.getText());
t1.setText(t1.getText()+b13.getText());
}});
b14.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (o.op==null) {
o.n1 =Double.parseDouble(t1.getText());
o.op = "*";
}
else {
o.n2 =Double.parseDouble(t1.getText());
}
t1.setText(t1.getText()+b14.getText());
}});
b15.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(o.n1!=0 && o.n2!=0) {
o.op = "=";
}
}});
setVisible(true);
b16.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
FileWriter fw= new FileWriter("E:\calculator.txt");
//fw.write();
fw.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
b17.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Reader r = new FileReader("E:\testout.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new Calculator();
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team