actionlistener not responding in java calculator

Posted by tokee on Stack Overflow See other posts from Stack Overflow or by tokee
Published on 2010-03-19T00:30:51Z Indexed on 2010/03/19 0:41 UTC
Read the original article Hit count: 354

Filed under:
|

hi, please see calculator interface code below, from my beginners point of view the "1" should display when it's pressed but evidently i'm doing something wrong. any suggestiosn please?

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
*A Class that operates as the framework for a calculator. 
*No calculations are performed in this section
*/
public class CalcFrame extends JPanel 
{
    private CalcEngine calc;

    private JFrame frame;
    private JTextField display;
    private JLabel status;

    /**
     * Constructor for objects of class GridLayoutExample
     */
    //public CalcFrame(CalcEngine engine)
    //{

        //frame.setVisible(true);
       // calc = engine;
       // makeFrame();

    //}
    public CalcFrame() {
    makeFrame();
    calc = new CalcEngine();
}
class ButtonListener implements ActionListener {
  ButtonListener() {
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("1")) {
      System.out.println("1");
    }
  }
}




    /**
     * This allows you to quit the calculator.
     */

    // Alows the class to quit.
    private void quit()
    {
        System.exit(0);


    }

    // Calls the dialog frame with the information about the project.
    private void showAbout()
    {
        JOptionPane.showMessageDialog(frame, 
                    "Declan Hodge and Tony O'Keefe Group Project",
                    "About Calculator Group Project", 
                    JOptionPane.INFORMATION_MESSAGE);
    }


      // ---- swing stuff to build the frame and all its components ----

    /**
     * The following creates a layout of the calculator frame.
     */
    private void makeFrame()
    {
        frame = new JFrame("Group Project Calculator");
        makeMenuBar(frame);

        JPanel contentPane = (JPanel)frame.getContentPane();
        contentPane.setLayout(new BorderLayout(8, 8));
        contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));





        /**
     * Insert a text field
     */
        display = new JTextField(8);
        contentPane.add(display, BorderLayout.NORTH);

        //Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(4, 5));
        JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
        contentPane.add(new JButton("9"));
        contentPane.add(new JButton("8"));
        contentPane.add(new JButton("7"));
        contentPane.add(new JButton("6"));
        contentPane.add(new JButton("5"));
        contentPane.add(new JButton("4"));
        contentPane.add(new JButton("3"));
        contentPane.add(new JButton("2"));
        contentPane.add(new JButton("1"));
        contentPane.add(new JButton("0"));
        contentPane.add(new JButton("+"));
        contentPane.add(new JButton("-"));
        contentPane.add(new JButton("/"));
        contentPane.add(new JButton("*"));
        contentPane.add(new JButton("="));
        contentPane.add(new JButton("C"));
        contentPane.add(new JButton("CE"));
        contentPane.add(new JButton("%"));
        contentPane.add(new JButton("#"));
        //contentPane.add(buttonPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Create the main frame's menu bar.
     * The frame that the menu bar should be added to.
     */










    private void makeMenuBar(JFrame frame)
    {
        final int SHORTCUT_MASK =
            Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();


        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu menu;
        JMenuItem item;

        // create the File menu
        menu = new JMenu("File");
        menubar.add(menu);






        // create the Quit menu with a shortcut "Q" key.
         item = new JMenuItem("Quit");
            item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { quit(); }
                           });
        menu.add(item);

        // Adds an about menu.
        menu = new JMenu("About");
        menubar.add(menu);

        // Displays 
        item = new JMenuItem("Calculator Project");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { showAbout(); }
                           });
        menu.add(item);

    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing