aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes advanced calculator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "advanced calculator" Watch "advanced calculator" New topic
Author

advanced calculator

Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
hi friends...
am now preparing a calculator software.. i like to add some advanced things like calendar view, graphs etc..
can you give some idea about overall design and related functions???
thank you
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

For calender view I prefer JCalendar You might find others if you Google.
For graphs and charts I prefer JFreeChart Again, you will find more if you Google.
Without knowing what your advanced calculator is supposed to do, it is very difficult for us to provide ideas.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
thanks Maneesh... actually i have already included arithmetic and logic functions.
i just need to enhance it little bit by showing calenders while clicking an button in calculator.
thats it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
I'm a bit confused: what is the use of a calendar in a calculator?


Ping & DNS - my free Android networking tools app
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Maybe date/time calculations?
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
i said it was just an additional thing like viewing date and time..
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
hi.. i want to display the actual formula for the calculations done by the user in a text field..
do any one have idea about it..
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
help me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
What is "the actual formula for the calculations"? Regular calculators only display numbers; more advanced ones display anything the user types (like parentheses, operators etc.) - is the latter what you intend to do? If so, just put anything in the text field the user types. You can use a KeyListener for that.

If you meant something else, please provide more detail.
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JLabel;

public class calculator extends JFrame implements ActionListener

{
// variables declaration
JLabel frmlbl;
double temp,temp1,result,a,b;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
String com="";
JTextField jtx;
JTextArea txt;
JButton one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3,exp;
JButton plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan;
JMenuBar bar;
JMenu view;
JMenuItem calendar,exit;
JRadioButtonMenuItem standard,scientific;
JSeparator jp;
ButtonGroup bg;
Container cont;
JPanel textPanel,syntpanel,buttonpanel,formpanel;

// constructor
calculator()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
//JPanel lblpanel=new JPanel();
Font font=new Font("Arial",Font.PLAIN,18);
ImageIcon icon = new ImageIcon("home/ambiga/java/Java Programs/Java Theoritical Notes-Good/course/course/week9/sun.gif ");

textpanel.setLayout(new GridLayout(0,2,3,2));
frmlbl=new JLabel(" ",icon, JLabel.RIGHT);
frmlbl.setFont(font);
frmlbl.setHorizontalAlignment(SwingConstants.CENTER);
frmlbl.setBackground(Color.white);
frmlbl.setForeground(Color.black);
frmlbl.setBorder(BorderFactory.createLineBorder(Color.black));
frmlbl.setOpaque(true);
//frmlbl.setIcon(new javax.swing.ImageIcon("home/ambiga/java/Java Programs/Java Theoritical Notes-Good/course/course/week9/sun.gif"));

jtx=new JTextField(17);
jtx.setFont(font);
jtx.setHorizontalAlignment(SwingConstants.CENTER);
jtx.setBackground(Color.black);
jtx.setMaximumSize(jtx.getPreferredSize() );
jtx.setForeground(Color.orange);
//jtx.setBorderPainted(false)
jtx.setBorder(BorderFactory.createLineBorder(Color.black));


//adding actions to the keys
jtx.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0' && c<='9')
{
}
else
{
keyevent.consume();
}
}
});
//lblpanel.add(frmlbl);
textpanel.add(frmlbl);
textpanel.add(jtx);

buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(5,0,2,2));

syntpanel=new JPanel();
syntpanel.setLayout(new GridLayout(6,0,2,1));

bar=new JMenuBar();
view=new JMenu("View");
view.setMnemonic(KeyEvent.VK_V);

standard =new JRadioButtonMenuItem("Standard",true);
standard.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.ALT_MASK));
standard.addItemListener(new radiohandler());

scientific =new JRadioButtonMenuItem("Scientific");
scientific.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.ALT_MASK));
scientific.addItemListener(new radiohandler());

//using separator between design and exit
jp=new JSeparator();

exit=new JMenuItem("Exit");
exit.setMnemonic('E');
exit.addActionListener(this);

// creating button groups
bg=new ButtonGroup();
bg.add(standard);
bg.add(scientific);
view.add(standard);
view.add(scientific);
view.add(jp);
view.add(exit);
bar.add(view);
setJMenuBar(bar);

