如何给计算器的按键设置事件监视器。

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.*; 
public class Calculator{
public static void main(String args[]) {
      JFrame f1 = new JFrame("计算器");
      f1.setSize(400,300);
      JPanel p1 = new JPanel(new BorderLayout(5,5)); 
      p1.setBorder(new EmptyBorder(5,5,5,5));
      JTextField t1 = new JTextField();
      t1.setColumns(10);
      t1.setHorizontalAlignment(SwingConstants.RIGHT);
      JPanel p2 = new JPanel(new GridLayout(4,4,5,5));
      JButton b1 = new JButton("1");
      JButton b2 = new JButton("2");
      JButton b3 = new JButton("3");
      JButton b4 = new JButton("+");
      JButton b5 = new JButton("4");
      JButton b6 = new JButton("5");
      JButton b7 = new JButton("6");
      JButton b8 = new JButton("-");
      JButton b9 = new JButton("7");
      JButton b10 = new JButton("8");
      JButton b11 = new JButton("9");
      JButton b12 = new JButton("*");
      JButton b13 = new JButton("0");
      JButton b14 = new JButton(".");
      JButton b15 = new JButton("=");
      JButton b16 = new JButton("/");
      p2.add(b1);
      p2.add(b2);
      p2.add(b3);
      p2.add(b4);
      p2.add(b5);
      p2.add(b6);
      p2.add(b7);
      p2.add(b8); 
      p2.add(b9);
      p2.add(b10);
      p2.add(b11);
      p2.add(b12);
      p2.add(b13);
      p2.add(b14);
      p2.add(b15);
      p2.add(b16);
      JPanel p3 = new JPanel();
      p3.add(t1);
      p1.add(p3,BorderLayout.NORTH);
      p1.add(p2,BorderLayout.CENTER);
      f1.add(p1);
      f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f1.setVisible(true);
   } 
}

 

给按钮添加Action事件,我有个现成的计算器程序是否有需要。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m