如果按了按钮2,那么就删除输入的文本最后一个字符,把剩下的内容显示在文本框中。
站在用户的角度思考问题,与客户深入沟通,找到浑江网站设计与浑江网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名、雅安服务器托管、企业邮箱。业务覆盖浑江地区。
如果按了按钮8,则将文本框中内容置空,相当于重置。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
class CalculatorPanel extends JPanel {
private JPanel jp; // 中间的面板
private double result; // 计算结果
private boolean start; // 用于判断是否是首次输入,true首次,false不是首次
private boolean flag; // 用于判断是否清空显示区域的值,true需要,false不需要
private String lastCommand; // 用于保存运算符
JButton display; // 显示区域
public CalculatorPanel() {
// 初始化各项值
result = 0;
start = true;
flag = false;
lastCommand = "=";
// 设置CalculatorPanel这个面板的布局为框架布局
setLayout(new BorderLayout());
// 这个按钮用来显示值的区域
display = new JButton("0.0");
display.setEnabled(false);// 按钮样式设置为禁用样式
// 用来初始化,清除用的
JButton clear = new JButton("clear");
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 作初始化的操作
result = 0;
start = true;
flag = false;
lastCommand = "=";
display.setText("0.0");
}
});
jp = new JPanel();
// 将此面板布局设置为网格布局,4行4列
jp.setLayout(new GridLayout(4, 4));
// 实例化监听器对象
NumberAction na = new NumberAction();
CommandAction ca = new CommandAction();
makeButton("7", na);
makeButton("8", na);
makeButton("9", na);
makeButton("/", ca);
makeButton("4", na);
makeButton("5", na);
makeButton("6", na);
makeButton("*", ca);
makeButton("1", na);
makeButton("2", na);
makeButton("3", na);
makeButton("-", ca);
makeButton("0", na);
makeButton(".", na);
makeButton("+", ca);
makeButton("=", ca);
// 将面板,两个按键加入到我的面板,实现计算器的界面
add(display, BorderLayout.NORTH);
add(jp, BorderLayout.CENTER);
add(clear, BorderLayout.SOUTH);
}
private void makeButton(String buttonName, ActionListener al) {
JButton jb = new JButton(buttonName);
jp.add(jb);
jb.addActionListener(al);
}
// 数字监听器
private class NumberAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource(); // 获取事件源对象
String input = jb.getText(); // 获取事件源对象上的标签
if (start) { // 首次输入
// 一上就".",就什么也不做
if (input.equals(".")) {
return;
}
// 如果是"0.0"的话,就清空
if (display.getText().equals("0.0")) {
display.setText("");
}
start = false;// 改变是否首次输入的标记值
} else {
// 判断显示区域的值里面是否已经有".",如果有,输入的又是".",就什么都不做
if (display.getText().indexOf(".") != -1) {
if (input.equals(".")) {
return;
}
}
// 判断显示区域的值里面只有"-",输入的又是".",就什么都不做
if (display.getText().equals("-")) {
if (input.equals(".")) {
return;
}
}
// 判断显示区域的值如果是"0",输入的不是".",就什么也不做
if (display.getText().equals("0")) {
if (!input.equals(".")) {
return;
}
}
}
// 如果我点击了运算符以后,再输入数字的话,就要清空显示区域的值
if (flag) {
display.setText("");
flag = false;// 还原初始值,不需要清空
}
display.setText(display.getText() + input);// 设置显示区域的值
}
}
// 运算符监听器
private class CommandAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource(); // 获取事件源对象
String inputCommand = jb.getText(); // 获取事件源对象上的标签
if (start) {// 首次输入
// 一上就是"-"
if (inputCommand.equals("-")) {
display.setText("-");// 显示区域的内容设置为"-"
start = false;// 改变首次输入的标记
}
} else {
if (!flag) {// 如果flag=false不需要清空显示区的值,就调用方法计算
calculate(Double.parseDouble(display.getText()));// 保存显示区域的值,并计算
}
// 保存你点击的运算符
lastCommand = inputCommand;
flag = true;// 因为我这里已经输入过运算符,
}
}
}
// 计算用的方法
private void calculate(double x) {
if (lastCommand.equals("+")) {
result += x;
} else if (lastCommand.equals("-")) {
result -= x;
} else if (lastCommand.equals("*")) {
result *= x;
} else if (lastCommand.equals("/")) {
result /= x;
} else if (lastCommand.equals("=")) {
result = x;
}
display.setText("" + result);
}
}
class MyFrameUitl {
public static void init(JFrame jFrame, Dimension frameSize, String title,
String iconFileName, boolean resizable) {
Toolkit tk = Toolkit.getDefaultToolkit(); // 系统工具包,可以读取系统信息
Dimension screenSize = tk.getScreenSize();// 获取屏幕大小
// 获取宽高
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
// 找中心点
int centerX = screenWidth / 2;
int centerY = screenHeight / 2;
// 解析窗体大小
int frameWidth = frameSize.width;
int frameHeight = frameSize.height;
// 设置窗体位置
jFrame.setBounds(centerX - frameWidth / 2, centerY - frameHeight / 2,
frameWidth, frameHeight);
jFrame.setTitle(title);// 设置标题
if (iconFileName != null) {
jFrame.setIconImage(tk.getImage(iconFileName));// 设置图标
}
// 设置皮肤com.sun.java.swing.plaf.windows.WindowsLookAndFeel
/**/
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(jFrame);
} catch (Exception e1) {
e1.printStackTrace();
}
jFrame.setResizable(resizable);// 设置窗体是否可以改变大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置程序关闭动作
jFrame.setVisible(true);// 显示窗体
}
}
class CalculatorFrame extends JFrame {
public CalculatorFrame() {
add(new CalculatorPanel());
MyFrameUitl.init(this, new Dimension(400, 300), "简易计算器", null,
false);
this.pack();// 让组件按原始大小显示,并且窗体的大小刚好能够容纳所有的组件
}
}
public class CalculatorTest {
public static void main(String[] args) {
new CalculatorFrame();
}
}
运行结果:
以前写的,给你了
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//删除
private JButton C = new JButton("C ");
//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args) {
new Calculator();//new计算器实例
}
public Calculator(){
//添加组件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注册各个组件监听事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注册四则运算监听
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注册数字监听事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格键,删除一个直到没有字符删除
String inputText = input.getText();
if(inputText.length() 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有运算数字和符号
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE--将文本框置为0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等号
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){//执行加法运算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//减法运算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法运算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法运算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
//格式化最终结果,保留两位小数点
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}