Java Calculator HELP!
807580Jan 13 2010 — edited Jan 13 2010Hi all,
I am new to Java and for my computer science class I have to make a calculator with GUI.
I got the GUI part but I dont know anything about the coding part, like the actionlisteners and stuff.
Can someone help me out with the code? Right now i am having problems with the transfering Jbutton info to Jtextfield and also
keep the text in Jtextfield so that when I press a new button the number in the text field doesn't get replaced.
Here's what I have so far:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
public class calcu extends JFrame implements ActionListener{
JFrame frame;
JTextField txtOut;
JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btnAdd,btnMinus,btnDivision,btnMUL,btnDec,btnAC,btnEqual;
JPanel btnPanel;
double num1,num2,total;
public calcu(){
frame = new JFrame("Ken's Calculator");
frame.setVisible(true);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JFrame.setDefaultLookAndFeelDecorated(true);
txtOut = new JTextField(10);
btn1 = new JButton("1");
btn2 = new JButton("2");
btn3 = new JButton("3");
btn4 = new JButton("4");
btn5 = new JButton("5");
btn6 = new JButton("6");
btn7 = new JButton("7");
btn8 = new JButton("8");
btn9 = new JButton("9");
btn0 = new JButton("0");
btnAdd = new JButton("+");
btnMinus = new JButton("-");
btnDivision = new JButton("/");
btnMUL = new JButton("*");
btnDec = new JButton(".");
btnAC = new JButton("AC");
btnEqual = new JButton("=");
btn1.setSize(50,50);
btn2.setSize(50,50);
btn3.setSize(50,50);
btn4.setSize(50,50);
btn5.setSize(50,50);
btn6.setSize(50,50);
btn7.setSize(50,50);
btn8.setSize(50,50);
btn9.setSize(50,50);
btnAdd.setSize(50,50);
btnMinus.setSize(50,50);
btnDivision.setSize(50,50);
btnMUL.setSize(50,50);
btnDec.setSize(50,50);
btn0.setSize(50,50);
btnAC.setSize(50,50);
btnEqual.setSize(50,50);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
btnAdd.addActionListener(this);
btnMinus.addActionListener(this);
btnDivision.addActionListener(this);
btnDec.addActionListener(this);
btnAC.addActionListener(this);
btnMUL.addActionListener(this);
txtOut.addActionListener(this);
btnPanel = new JPanel();
btnPanel.setLayout(new GridLayout(4,3));
btnPanel.add(btn7);
btnPanel.add(btn8);
btnPanel.add(btn9);
btnPanel.add(btnAdd);
btnPanel.add(btn4);
btnPanel.add(btn5);
btnPanel.add(btn6);
btnPanel.add(btnMinus);
btnPanel.add(btn1);
btnPanel.add(btn2);
btnPanel.add(btn3);
btnPanel.add(btnDivision);
btnPanel.add(btn0);
btnPanel.add(btnDec);
btnPanel.add(btnAC);
btnPanel.add(btnMUL);
frame.setLayout(new BorderLayout());
frame.add(btnPanel, BorderLayout.CENTER);
frame.add(txtOut, BorderLayout.NORTH);
frame.add(btnEqual, BorderLayout.SOUTH);
}
public static void main(String[] args) {
calcu Calcu = new calcu();
}
public void actionPerformed(ActionEvent e){
Object Source = e.getSource();
if(e.getSource()== btn1){
txtOut.setText("1");
}else if(e.getSource()==btn2){
txtOut.setText("2");
}else if(e.getSource()==btn3){
txtOut.setText("3");
}else if(e.getSource()==btn4){
txtOut.setText("4");
}else if(e.getSource()==btn5){
txtOut.setText("5");
}else if(e.getSource()==btn6){
txtOut.setText("6");
}else if(e.getSource()==btn7){
txtOut.setText("7");
}else if(e.getSource()==btn8){
txtOut.setText("8");
}else if(e.getSource()==btn9){
txtOut.setText("9");
}else if(e.getSource()==btn0){
txtOut.setText("0");
}
}
}
So can anyone help please?
Also if you can, can you give me more info on the algorithm of how the coding of a Java calculator works?
Cheers,
Ken