//standard

mr=new JButton("MR");
mr.setForeground(Color.orange);
mr.setBackground(Color.DARK_GRAY);
mr.setBorderPainted(false);
buttonpanel.add(mr);
mr.addActionListener(this);

seven=new JButton("7");
seven.setForeground(Color.orange);
seven.setBackground(Color.GRAY);
seven.setBorderPainted(false);
seven.setOpaque(true);
buttonpanel.add(seven);
seven.addActionListener(this);

eight=new JButton("8");
eight.setForeground(Color.orange);
eight.setBackground(Color.GRAY);
eight.setBorderPainted(false);
buttonpanel.add(eight);
eight.addActionListener(this);

nine=new JButton("9");
nine.setForeground(Color.orange);
nine.setBackground(Color.GRAY);
nine.setBorderPainted(false);
buttonpanel.add(nine);
nine.addActionListener(this);

clr=new JButton("AC");
clr.setForeground(Color.orange);
clr.setBackground(Color.GRAY);
clr.setBorderPainted(false);
buttonpanel.add(clr);
clr.addActionListener(this);

mc=new JButton("MC");
mc.setForeground(Color.orange);
mc.setBackground(Color.DARK_GRAY);
mc.setBorderPainted(false);
buttonpanel.add(mc);
mc.addActionListener(this);

four=new JButton("4");
four.setForeground(Color.orange);
four.setBackground(Color.GRAY);
four.setBorderPainted(false);
buttonpanel.add(four);
four.addActionListener(this);

five=new JButton("5");
five.setForeground(Color.orange);
five.setBackground(Color.GRAY);
five.setBorderPainted(false);
buttonpanel.add(five);
five.addActionListener(this);

six=new JButton("6");
six.setForeground(Color.orange);
six.setBackground(Color.GRAY);
six.setBorderPainted(false);
buttonpanel.add(six);
six.addActionListener(this);

mul=new JButton("*");
mul.setForeground(Color.orange);
mul.setBackground(Color.GRAY);
mul.setBorderPainted(false);
buttonpanel.add(mul);
mul.addActionListener(this);

mp=new JButton("M+");
mp.setForeground(Color.orange);
mp.setBackground(Color.DARK_GRAY);
mp.setBorderPainted(false);
buttonpanel.add(mp);
mp.addActionListener(this);

one=new JButton("1");
one.setForeground(Color.orange);
one.setBackground(Color.GRAY);
one.setBorderPainted(false);
buttonpanel.add(one);
one.addActionListener(this);

two=new JButton("2");
two.setForeground(Color.orange);
two.setBackground(Color.GRAY);
two.setBorderPainted(false);
buttonpanel.add(two);
two.addActionListener(this);

three=new JButton("3");
three.setForeground(Color.orange);
three.setBackground(Color.GRAY);
three.setBorderPainted(false);
buttonpanel.add(three);
three.addActionListener(this);

min=new JButton("-");
min.setForeground(Color.orange);
min.setBackground(Color.GRAY);
min.setBorderPainted(false);
buttonpanel.add(min);
min.addActionListener(this);

mm=new JButton("M-");
mm.setForeground(Color.orange);
mm.setBackground(Color.DARK_GRAY);
mm.setBorderPainted(false);
buttonpanel.add(mm);
mm.addActionListener(this);

zero=new JButton("0");
zero.setForeground(Color.orange);
zero.setBackground(Color.GRAY);
zero.setBorderPainted(false);
buttonpanel.add(zero);
zero.addActionListener(this);

plmi=new JButton("+/-");
plmi.setForeground(Color.orange);
plmi.setBackground(Color.GRAY);
plmi.setBorderPainted(false);
buttonpanel.add(plmi);
plmi.addActionListener(this);

poin=new JButton(".");
poin.setForeground(Color.orange);
poin.setBackground(Color.GRAY);
poin.setBorderPainted(false);
buttonpanel.add(poin);
poin.addActionListener(this);

