Long story short:
I am writing an object-oriented calculator that I hope will have some of the functionality of the Windows calculator. So far, I have written two GUI's; one displays in an undesirable way but is fully integrated with the logic module and therefore can perform basic arithmetic calculations; the other displays as I want it to display but is not integrated with the logic module and therefore does not perform any calculations.
I have some idea why the latter GUI is not performing as I want it to, but I have less of a idea how to get it to do so.
The code is rather long so I'm not sure exactly how much to post, but here is some of it:
import javax.swing.*; class CalculatorScreen extends JPanel { JTextField screen = new JTextField("0", 15); CalculatorScreen() { add(screen); } }import java.awt.*; import java.awt.event.*; import javax.swing.*; class NumberPad extends JPanel { JButton button; ActionListener ariLis; NumberPad() { setLayout(new GridLayout(4, 4)); button = new JButton("7"); button.addActionListener(ariLis); add(button); button = new JButton("8"); button.addActionListener(ariLis); add(button); button = new JButton("9"); button.addActionListener(ariLis); add(button); button = new JButton("+"); button.addActionListener(ariLis); add(button); button = new JButton("4"); button.addActionListener(ariLis); add(button); button = new JButton("5"); button.addActionListener(ariLis); add(button); button = new JButton("6"); button.addActionListener(ariLis); add(button); button = new JButton("-"); button.addActionListener(ariLis); add(button); button = new JButton("1"); button.addActionListener(ariLis); add(button); button = new JButton("2"); button.addActionListener(ariLis); add(button); button = new JButton("3"); button.addActionListener(ariLis); add(button); button = new JButton("*"); button.addActionListener(ariLis); add(button); button = new JButton("."); button.addActionListener(ariLis); add(button); button = new JButton("0"); button.addActionListener(ariLis); add(button); button = new JButton("="); button.addActionListener(ariLis); add(button); button = new JButton("/"); button.addActionListener(ariLis); add(button); } }import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Stack; import javax.swing.JTextField; import java.lang.*; public class ArithmeticListener implements ActionListener { boolean optPsh = false; boolean pntPsh = false; double fOpd; double sOpd; double res; String ariStr = ""; String digStr = ""; String pntStr = ""; String opdStr = ""; String curOpdStr = ""; String prvOpdStr = ""; String curOptStr = ""; String prvOptStr = ""; Stack<String> expStck = new Stack<String> (); Stack<String> opdStck = new Stack<String> (); Stack<String> optStck = new Stack<String> (); JTextField scrn; ArithmeticListener(CalculatorGUI calGUI) { scrn = calGUI.screen; } ArithmeticListener(GUI gui) { scrn = gui.calcScrn.screen; } public void actionPerformed(ActionEvent ae) { /*enter numbers and perform calculations*/ } }
The basic problem, as I understand it, is that when the main method in the GUI class calls the GUI the GUI construct ariLis isn't initialized, so when it is added to the buttons in the number pad it is added as an object reference without a referent object. (Why isn't that throw a NullPointerException?) What I am not so sure how to do is to add the actual ArithmeticListener object to the buttons in the number pad after the GUI has been initialized.
Any suggestions?