import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class CalPad implements ActionListener
{
JFrame frame;
JPanel upperPanel;
JPanel lowerPanel;
JPanel buttonPanel;
JTextArea textArea;
JTextField display;
JScrollPane sp;
JLabel lb;
JButton button[]=new JButton[20];
Icon icon[]=new Icon[20];
static int count;
double opr1,opr2;
String lastoperator="";
double result;
static boolean saveflag=false;
String filename="";
File fname;
CalPad()
{
frame=new JFrame("Calculator ~~milan~~");
upperPanel=new JPanel();
lowerPanel=new JPanel();
buttonPanel=new JPanel();
textArea=new JTextArea();
Color c=new Color(0X4682B4);
textArea.setBackground(c);
textArea.setForeground(Color.white);
textArea.setFont(new Font("comic sans",Font.PLAIN,14));
textArea.setEditable(false);
//textArea.setText(format+"milan");
display=new JTextField("");
display.setBackground(Color.black);
display.setForeground(Color.green);
display.setFont(new Font("comic sans",Font.PLAIN,22));
display.setEditable(false);
lb=new JLabel(new ImageIcon("calculator1.gif"));
sp=new JScrollPane(textArea);
upperPanel.setLayout(null);
lb.setBounds(-10,0,270,50);
sp.setBounds(0,50,250,150);
display.setBounds(0,200,250,40);
upperPanel.add(lb);
upperPanel.add(sp);
upperPanel.add(display);
buttonPanel.setLayout(new GridLayout(5,4));
for(int i=0;i<20;i++)
{
icon[i]=new ImageIcon(i+".gif");
button[i]=new JButton(icon[i]);
buttonPanel.add(button[i]);
button[i].addActionListener(this);
}
setCalOff();
lowerPanel.setLayout(new BorderLayout());
lowerPanel.add(buttonPanel,BorderLayout.CENTER);
frame.setLayout(new GridLayout(2,1));
frame.add(upperPanel);
frame.add(lowerPanel);
frame.setSize(260,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String... s)
{
new CalPad();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button[0])
save();
if(e.getSource()==button[1])
{
if(saveflag==false)
{
if(JOptionPane.showConfirmDialog(null,"Do You Want To Save?", "Hi..(milan)", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
save();
else
System.exit(0);
}
else
{
save();
System.exit(0);
}
}
if(e.getSource()==button[2])
setCalOn();
if(e.getSource()==button[3])
setCalOff();
if(e.getSource()==button[4])
show("1");
else if(e.getSource()==button[5])
show("2");
else if(e.getSource()==button[6])
show("3");
else if(e.getSource()==button[8])
show("4");
else if(e.getSource()==button[9])
show("5");
else if(e.getSource()==button[10])
show("6");
else if(e.getSource()==button[12])
show("7");
else if(e.getSource()==button[13])
show("8");
else if(e.getSource()==button[14])
show("9");
else if(e.getSource()==button[17])
show("0");
if(e.getSource()==button[16])
{
if(display.getText().indexOf(".")==-1)
{
display.setText(display.getText()+".");
textArea.append(".");
}
else{}
}
if(e.getSource()==button[7])
{
count++;
setopr1("+");
lastoperator="+";
textArea.append("+\n");
}
if(e.getSource()==button[11])
{
count++;
setopr1("-");
lastoperator="-";
textArea.append("-\n");
}
if(e.getSource()==button[15])
{
count++;
setopr1("*");
lastoperator="*";
textArea.append("*\n");
}
if(e.getSource()==button[19])
{
count++;
setopr1("/");
lastoperator="/";
textArea.append("/\n");
}
if(e.getSource()==button[18])
{
count++;
setopr1("=");
lastoperator="=";
display.setText(""+result);
textArea.append("\n----------------------\n");
textArea.append("="+result);
}
}
public void setopr1(String op)
{
if(count<=1)
{
opr1=Double.parseDouble(display.getText());
result=opr1;
}
else
{
if(!display.getText().equals(""))
{
opr2=Double.parseDouble(display.getText());
if(lastoperator.equals("+"))
{
result=result+opr2;
}
if(lastoperator.equals("-"))
{
result=result-opr2;
}
if(lastoperator.equals("*"))
{
result=result*opr2;
}
if(lastoperator.equals("/"))
{
result=result/opr2;
}
if(lastoperator.equals("="))
{
result=result;
display.setText(""+result);
}
}
else{}
}
display.setText("");
}
public void setData(double operand,String operator)
{
textArea.append(""+operand+operator);
textArea.append("\n");
}
public void setCalOn()
{
for(int i=0;i<20;i++)
{
if(i==2)
button[2].setEnabled(false);
else
button[i].setEnabled(true);
}
}
public void setCalOff()
{
for(int i=0;i<20;i++)
{
if(i==2)
button[i].setEnabled(true);
else
button[i].setEnabled(false);
}
resetCal();
}
public void resetCal()
{
textArea.setText("");
display.setText("");
result=0.0;
opr1=0.0;
opr2=0.0;
count=0;
}
public void show(String s)
{
if(display.getText().equals(""))
display.setText(s);
else
display.setText(display.getText()+s);
textArea.append(s);
}
public void save()
{
if(saveflag==false)
{
JFileChooser chooser= new JFileChooser();
int result;
result=chooser.showSaveDialog(null);
if(result==chooser.APPROVE_OPTION)
{
filename=chooser.getSelectedFile().getAbsolutePath();
fname=new File(filename);
if(fname.exists())
{
if(JOptionPane.showConfirmDialog(null,"File already exists.Do you want to Raplace?", "Hi..", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
madeFile(filename);
}
else{saveflag=false;save();}
}
else
madeFile(filename);
}
else
return;
}
else
madeFile(filename);
}
public void madeFile(String filename)
{
try{
String data1=textArea.getText();
String[] a=data1.split("\n");
BufferedWriter bw=new BufferedWriter(new FileWriter(filename));
for(String data:a)
{
bw.write(data);
bw.newLine();
}
bw.close();
fname=new File(filename);
}
catch(Exception e2){ System.out.println("can not write"+e2);}
String str=fname.getName();
JOptionPane.showMessageDialog(null,"Saved in :: "+str,"Success",JOptionPane.INFORMATION_MESSAGE);
saveflag=true;
}
}//end classs
Download
Sudoku by Er. Minal Bansal galat hai..............
ReplyDeleteDon't mislead the innocent people...............
Correct it otherwise strict measures will be taken against the author.....................
//By Er. Praveen Kumar Soni(66).............
//By Er. Ishan Makkar.........................