注:文章原地址:blog.csdn.net/weixin_5237…
Windows系统自带计算器的简单实现
- 布局方式:
整体采用BorderLayout布局方式,分为NORTH和SOUTH两部分。
NORTH部分为一个文本域(JTextArea)。
SOUTH部分采用GridLayout布局,分成6行4列,用来添加按钮。
- 代码结构
代码分为GUI界面的实现和事件的处理两部分。 GUI界面重点为按钮的添加(采用循环方式,可减少代码量)。 事件处理重点是准确获取事件发生源和文本域字符串的获取和设置。
-
代码实现
GUI界面
package com.calculator.exer1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame {
JButton jb;
Calculator(){
super("计算器");
setResizable(false); //设置窗口尺寸不能改变
// setSize(365,650);
// setLocation(1000,50);
setBounds(1000,50,365,650);
surface();
setVisible(true);
}
public void surface(){
setLayout(new BorderLayout()); //设置整体布局为BorderLayout
//NORTH部分
JPanel topPanel = new JPanel();
JTextArea jt = new JTextArea("0",5,22);
jt.setFont(new Font("宋体",Font.BOLD,30));
jt.setEditable(false);
topPanel.add(jt);
add(topPanel,BorderLayout.NORTH);
//SOUTH部分
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(6,4,6,6));
String[] strs = {
"%","CE","C","Del",
"1/x","x²","√x","/",
"7","8","9","*",
"4","5","6","-",
"1", "2","3","+",
"+/_","0",".","="
};
Event event = new Event(jt); //监听器的创建
for(int i =0;i < strs.length; i++){
if(strs[i].equals("=")){
jb = new JButton(strs[i]);
jb.setBackground(Color.blue); //设置背景颜色
jb.setForeground(Color.white); //设置字体颜色
jb.setFont(new Font("宋体",Font.BOLD,30));//设置字体参数
bottomPanel.add(jb);
jb.addActionListener(event);
}else{
jb = new JButton(strs[i]);
jb.setBackground(Color.white);
jb.setFont(new Font("宋体",Font.BOLD,20));
bottomPanel.add(jb);
jb.addActionListener(event);
}
}
add(bottomPanel);
}
public static void main(String[] args) {
new Calculator();
}
}
事件处理
package com.calculator.exer1;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Event implements ActionListener {
JTextArea jt = new JTextArea();
StringBuffer sb = new StringBuffer();
Event(JTextArea jt){
this.jt = jt;
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand(); //返回与此动作相关的命令字符串。
String temp = jt.getText();
switch (command){
case "1": case "2": case "3": case "4": case "5":
case "6": case "7": case "8": case "9": case "0":
case "+": case "-": case "*": case "/": case ".":
sb.append(command);
jt.setText(sb.toString());
break;
case "%": case "CE": case "C":
sb.setLength(0);
jt.setText("0");
break;
case "Del":
sb.deleteCharAt(sb.length()-1);
jt.setText(temp.substring(0,temp.length()-1)); //返回该字符串的子字符串(去除原字符串尾部的一位,从而达到删除最后一位的目的)
break;
case "1/x":
double ans1 = Double.valueOf(jt.getText());
jt.setText(String.valueOf(1 / ans1));
sb.setLength(0);
break;
case "x²":
double ans2 = Double.valueOf(jt.getText());
jt.setText(String.valueOf(Math.pow(ans2,2)));
sb.setLength(0);
break;
case "√x":
double ans3 = Double.valueOf(jt.getText());
jt.setText(String.valueOf(Math.pow(ans3,0.5)));
sb.setLength(0);
break;
case "=":
if (temp.contains("+")) {
String[] string = temp.split("\\+"); //分隔字符串
double num1 = Double.valueOf(string[0]);
double num2 = Double.valueOf(string[1]);
jt.setText(String.valueOf(num1 + num2));
sb.setLength(0);
} else if (temp.contains("-")) {
String[] string = temp.split("-");
double num1 = Double.valueOf(string[0]);
double num2 = Double.valueOf(string[1]);
jt.setText(String.valueOf(num1 - num2));
sb.setLength(0);
} else if (temp.contains("*")) {
String[] string = temp.split("\\*");
double num1 = Double.valueOf(string[0]);
double num2 = Double.valueOf(string[1]);
jt.setText(String.valueOf(num1 * num2));
sb.setLength(0);
} else if (temp.contains("/")) {
String[] string = temp.split("\\/");
double num1 = Double.valueOf(string[0]);
double num2 = Double.valueOf(string[1]);
jt.setText(String.valueOf(num1 / num2));
sb.setLength(0);
}
break;
}
}
}
- 最终呈现