Forums Register Login

please help with calculator prgram

+Pie Number of slices to send: Send
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();
}
}
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 was born with webbed fish toes. This tiny ad is my only friend:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 784 times.
Similar Threads
Problem with ActionListener and JButtons (harder than it first seems
button colors, etc.
what i have done
what i have done
unable to remove spaces between buttons(making a calculator)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:33:37.