plus=new JButton("+");
plus.setForeground(Color.orange);
plus.setBackground(Color.GRAY);
plus.setBorderPainted(false);
buttonpanel.add(plus);
plus.addActionListener(this);

rec=new JButton("1/x");
rec.setForeground(Color.orange);
rec.setBackground(Color.DARK_GRAY);
rec.setBorderPainted(false);
buttonpanel.add(rec);
rec.addActionListener(this);

sqrt=new JButton("sqrt");
sqrt.setForeground(Color.orange);
sqrt.setBackground(Color.DARK_GRAY);
sqrt.setBorderPainted(false);
buttonpanel.add(sqrt);
sqrt.addActionListener(this);

lg=new JButton("log");
lg.setForeground(Color.orange);
lg.setBackground(Color.DARK_GRAY);
buttonpanel.add(lg);
lg.setBorderPainted(false);
lg.addActionListener(this);

div=new JButton("/");
div.addActionListener(this);
div.setForeground(Color.orange);
div.setBackground(Color.GRAY);
div.setBorderPainted(false);
buttonpanel.add(div);

eq=new JButton("=");
eq.setForeground(Color.orange);
eq.setBackground(Color.GRAY);
eq.setBorderPainted(false);
buttonpanel.add(eq);
eq.addActionListener(this);

//scientific
sin=new JButton("Sin");
sin.setForeground(Color.white);
sin.setBackground(Color.gray);
sin.setBorderPainted(false);
syntpanel.add(sin);
sin.addActionListener(this);

cos=new JButton("Cos");
cos.setForeground(Color.white);
cos.setBackground(Color.gray);
cos.setBorderPainted(false);
syntpanel.add(cos);
cos.addActionListener(this);

tan=new JButton("Tan");
tan.setForeground(Color.white);
tan.setBackground(Color.gray);
tan.setBorderPainted(false);
syntpanel.add(tan);
tan.addActionListener(this);

pow2=new JButton("x^2");
pow2.setForeground(Color.white);
pow2.setBackground(Color.gray);
pow2.setBorderPainted(false);
syntpanel.add(pow2);
pow2.addActionListener(this);

pow3=new JButton("x^3");
pow3.setForeground(Color.white);
pow3.setBackground(Color.gray);
syntpanel.add(pow3);
pow3.setBorderPainted(false);
pow3.addActionListener(this);

exp=new JButton("Exp");
exp.setForeground(Color.white);
exp.setBackground(Color.gray);
exp.setBorderPainted(false);
syntpanel.add(exp);
exp.addActionListener(this);

cont.add("Center",buttonpanel);
cont.add("North",textpanel);

this.setTitle("Swing Calculator");
this.setSize(450,250);
this.setLocation(300,300);
this.setResizable(true);
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Clear();
}
class radiohandler implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
AbstractButton button=(AbstractButton)ie.getItem();
String label=button.getText();

if(label.equals("Standard"))
{
cont.remove(syntpanel);
validate();
}
if(label.equals("Scientific"))
{
cont.add("West",syntpanel);
validate();
}

}
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("Exit"))
{
System.exit(0);
}
if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}

if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}

if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}

if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}

if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}

if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}

if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}

if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}

if(s.equals("AC"))
{
jtx.setText("");
frmlbl.setText("");
x=0;
y=0;
z=0;
}

if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Log of "+jtx.getText()+" : ");
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("1/x of "+jtx.getText()+" : ");
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Exp"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Exp of "+jtx.getText()+": ");
a=Math.exp(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Square of "+jtx.getText()+" : ");
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Cube of "+jtx.getText()+" : ");
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}

if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}

if(s.equals("+"))
{
if(jtx.getText().equals(""))
{

jtx.setText("");
temp=0;
ch='+';
}
else
{

temp=Double.parseDouble(jtx.getText());
com+=jtx.getText()+" and ";
frmlbl.setText(com);
frmlbl.setText("");
jtx.setText("");

ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}

if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=jtx.getText();//+" from "+com;
frmlbl.setText("");
//frmlbl.setText(com);
frmlbl.setText("");
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}

if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=com+jtx.getText()+" by ";
frmlbl.setText("");
frmlbl.setText(com);
frmlbl.setText("");
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}

