Hello everyone,
I am trying to get an ActionListener to work in Java.
On lines 310 and 312 it says 'cannot find symbol: variable calcDisplayF'.
I am a complete noob when it comes to Event Handling so any help on this
subject would be greatly appreciated.
Here is the code (warning, it's long (315)):
/*
* Creating here for development
*
* Please save all questions until the end of class...
*/
package macey;
/**
* @author Mike
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class normCalcDev implements ActionListener {
public void normCalc() {
// Content Pane
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel spacingL = new JLabel(" ");
c.gridy=1;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(spacingL, c);
// Contents
JButton oneB = new JButton("1");
c.gridy=2;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
oneB.addActionListener(this);
content.add(oneB, c);
JButton twoB = new JButton("2");
c.gridy=2;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(twoB, c);
JButton threeB = new JButton("3");
c.gridy=2;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(threeB, c);
JButton fourB = new JButton("4");
c.gridy=3;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(fourB, c);
JButton fiveB = new JButton("5");
c.gridy=3;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(fiveB, c);
JButton sixB = new JButton("6");
c.gridy=3;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(sixB, c);
JButton sevenB = new JButton("7");
c.gridy=4;
c.gridx=0;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(sevenB, c);
JButton eightB = new JButton("8");
c.gridy=4;
c.gridx=1;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(eightB, c);
JButton nineB = new JButton("9");
c.gridy=4;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(nineB, c);
JButton plusB = new JButton("+");
c.gridy=5;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(plusB, c);
JButton minusB = new JButton("-");
c.gridy=4;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=3;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(minusB, c);
JButton multB = new JButton("*");
c.gridy=2;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=2;
c.ipady=0;
c.insets=new Insets(0, 20, 5, 20);
content.add(multB, c);
JButton divB = new JButton("/");
c.gridy=3;
c.gridx=3;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=4;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(divB, c);
JButton expB = new JButton("^");
c.gridy=4;
c.gridx=8;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=29;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(expB, c);
JButton equalsB = new JButton("=");
c.gridy=5;
c.gridx=5;
c.ipadx=16;
c.gridwidth=1;
c.gridheight=1;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 20);
content.add(equalsB, c);
JButton cB = new JButton("C");
c.gridy=4;
c.gridx=7;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=7;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(cB, c);
JButton ceB = new JButton("CE");
c.gridy=3;
c.gridx=7;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(ceB, c);
JButton roundB = new JButton("Round");
c.gridy=3;
c.gridx=8;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(roundB, c);
JButton pointB = new JButton(".");
c.gridy=5;
c.gridx=2;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=4;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(pointB, c);
JButton sqrtB = new JButton("sqrt");
c.gridy=2;
c.gridx=5;
c.gridwidth=1;
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 20);
content.add(sqrtB, c);
JButton modB = new JButton("%");
c.gridy=4;
c.gridx=5;
c.ipadx=14;
c.gridwidth=1;
c.gridheight=1;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 20);
content.add(modB, c);
JButton piB = new JButton("pi");
c.gridy=3;
c.gridx=5;
c.ipadx=13;
c.gridwidth=1;
c.gridheight=1;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 20);
content.add(piB, c);
JButton posnegB = new JButton("+/-");
c.gridy=5;
c.gridx=7;
c.gridheight=1;
c.ipadx=76;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
c.gridwidth=2;
content.add(posnegB, c);
JButton zeroB = new JButton("0");
c.gridy=5;
c.gridx=0;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=47;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 5);
content.add(zeroB, c);
JButton bkspB = new JButton("Backspace");
c.gridy=2;
c.gridx=7;
c.gridwidth=2;
c.gridheight=1;
c.ipadx=26;
c.ipady=0;
c.insets=new Insets(0, 0, 5, 0);
content.add(bkspB, c);
JTextField calcDisplayF = new JTextField();
calcDisplayF.setEnabled(false);
c.gridy=0;
c.gridx=0;
c.gridwidth=10;
c.ipadx=412;
c.gridheight=1;
c.ipady=0;
c.insets=new Insets(0, 0, 0, 0);
content.add(calcDisplayF, c);
// Menubar
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem fileQuit = new JMenuItem("Quit");
fileQuit.setMnemonic('Q');
fileMenu.add(fileQuit);
menuBar.add(fileMenu);
JMenu optionsMenu = new JMenu("Options");
JMenuItem piLength = new JMenuItem("pi Length");
optionsMenu.add(piLength);
menuBar.add(optionsMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem helpTutorial = new JMenuItem("Tutorial");
helpTutorial.setMnemonic('T');
helpMenu.add(helpTutorial);
menuBar.add(helpMenu);
// Main Window
JFrame normCalcWin = new JFrame("Calculator");
normCalcWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
normCalcWin.setSize(440, 260);
normCalcWin.setLocation(425, 275);
normCalcWin.setContentPane(content);
normCalcWin.setJMenuBar(menuBar);
normCalcWin.setResizable(false);
normCalcWin.setVisible(true);
}
public void main(String[] args) {
// Creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
normCalc();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("1") != false) {
calcDisplayF.setText("1");
} else {
calcDispalyF.setText("Other Button");
}
}
}