开发者

actionlistener not responding in java calculator

开发者 https://www.devze.com 2022-12-23 23:32 出处:网络
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?

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.setLayou开发者_运维技巧t(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);

    }

}


That's a lot of code!

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消