if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=com+jtx.getText()+" * ";
frmlbl.setText("");
frmlbl.setText(com);
frmlbl.setText("");
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}

if(s.equals("MC")) [b][/b]
{
try
{
m1=0;
jtx.setText("");
}
catch(Exception ee)
{
System.out.println("Error:"+ee);
}
}

if(s.equals("MR"))
{
try
{
//jtx.setText("");
jtx.setText(jtx.getText() );
}
catch(Exception me)
{
System.out.println("Error:"+me);
}
}

if(s.equals("M+"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1+=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}

if(s.equals("M-"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1-=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
} (ends with here)

if(s.equals("sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Square Root of "+jtx.getText()+" = ");
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Sin"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Sin ( "+jtx.getText()+" ) = ");
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Cos"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Cos ( "+jtx.getText()+" ) = ");
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Tan"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Tan ( "+jtx.getText()+" ) = ");
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':

result=temp+temp1;
frmlbl.setText("");
com=com+temp1+"=";
frmlbl.setText("Addition of "+com);
break;

case '-':
result=temp-temp1;
frmlbl.setText("");
com= temp1 + " from " + com + "=";
frmlbl.setText("Subtraction of "+ com);
break;

case '/':
result=temp/temp1;
frmlbl.setText("");
com=com+temp1+"=";
frmlbl.setText("Division of "+com);
break;

case '*':
result=temp*temp1;
frmlbl.setText("");
com=com + temp1 +"=";
frmlbl.setText("Multipliaction of "+com);
break;
}

jtx.setText("");
jtx.setText(jtx.getText() + result);
//to clear previous and display next value
com="";
z=1;
}
}
jtx.requestFocus();

}

public static void main(String args[])
{
calculator n=new calculator();

}

}


this s my swing calculator.. everything is going fine except MR,M+ functions.. anyone have idea about how to solve it..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Ambika, please use code tags. You can edit your post to add them properly.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
Ambika Nagalakshmi wrote:import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JLabel;

public class calculator extends JFrame implements ActionListener

{
// variables declaration
JLabel frmlbl;
double temp,temp1,result,a,b;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
String com="";
JTextField jtx;
JTextArea txt;
JButton one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3,exp;
JButton plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan;
JMenuBar bar;
JMenu view;
JMenuItem calendar,exit;
JRadioButtonMenuItem standard,scientific;
JSeparator jp;
ButtonGroup bg;
Container cont;
JPanel textPanel,syntpanel,buttonpanel,formpanel;

// constructor
calculator()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
//JPanel lblpanel=new JPanel();
Font font=new Font("Arial",Font.PLAIN,18);
ImageIcon icon = new ImageIcon("home/ambiga/java/Java Programs/Java Theoritical Notes-Good/course/course/week9/sun.gif ");

textpanel.setLayout(new GridLayout(0,2,3,2));
frmlbl=new JLabel(" ",icon, JLabel.RIGHT);
frmlbl.setFont(font);
frmlbl.setHorizontalAlignment(SwingConstants.CENTER);
frmlbl.setBackground(Color.white);
frmlbl.setForeground(Color.black);
frmlbl.setBorder(BorderFactory.createLineBorder(Color.black));
frmlbl.setOpaque(true);
//frmlbl.setIcon(new javax.swing.ImageIcon("home/ambiga/java/Java Programs/Java Theoritical Notes-Good/course/course/week9/sun.gif"));

jtx=new JTextField(17);
jtx.setFont(font);
jtx.setHorizontalAlignment(SwingConstants.CENTER);
jtx.setBackground(Color.black);
jtx.setMaximumSize(jtx.getPreferredSize() );
jtx.setForeground(Color.orange);
//jtx.setBorderPainted(false)
jtx.setBorder(BorderFactory.createLineBorder(Color.black));


//adding actions to the keys
jtx.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0' && c<='9')
{
}
else
{
keyevent.consume();
}
}
});
//lblpanel.add(frmlbl);
textpanel.add(frmlbl);
textpanel.add(jtx);

buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(5,0,2,2));

