File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help with calculator prgram

 
dave ash
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i am currently writing a calculator which looks and acts like the windows calculator, i have got the layout and buttons to appear but that's it. i am having trouble with the rest, how would i make the buttons work, like when 1 is clicked a 1 apears in the textfield etc. can anybody offer any asistance??

thanks in advance.

here is the code i have so far

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Name: Dave Ash


public class Calculator extends JFrame implements ActionListener
{
JButton[] btnNums;
JButton btnBack;
JButton btnClear;
JButton btnCalculate;

JTextField txtDisplay;

public Calculator()
{
Container content = getContentPane();
setSize(350,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel jpDisplay = new JPanel();
jpDisplay.setLayout(new BorderLayout());
txtDisplay = new JTextField(15);
jpDisplay.add(txtDisplay);


JPanel jpRow2 = new JPanel();
btnBack = new JButton("Backspace");
btnClear = new JButton("Clear");
jpRow2.add(btnBack);
jpRow2.add(btnClear);


String[] strNames = {"MC","7","8", "9","/","sqrt","MR","4", "5", "6","*","%","MS","1", "2", "3","+","1x","M+","0", "+/-", ".", "-","=",};
btnNums = new JButton[24];
JPanel jpButtons = new JPanel();
jpButtons.setLayout(new GridLayout(4,6));

for (int i = 0; i < 24; i++)
{
btnNums[i] = new JButton(strNames[i]);
btnNums[i].addActionListener(this);
jpButtons.add(btnNums[i]);
}


setContentPane(content);
setLayout(new FlowLayout());
content.add(jpDisplay, BorderLayout.NORTH);
content.add(jpRow2);
content.add(jpButtons);
setTitle("Mini Calculator");
setVisible(true);




}

public void actionPerformed(ActionEvent ae)
{
}

public static void main(String[] args)
{
Calculator calc = new Calculator();
}
}
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following line throws an error
.Please comment it.

I have given the sample statement to print the value in the textfield. but its not an ideal approach.

try and post your comment if it works.
bye for now
sat
 
dave ash
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was thinking of useing something like an actionlistner for the button's but dont have a clue of how to do that.

thanks for the advice but when i comment out the code my backspace and clear disapear,
i am useing textpad to program my code.
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sat must be using a 1.4 or earlier JDK. Your code does not throw an exception in JDK 1.5.
Have a look at the Java Tutorial on Swing for how to hook up an ActionListener.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic