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();
}
}
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();
}
}