When trying to call on a method from another class my method as a error at
private class equalsButton implements ActionListener {
// makes the equals button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("=");
InfixToPostfixParens.convert(operator); // Line with error
}
}
Saying Non Static String cannot be referenced from a static context. The variable isn't static but I still get this problem any ideas?
The Bulk of my main program as it stands now (Sorry for the sloppy coding in places)
/**
* Graphics of the calculator
*
* @author Collin Blake
* @version (2-28-11)
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator extends JFrame {
JTextField textField;
String operator = "";
public SimpleCalculator() {
setTitle("Simple Calculator"); // makes the window for the calculator
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(0, 1));
JPanel topPanel = new JPanel(); // Initilaized for the text window
topPanel.setLayout(new GridLayout());
textField = new JTextField(10);
textField.addActionListener(new NumEntered());
topPanel.add(textField);
add(topPanel);
JPanel centerPanel = new JPanel(); // center panel initilaized
centerPanel.setLayout(new GridLayout(0, 4));
JButton oneButton = new JButton("1"); // initilaized values for each
// button in their menus
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton plusButton = new JButton("+");
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton minusButton = new JButton("-");
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton timesButton = new JButton("*");
JButton zeroButton = new JButton("0");
JButton decimalButton = new JButton(".");
JButton powerButton = new JButton("^");
JButton divideButton = new JButton("/");
JButton LParButton = new JButton("(");
JButton RParButton = new JButton(")");
JButton equalsButton = new JButton("="); // creates equals button
JButton allClearButton = new JButton("AC");
oneButton.addActionListener(new oneButton()); // initilaized values for
// each button's
// actionlistener
twoButton.addActionListener(new twoButton());
threeButton.addActionListener(new threeButton());
plusButton.addActionListener(new plusButton());
fourButton.addActionListener(new fourButton());
fiveButton.addActionListener(new fiveButton());
sixButton.addActionListener(new sixButton());
minusButton.addActionListener(new minusButton());
sevenButton.addActionListener(new sevenButton());
eightButton.addActionListener(new eightButton());
nineButton.addActionListener(new nineButton());
timesButton.addActionListener(new timesButton());
zeroButton.addActionListener(new zeroButton());
decimalButton.addActionListener(new decimalButton());
powerButton.addActionListener(new powerButton());
divideButton.addActionListener(new divideButton());
LParButton.addActionListener(new LParButton());
RParButton.addActionListener(new RParButton());
equalsButton.addActionListener(new equalsButton()); // creates action
// listener for
// equals
allClearButton.addActionListener(new allClearButton());
centerPanel.add(oneButton); // Adds the buttons to the sub panel
centerPanel.add(twoButton);
centerPanel.add(threeButton);
centerPanel.add(plusButton);
centerPanel.add(fourButton);
centerPanel.add(fiveButton);
centerPanel.add(sixButton);
centerPanel.add(minusButton);
centerPanel.add(sevenButton);
centerPanel.add(eightButton);
centerPanel.add(nineButton);
centerPanel.add(timesButton);
centerPanel.add(zeroButton);
centerPanel.add(decimalButton);
centerPanel.add(powerButton);
centerPanel.add(divideButton);
centerPanel.add(LParButton);
centerPanel.add(RParButton);
centerPanel.add(equalsButton);
centerPanel.add(allClearButton);
add(centerPanel); // Creates the panel with buttons
pack();
setLocationRelativeTo(null);
}
private class NumEntered implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// do nothing
}
}
private class oneButton implements ActionListener {
// makes the one button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("1");
operator += "1";
textField.setText(operator);
}
}
private class twoButton implements ActionListener {
// makes the two button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("2");
operator += "2";
textField.setText(operator);
}
}
private class threeButton implements ActionListener {
// makes the three button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("3");
operator += "3";
textField.setText(operator);
}
}
private class fourButton implements ActionListener {
// makes the four button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("4");
operator += "4";
textField.setText(operator);
}
}
private class fiveButton implements ActionListener {
// makes the five button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("5");
operator += "5";
textField.setText(operator);
}
}
private class sixButton implements ActionListener {
// makes the six button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("6");
operator += "6";
textField.setText(operator);
}
}
private class sevenButton implements ActionListener {
// makes the seven button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("7");
operator += "7";
textField.setText(operator);
}
}
private class eightButton implements ActionListener {
// makes the eight button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("8");
operator += "8";
textField.setText(operator);
}
}
private class nineButton implements ActionListener {
// makes the nine button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("9");
operator += "9";
textField.setText(operator);
}
}
private class zeroButton implements ActionListener {
// makes the zerobutton listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("0");
operator += "0";
textField.setText(operator);
}
}
private class plusButton implements ActionListener {
// makes the plus button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("+");
operator += "+";
textField.setText(operator);
}
}
private class minusButton implements ActionListener {
// makes the minus button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("-");
operator += "-";
textField.setText(operator);
}
}
private class timesButton implements ActionListener {
// makes the times button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("*");
operator += "*";
textField.setText(operator);
}
}
private class divideButton implements ActionListener {
// makes the divide button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("/");
operator += "/";
textField.setText(operator);
}
}
private class allClearButton implements ActionListener {
// makes the all clear button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("AC");
operator = "";
textField.setText(operator);
}
}
private class decimalButton implements ActionListener {
// makes the decimal button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(".");
operator += ".";
textField.setText(operator);
}
}
private class equalsButton implements ActionListener {
// makes the equals button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("=");
InfixToPostfixParens.convert(operator);
}
}
private class LParButton implements ActionListener {
// makes the ( button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("(");
operator += "(";
textField.setText(operator);
}
}
private class RParButton implements ActionListener {
// makes the ) button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(")");
operator += ")";
textField.setText(operator);
}
}
private class powerButton implements ActionListener {
// makes the ^ button listener
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("^");
operator += "^";
textField.setText(operator);
}
}
}
The code for the InfixToPostfixParens
import java.util.Stack;
import java.util.EmptyStackException;
import java.util.regex.Pattern;
import java.util.Scanner;
/**
* Translates an infix expression with parentheses to a postfix expression.
*
* @author Koffman & Wolfgang
*/
public class InfixToPostfixParens {
// Nested Class
/** Class to report a syntax error. */
public static class SyntaxErrorException extends Exception {
/**
* Cons
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…