syntpanel=new JPanel();
syntpanel.setLayout(new GridLayout(6,0,2,1));

bar=new JMenuBar();
view=new JMenu("View");
view.setMnemonic(KeyEvent.VK_V);

standard =new JRadioButtonMenuItem("Standard",true);
standard.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.ALT_MASK));
standard.addItemListener(new radiohandler());

scientific =new JRadioButtonMenuItem("Scientific");
scientific.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.ALT_MASK));
scientific.addItemListener(new radiohandler());

//using separator between design and exit
jp=new JSeparator();

exit=new JMenuItem("Exit");
exit.setMnemonic('E');
exit.addActionListener(this);

// creating button groups
bg=new ButtonGroup();
bg.add(standard);
bg.add(scientific);
view.add(standard);
view.add(scientific);
view.add(jp);
view.add(exit);
bar.add(view);
setJMenuBar(bar);

//standard

mr=new JButton("MR");
mr.setForeground(Color.orange);
mr.setBackground(Color.DARK_GRAY);
mr.setBorderPainted(false);
buttonpanel.add(mr);
mr.addActionListener(this);

seven=new JButton("7");
seven.setForeground(Color.orange);
seven.setBackground(Color.GRAY);
seven.setBorderPainted(false);
seven.setOpaque(true);
buttonpanel.add(seven);
seven.addActionListener(this);

eight=new JButton("8");
eight.setForeground(Color.orange);
eight.setBackground(Color.GRAY);
eight.setBorderPainted(false);
buttonpanel.add(eight);
eight.addActionListener(this);

nine=new JButton("9");
nine.setForeground(Color.orange);
nine.setBackground(Color.GRAY);
nine.setBorderPainted(false);
buttonpanel.add(nine);
nine.addActionListener(this);

clr=new JButton("AC");
clr.setForeground(Color.orange);
clr.setBackground(Color.GRAY);
clr.setBorderPainted(false);
buttonpanel.add(clr);
clr.addActionListener(this);

mc=new JButton("MC");
mc.setForeground(Color.orange);
mc.setBackground(Color.DARK_GRAY);
mc.setBorderPainted(false);
buttonpanel.add(mc);
mc.addActionListener(this);

four=new JButton("4");
four.setForeground(Color.orange);
four.setBackground(Color.GRAY);
four.setBorderPainted(false);
buttonpanel.add(four);
four.addActionListener(this);

five=new JButton("5");
five.setForeground(Color.orange);
five.setBackground(Color.GRAY);
five.setBorderPainted(false);
buttonpanel.add(five);
five.addActionListener(this);

six=new JButton("6");
six.setForeground(Color.orange);
six.setBackground(Color.GRAY);
six.setBorderPainted(false);
buttonpanel.add(six);
six.addActionListener(this);

mul=new JButton("*");
mul.setForeground(Color.orange);
mul.setBackground(Color.GRAY);
mul.setBorderPainted(false);
buttonpanel.add(mul);
mul.addActionListener(this);

mp=new JButton("M+");
mp.setForeground(Color.orange);
mp.setBackground(Color.DARK_GRAY);
mp.setBorderPainted(false);
buttonpanel.add(mp);
mp.addActionListener(this);

one=new JButton("1");
one.setForeground(Color.orange);
one.setBackground(Color.GRAY);
one.setBorderPainted(false);
buttonpanel.add(one);
one.addActionListener(this);

two=new JButton("2");
two.setForeground(Color.orange);
two.setBackground(Color.GRAY);
two.setBorderPainted(false);
buttonpanel.add(two);
two.addActionListener(this);

three=new JButton("3");
three.setForeground(Color.orange);
three.setBackground(Color.GRAY);
three.setBorderPainted(false);
buttonpanel.add(three);
three.addActionListener(this);

min=new JButton("-");
min.setForeground(Color.orange);
min.setBackground(Color.GRAY);
min.setBorderPainted(false);
buttonpanel.add(min);
min.addActionListener(this);

mm=new JButton("M-");
mm.setForeground(Color.orange);
mm.setBackground(Color.DARK_GRAY);
mm.setBorderPainted(false);
buttonpanel.add(mm);
mm.addActionListener(this);

