要求:
在上个图形界面实验要求下添加事件监听服务,能够执行某个动作,让计算器能加完善
做一个加法计算器试试手
排版:1、2、3、4、5、6、7、8、9、0、小数点、等于、清空、加法、减法
15个元素,5行3列 网状布局
代码
1 package 计算器; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 import java.awt.Dimension; 6 import java.math.*; 7 public class mine extends JFrame implements ActionListener 8 { 9 int f = 0, fd = 0; 10 double a = 0, b = 0; 11 double t = 10.0; 12 JPanel pan1 = new JPanel(); 13 JPanel pan2 = new JPanel(new GridLayout(5, 3, 1, 3)); 14 //网状布局,5行3列 平行间距1mm 垂直间距3mm 15 JTextField tf = new JTextField();//文本行 16 //定义一系列按钮 17 JButton bu0 = new JButton("0"); 18 JButton bu1 = new JButton("1"); 19 JButton bu2 = new JButton("2"); 20 JButton bu3 = new JButton("3"); 21 JButton bu4 = new JButton("4"); 22 JButton bu5 = new JButton("5"); 23 JButton bu6 = new JButton("6"); 24 JButton bu7 = new JButton("7"); 25 JButton bu8 = new JButton("8"); 26 JButton bu9 = new JButton("9"); 27 JButton bu_jia = new JButton("+"); 28 JButton bu_jian = new JButton("-"); 29 JButton bu_dengyu = new JButton("="); 30 JButton bu_dian = new JButton("."); //小数点,实现 31 JButton bu_clean = new JButton("clean");//清除所有 32 33 public mine() {//构造函数 34 tf.setEditable(false);//不允许编辑 35 36 bu0.setActionCommand("0"); 37 bu1.setActionCommand("1"); 38 bu2.setActionCommand("2"); 39 bu3.setActionCommand("3"); 40 bu4.setActionCommand("4"); 41 bu5.setActionCommand("5"); 42 bu6.setActionCommand("6"); 43 bu7.setActionCommand("7"); 44 bu8.setActionCommand("8"); 45 bu9.setActionCommand("9"); 46 bu_jia.setActionCommand("+"); 47 bu_jian.setActionCommand("-"); 48 bu_dengyu.setActionCommand("="); 49 bu_dian.setActionCommand("."); 50 bu_clean.setActionCommand("clean"); 51 52 //事件监听监听器,用this加进去 53 bu0.addActionListener(this); 54 bu1.addActionListener(this); 55 bu2.addActionListener(this); 56 bu3.addActionListener(this); 57 bu4.addActionListener(this); 58 bu5.addActionListener(this); 59 bu6.addActionListener(this); 60 bu7.addActionListener(this); 61 bu8.addActionListener(this); 62 bu9.addActionListener(this); 63 bu_jia.addActionListener(this); 64 bu_jian.addActionListener(this); 65 bu_dengyu.addActionListener(this); 66 bu_dian.addActionListener(this); 67 bu_clean.addActionListener(this); 68 69 tf.setPreferredSize(new Dimension(465, 40)); 70 //这儿是网格布局,这里就是按照顺序加入按钮 71 pan2.add(bu1); 72 pan2.add(bu2); 73 pan2.add(bu3); 74 pan2.add(bu4); 75 pan2.add(bu5); 76 pan2.add(bu6); 77 pan2.add(bu7); 78 pan2.add(bu8); 79 pan2.add(bu9); 80 pan2.add(bu0); 81 pan2.add(bu_dian); 82 pan2.add(bu_dengyu); 83 pan2.add(bu_clean); 84 pan2.add(bu_jia); 85 pan2.add(bu_jian); 86 this.setTitle("计算器"); 87 88 this.add(pan2, BorderLayout.CENTER); 89 this.setSize(500, 350); 90 this.setLocationRelativeTo(null); 91 this.setVisible(true);//显示框架 92 this.setResizable(false); 93 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出程序 94 } 95 96 public void actionPerformed(ActionEvent e) 97 { //操作处理 98 if (e.getActionCommand().equals("1")) {opt(1);} //执行对应操作 99 else if (e.getActionCommand().equals("2")) {opt(2);} 100 else if (e.getActionCommand().equals("3")) {opt(3);} 101 else if (e.getActionCommand().equals("4")) {opt(4);} 102 else if (e.getActionCommand().equals("5")) {opt(5);} 103 else if (e.getActionCommand().equals("6")) {opt(6);} 104 else if (e.getActionCommand().equals("7")) {opt(7);} 105 else if (e.getActionCommand().equals("8")) {opt(8);} 106 else if (e.getActionCommand().equals("9")) {opt(9);} 107 else if (e.getActionCommand().equals("0")) {opt(0);} 108 else if (e.getActionCommand().equals("+")) 109 { 110 this.f = 1;tf.setText(""); 111 fd = 0;this.t = 10.0; } 112 else if (e.getActionCommand().equals("-")) 113 { 114 this.f = 2; 115 tf.setText(""); 116 fd = 0; 117 this.t = 10.0; } 118 else if (e.getActionCommand().equals("=")) 119 {calcu(this.a, this.b, this.f); 120 this.a = 0; 121 this.b = 0; 122 this.f = 0; 123 this.t = 10; 124 fd = 0; } 125 else if (e.getActionCommand().equals("clean")) 126 {tf.setText(""); 127 this.a = 0; 128 this.b = 0; 129 this.f = 0; 130 t = 10; 131 fd = 0; 132 } 133 else if (e.getActionCommand().equals(".")) 134 {fd = 1;} 135 136 } 137 public void opt(int number) { 138 //具体操作 139 if (this.f == 0) // the first number 140 { 141 if (fd == 0)// 没小数点 142 {this.a = this.a * 10 + number; 143 tf.setText(String.valueOf((int) this.a)); 144 } 145 else {// 有小数点 146 double dnumber; 147 dnumber = (double) number / t; 148 t = t * 10.0; 149 this.a += dnumber; 150 BigDecimal BB = new BigDecimal(this.a); 151 // 四舍五入取值 152 double dd = BB.setScale(8, BigDecimal.ROUND_HALF_DOWN).doubleValue(); 153 tf.setText(String.valueOf(dd)); 154 } 155 } 156 // this second number 157 else { // 158 t=10; 159 if (fd == 0)// 没小数点 160 {this.b = this.b * 10 + number; 161 tf.setText(String.valueOf((int) this.b)); } 162 else { 163 double dnumber; 164 dnumber = (double) number / t; 165 t = t * 10.0; 166 this.b += dnumber; 167 BigDecimal BB = new BigDecimal(this.b); 168 double dd = BB.setScale(8, BigDecimal.ROUND_HALF_DOWN).doubleValue(); 169 tf.setText(String.valueOf(dd)); 170 } 171 } 172 } 173 public void calcu(double a, double b, int f) { 174 double sum = 789; 175 176 //用switch函数 177 switch (f) { 178 case 0: 179 //无操作直接按等于 180 case 1: // 加 181 sum = a + b; break; 182 case 2: // 减 183 sum = a - b; break; 184 } 185 186 //对结果的一些操作 187 double q1 = sum; 188 int q2 = (int) q1; 189 double q3 = (double) q2; 190 if (q3 == sum) 191 tf.setText(String.valueOf(q2)); 192 else 193 tf.setText(String.valueOf(sum)); 194 this.a = sum; 195 } 196 197 public static void main(String[] args) { 198 mine kk = new mine(); 199 } 200 201 }
执行效果
心得:
对框架、组件、监听、程序退出等有了进一步的理解,
监听可以根据自己需要定义执行不同功能,
把监听与动作触发源如 按钮 进行链接,
对该按钮操作就能触发该监听执行对应的操作
至此,完毕!
原文:https://www.cnblogs.com/lul3/p/11000586.html