B
bilsch
I'm trying to make a program that works like the calculator in Windows
Accessories - all input is from button clicks. Below I listed a
stripped down version of the GUI and a little of the processing behind
it. It is in two small files. I had intended that the program would
display a sequence of digits from button clicks, which I would then
convert into a Double precision number. The problem is I am constrained
to initialize the string within the method that accumulates the sequence
of digits. Everytime the method is called the string is reinitialized
with the result that my sequence of digits is only ever one digit long.
My plan would work if I could initialize the string outside the method,
however variable scope in Java doesn't allow it.
If you try the code below you will see my problem. Alternative
suggestions on how to make a simple calculator will be appreciated.
TIA Bill S.
HERE'S THE GUI:
import java.awt.*;
import javax.swing.*;
public class CalcGUIQ1 extends JFrame {
CrunchQ1 crunchNu = new CrunchQ1(this);
// set up row 1
JPanel row1 = new JPanel();
JTextField number1 = new JTextField(10);
// set up row 2
JPanel row2 = new JPanel();
JButton sev = new JButton("7");
JButton ate = new JButton("8");
JButton nin = new JButton("9");
JButton fou = new JButton("4");
JButton fiv = new JButton("5");
JButton six = new JButton("6");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton tre = new JButton("3");
JButton zro = new JButton("0");
JButton dot = new JButton(".");
public CalcGUIQ1() {
super();
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(2, 1, 10, 10);
setLayout(layout);
//add listeners
dot.addActionListener(crunchNu);
zro.addActionListener(crunchNu);
one.addActionListener(crunchNu);
two.addActionListener(crunchNu);
tre.addActionListener(crunchNu);
fou.addActionListener(crunchNu);
fiv.addActionListener(crunchNu);
six.addActionListener(crunchNu);
sev.addActionListener(crunchNu);
ate.addActionListener(crunchNu);
nin.addActionListener(crunchNu);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.add(number1);
row1.setLayout(layout1);
add(row1);
GridLayout layout2 = new GridLayout(4, 3, 10, 10);
row2.setLayout(layout2);
row2.add (sev);
row2.add (ate);
row2.add (nin);
row2.add (fou);
row2.add (fiv);
row2.add (six);
row2.add (one);
row2.add (two);
row2.add (tre);
row2.add (zro);
row2.add (dot);
add(row2);
pack();
setVisible(true);
}
public static void main(String[] arguments) {
CalcGUIQ1 frame = new CalcGUIQ1();
}
}
HERE'S THE SECOND FILE:
import javax.swing.*;
import java.awt.event.*;
public class CrunchQ1 implements ActionListener{
CalcGUIQ1 gui;
public CrunchQ1(CalcGUIQ1 in) {
gui = in;
}
public void actionPerformed(ActionEvent event){
String strng1 = "";
String btn = event.getActionCommand();
if (btn == ".") {strng1 += btn;}
else if (btn == "0") {strng1 += btn;}
else if (btn == "1") {strng1 += btn;}
else if (btn == "2") {strng1 += btn;}
else if (btn == "3") {strng1 += btn;}
else if (btn == "4") {strng1 += btn;}
else if (btn == "5") {strng1 += btn;}
else if (btn == "6") {strng1 += btn;}
else if (btn == "7") {strng1 += btn;}
else if (btn == "8") {strng1 += btn;}
else if (btn == "9") {strng1 += btn;}
gui.number1.setText(strng1);
}
}
Accessories - all input is from button clicks. Below I listed a
stripped down version of the GUI and a little of the processing behind
it. It is in two small files. I had intended that the program would
display a sequence of digits from button clicks, which I would then
convert into a Double precision number. The problem is I am constrained
to initialize the string within the method that accumulates the sequence
of digits. Everytime the method is called the string is reinitialized
with the result that my sequence of digits is only ever one digit long.
My plan would work if I could initialize the string outside the method,
however variable scope in Java doesn't allow it.
If you try the code below you will see my problem. Alternative
suggestions on how to make a simple calculator will be appreciated.
TIA Bill S.
HERE'S THE GUI:
import java.awt.*;
import javax.swing.*;
public class CalcGUIQ1 extends JFrame {
CrunchQ1 crunchNu = new CrunchQ1(this);
// set up row 1
JPanel row1 = new JPanel();
JTextField number1 = new JTextField(10);
// set up row 2
JPanel row2 = new JPanel();
JButton sev = new JButton("7");
JButton ate = new JButton("8");
JButton nin = new JButton("9");
JButton fou = new JButton("4");
JButton fiv = new JButton("5");
JButton six = new JButton("6");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton tre = new JButton("3");
JButton zro = new JButton("0");
JButton dot = new JButton(".");
public CalcGUIQ1() {
super();
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(2, 1, 10, 10);
setLayout(layout);
//add listeners
dot.addActionListener(crunchNu);
zro.addActionListener(crunchNu);
one.addActionListener(crunchNu);
two.addActionListener(crunchNu);
tre.addActionListener(crunchNu);
fou.addActionListener(crunchNu);
fiv.addActionListener(crunchNu);
six.addActionListener(crunchNu);
sev.addActionListener(crunchNu);
ate.addActionListener(crunchNu);
nin.addActionListener(crunchNu);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.add(number1);
row1.setLayout(layout1);
add(row1);
GridLayout layout2 = new GridLayout(4, 3, 10, 10);
row2.setLayout(layout2);
row2.add (sev);
row2.add (ate);
row2.add (nin);
row2.add (fou);
row2.add (fiv);
row2.add (six);
row2.add (one);
row2.add (two);
row2.add (tre);
row2.add (zro);
row2.add (dot);
add(row2);
pack();
setVisible(true);
}
public static void main(String[] arguments) {
CalcGUIQ1 frame = new CalcGUIQ1();
}
}
HERE'S THE SECOND FILE:
import javax.swing.*;
import java.awt.event.*;
public class CrunchQ1 implements ActionListener{
CalcGUIQ1 gui;
public CrunchQ1(CalcGUIQ1 in) {
gui = in;
}
public void actionPerformed(ActionEvent event){
String strng1 = "";
String btn = event.getActionCommand();
if (btn == ".") {strng1 += btn;}
else if (btn == "0") {strng1 += btn;}
else if (btn == "1") {strng1 += btn;}
else if (btn == "2") {strng1 += btn;}
else if (btn == "3") {strng1 += btn;}
else if (btn == "4") {strng1 += btn;}
else if (btn == "5") {strng1 += btn;}
else if (btn == "6") {strng1 += btn;}
else if (btn == "7") {strng1 += btn;}
else if (btn == "8") {strng1 += btn;}
else if (btn == "9") {strng1 += btn;}
gui.number1.setText(strng1);
}
}