繁体   English   中英

ActionListener在Java计算器中没有响应

[英]actionlistener not responding in java calculator

请从我的初学者的角度看下面的计算器接口代码,当按下它时,“ 1”应该显示,但显然我做错了。 有什么建议吗?

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

    }

}

大量的代码!

您实际上并没有创建ButtonListener更不用说向按钮添加一个了。

您需要使用按钮注册动作监听器。

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

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

编辑

首先,按照上面的步骤1声明按钮。 然后,在内容窗格中,需要像现在添加按钮一样添加按钮。

   contentPane.add(b1);

现在应该显示该按钮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM