tags:

views:

180

answers:

2

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);

    }

}
+3  A: 

That's a lot of code!

You don't actually create a ButtonListener let alone add one to a button.

Tom Hawtin - tackline
wow Tom! that's really helpful!
tokee
+2  A: 

You need to register the action listener with the button.

 //Step 1.
 JButton b1 = new JButton("1");

//Step2 register
b1.addActionListener(new ButtonListener());

EDIT

Start by declaring the Buttons as in step 1 above. Then in the content pane you need to add the button similar to the way you are adding the buttons right now.

   contentPane.add(b1);

Now the button should display.

Vincent Ramdhanie
thanks vincent, the only problem is when i add the code above exactly it compiles no problem but the button doesn't display? tony
tokee
@tokee The code I give is not complete. Let me edit the answer a bit and provide a more complete example.
Vincent Ramdhanie
Thanks again, it's definitely getting closer but now when i press "1" it opens in a terminal windows and doesn't display on the calculator screen, any idea why that would be? thanks for your assistance, in case it's not obvious already i'm new to this stuff. tony
tokee
actually it seems to be the system.out.println that i had in by mistake that's causing that, if i take that out the number shows on the panel but doesn't display on screen?
tokee
got it working, eventually, thanks for your assistnace vincent
tokee