zero=new JButton("0");
zero.setForeground(Color.orange);
zero.setBackground(Color.GRAY);
zero.setBorderPainted(false);
buttonpanel.add(zero);
zero.addActionListener(this);

plmi=new JButton("+/-");
plmi.setForeground(Color.orange);
plmi.setBackground(Color.GRAY);
plmi.setBorderPainted(false);
buttonpanel.add(plmi);
plmi.addActionListener(this);

poin=new JButton(".");
poin.setForeground(Color.orange);
poin.setBackground(Color.GRAY);
poin.setBorderPainted(false);
buttonpanel.add(poin);
poin.addActionListener(this);

plus=new JButton("+");
plus.setForeground(Color.orange);
plus.setBackground(Color.GRAY);
plus.setBorderPainted(false);
buttonpanel.add(plus);
plus.addActionListener(this);

rec=new JButton("1/x");
rec.setForeground(Color.orange);
rec.setBackground(Color.DARK_GRAY);
rec.setBorderPainted(false);
buttonpanel.add(rec);
rec.addActionListener(this);

sqrt=new JButton("sqrt");
sqrt.setForeground(Color.orange);
sqrt.setBackground(Color.DARK_GRAY);
sqrt.setBorderPainted(false);
buttonpanel.add(sqrt);
sqrt.addActionListener(this);

lg=new JButton("log");
lg.setForeground(Color.orange);
lg.setBackground(Color.DARK_GRAY);
buttonpanel.add(lg);
lg.setBorderPainted(false);
lg.addActionListener(this);

div=new JButton("/");
div.addActionListener(this);
div.setForeground(Color.orange);
div.setBackground(Color.GRAY);
div.setBorderPainted(false);
buttonpanel.add(div);

eq=new JButton("=");
eq.setForeground(Color.orange);
eq.setBackground(Color.GRAY);
eq.setBorderPainted(false);
buttonpanel.add(eq);
eq.addActionListener(this);

//scientific
sin=new JButton("Sin");
sin.setForeground(Color.white);
sin.setBackground(Color.gray);
sin.setBorderPainted(false);
syntpanel.add(sin);
sin.addActionListener(this);

cos=new JButton("Cos");
cos.setForeground(Color.white);
cos.setBackground(Color.gray);
cos.setBorderPainted(false);
syntpanel.add(cos);
cos.addActionListener(this);

tan=new JButton("Tan");
tan.setForeground(Color.white);
tan.setBackground(Color.gray);
tan.setBorderPainted(false);
syntpanel.add(tan);
tan.addActionListener(this);

pow2=new JButton("x^2");
pow2.setForeground(Color.white);
pow2.setBackground(Color.gray);
pow2.setBorderPainted(false);
syntpanel.add(pow2);
pow2.addActionListener(this);

pow3=new JButton("x^3");
pow3.setForeground(Color.white);
pow3.setBackground(Color.gray);
syntpanel.add(pow3);
pow3.setBorderPainted(false);
pow3.addActionListener(this);

exp=new JButton("Exp");
exp.setForeground(Color.white);
exp.setBackground(Color.gray);
exp.setBorderPainted(false);
syntpanel.add(exp);
exp.addActionListener(this);

cont.add("Center",buttonpanel);
cont.add("North",textpanel);

this.setTitle("Swing Calculator");
this.setSize(450,250);
this.setLocation(300,300);
this.setResizable(true);
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Clear();
}
class radiohandler implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
AbstractButton button=(AbstractButton)ie.getItem();
String label=button.getText();

if(label.equals("Standard"))
{
cont.remove(syntpanel);
validate();
}
if(label.equals("Scientific"))
{
cont.add("West",syntpanel);
validate();
}

}
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("Exit"))
{
System.exit(0);
}
if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}

if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}

if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}

if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}

if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}

if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}

if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}

if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}

if(s.equals("AC"))
{
jtx.setText("");
frmlbl.setText("");
x=0;
y=0;
z=0;
}

if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Log of "+jtx.getText()+" : ");
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("1/x of "+jtx.getText()+" : ");
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Exp"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Exp of "+jtx.getText()+": ");
a=Math.exp(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Square of "+jtx.getText()+" : ");
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Cube of "+jtx.getText()+" : ");
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}

if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}

if(s.equals("+"))
{
if(jtx.getText().equals(""))
{

jtx.setText("");
temp=0;
ch='+';
}
else
{

temp=Double.parseDouble(jtx.getText());
com+=jtx.getText()+" and ";
frmlbl.setText(com);
frmlbl.setText("");
jtx.setText("");

ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}

if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=jtx.getText();//+" from "+com;
frmlbl.setText("");
//frmlbl.setText(com);
frmlbl.setText("");
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}

if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=com+jtx.getText()+" by ";
frmlbl.setText("");
frmlbl.setText(com);
frmlbl.setText("");
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}

if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
com=com+jtx.getText()+" * ";
frmlbl.setText("");
frmlbl.setText(com);
frmlbl.setText("");
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}



if(s.equals("sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Square Root of "+jtx.getText()+" = ");
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Sin"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Sin ( "+jtx.getText()+" ) = ");
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Cos"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Cos ( "+jtx.getText()+" ) = ");
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("Tan"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
frmlbl.setText("Tan ( "+jtx.getText()+" ) = ");
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':

result=temp+temp1;
frmlbl.setText("");
com=com+temp1+"=";
frmlbl.setText("Addition of "+com);
break;

case '-':
result=temp-temp1;
frmlbl.setText("");
com= temp1 + " from " + com + "=";
frmlbl.setText("Subtraction of "+ com);
break;

case '/':
result=temp/temp1;
frmlbl.setText("");
com=com+temp1+"=";
frmlbl.setText("Division of "+com);
break;

case '*':
result=temp*temp1;
frmlbl.setText("");
com=com + temp1 +"=";
frmlbl.setText("Multipliaction of "+com);
break;
}

jtx.setText("");
jtx.setText(jtx.getText() + result);
//to clear previous and display next value
com="";
z=1;
}
}
jtx.requestFocus();

}

public static void main(String args[])
{
calculator n=new calculator();

}

}

thanks maneesh.. i have coded my problem..
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
help me
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Did you preview your post before you submitted your last post? Half is using Code tags whereas half is using the Quote tags. If you want people to help you, you can take some efforts to help them help you.
I do not think anybody is interested in going through so many lines of code. What you need to do is post a SSCCE code
Please do take out time to go through the link I have provided to understand what SSCCE code means.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Using things like if (f.equals("cos")) . . . looks dreadful to me. You are using a non-object-oriented style and that code would be really difficult to maintain.
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
hi everyone thanks for the help...

i have entirely changed the code that uses both mouse and key listener..
its working perfect..
the problem now is that i want to include next and previous button to display last 5 calculations.
here in this program i have used labels to show calculations.
so i need to store these label values and i want to display it in textfield..

can any help which collection method in java i have to use..

am new to collection...





this is the next and previous buttons mouse listener..
i need to display when we click these buttons

i have attached entire program...

thanks
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79










public static void main(String[] args)
{

new calculator();

}

}

hi everyone thanks for the help...

i have entirely changed the code that uses both mouse and key listener..
it is working perfect..
the problem now is that i want to include next and previous button to display last 5 calculations.
here in this program i have used labels to show calculations.
so i need to store these label values and i want to display it in text field..

can any one help which java collection method in i have to use..

am new to collection...




here is the next and previous buttons mouse listener..
i need to display when we click these buttons

i have entire program here and coded where i have to do changes...

thanks[
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Ambika,
Do you really think anyone would go through so many lines of your code? Would you?
Did you check out what SSCCE means? I had provided you a link in one of my previous post.

More about collections can be found here http://java.sun.com/docs/books/tutorial/collections/index.html
Maya Naga
Ranch Hand

Joined: Nov 09, 2009
Posts: 79
i attached just to get some idea about my program.
i have added code where am storing the string value of all labels that i want to display in text field using next and previous buttons

just give some idea what collection method i want to use ....




thanks maneesh..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: advanced calculator