我用java实现计算器时,将键盘的退格键连接了按钮退格键,为什么我一按下键盘的退格键就会发出提示音,而按下退格按钮则不会,怎么解决
时间: 2024-01-27 16:03:04 浏览: 144
这是因为键盘的退格键和按钮的退格键的事件处理方式不同。键盘的退格键在处理事件时会触发系统提示音,而按钮的退格键则不会。要解决这个问题,你可以在按钮的事件处理方法中屏蔽掉系统提示音,可以使用如下代码:
```java
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 屏蔽系统提示音
e.consume();
// 处理退格事件
// ...
}
});
```
在事件处理方法中使用 `e.consume()` 可以屏蔽事件的后续处理,包括系统提示音。这样就可以避免在按下按钮退格键时发出系统提示音的问题。
相关问题
用Java实现实现计算器,backspace为退格键,单目运算符:+/-,1/x,sqrt,%双目运算符:+,-,*,/。CE为清除,C为清除全部,M+,M-,MR,MC,M+表示将当前显示的存储数值进行加法运算
以下是一个简单的Java计算器实现,包括上述要求的功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton display;
private JButton backspace;
private JButton plusMinus;
private JButton reciprocal;
private JButton squareRoot;
private JButton percent;
private JButton clearEntry;
private JButton clearAll;
private JButton memoryPlus;
private JButton memoryMinus;
private JButton memoryRecall;
private JButton memoryClear;
private JButton divide;
private JButton multiply;
private JButton subtract;
private JButton add;
private JButton equals;
private double memory = 0;
private double currentNumber = 0;
private double result = 0;
private char lastOperator;
private boolean startNewNumber = true;
public Calculator() {
super("Calculator");
display = new JButton("0");
display.setEnabled(false);
backspace = new JButton("Backspace");
backspace.addActionListener(this);
plusMinus = new JButton("+/-");
plusMinus.addActionListener(this);
reciprocal = new JButton("1/x");
reciprocal.addActionListener(this);
squareRoot = new JButton("sqrt");
squareRoot.addActionListener(this);
percent = new JButton("%");
percent.addActionListener(this);
clearEntry = new JButton("CE");
clearEntry.addActionListener(this);
clearAll = new JButton("C");
clearAll.addActionListener(this);
memoryPlus = new JButton("M+");
memoryPlus.addActionListener(this);
memoryMinus = new JButton("M-");
memoryMinus.addActionListener(this);
memoryRecall = new JButton("MR");
memoryRecall.addActionListener(this);
memoryClear = new JButton("MC");
memoryClear.addActionListener(this);
divide = new JButton("/");
divide.addActionListener(this);
multiply = new JButton("*");
multiply.addActionListener(this);
subtract = new JButton("-");
subtract.addActionListener(this);
add = new JButton("+");
add.addActionListener(this);
equals = new JButton("=");
equals.addActionListener(this);
JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5));
buttonPanel.add(memoryClear);
buttonPanel.add(memoryRecall);
buttonPanel.add(memoryMinus);
buttonPanel.add(memoryPlus);
buttonPanel.add(percent);
buttonPanel.add(squareRoot);
buttonPanel.add(reciprocal);
buttonPanel.add(divide);
buttonPanel.add(7);
buttonPanel.add(8);
buttonPanel.add(9);
buttonPanel.add(multiply);
buttonPanel.add(4);
buttonPanel.add(5);
buttonPanel.add(6);
buttonPanel.add(subtract);
buttonPanel.add(1);
buttonPanel.add(2);
buttonPanel.add(3);
buttonPanel.add(add);
buttonPanel.add(plusMinus);
buttonPanel.add(0);
buttonPanel.add(".");
buttonPanel.add(equals);
buttonPanel.add(clearEntry);
buttonPanel.add(clearAll);
buttonPanel.add(backspace);
JPanel displayPanel = new JPanel(new BorderLayout());
displayPanel.add(display, BorderLayout.CENTER);
add(displayPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
setSize(300, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void handleNumberInput(int number) {
if (startNewNumber) {
display.setText(String.valueOf(number));
currentNumber = number;
startNewNumber = false;
} else {
String current = display.getText();
if (current.equals("0")) {
display.setText(String.valueOf(number));
} else {
display.setText(current + number);
}
currentNumber = Double.parseDouble(display.getText());
}
}
private void handleOperatorInput(char operator) {
if (startNewNumber) {
lastOperator = operator;
} else {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = operator;
}
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == backspace) {
String current = display.getText();
if (current.length() > 1) {
display.setText(current.substring(0, current.length() - 1));
currentNumber = Double.parseDouble(display.getText());
} else {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
}
} else if (source == plusMinus) {
currentNumber = -currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == reciprocal) {
currentNumber = 1 / currentNumber;
display.setText(String.valueOf(currentNumber));
} else if (source == squareRoot) {
currentNumber = Math.sqrt(currentNumber);
display.setText(String.valueOf(currentNumber));
} else if (source == percent) {
handleOperatorInput('%');
} else if (source == clearEntry) {
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == clearAll) {
result = 0;
lastOperator = ' ';
display.setText("0");
currentNumber = 0;
startNewNumber = true;
} else if (source == memoryPlus) {
memory += currentNumber;
} else if (source == memoryMinus) {
memory -= currentNumber;
} else if (source == memoryRecall) {
currentNumber = memory;
display.setText(String.valueOf(currentNumber));
} else if (source == memoryClear) {
memory = 0;
} else if (source == divide) {
handleOperatorInput('/');
} else if (source == multiply) {
handleOperatorInput('*');
} else if (source == subtract) {
handleOperatorInput('-');
} else if (source == add) {
handleOperatorInput('+');
} else if (source == equals) {
switch (lastOperator) {
case '+':
result += currentNumber;
break;
case '-':
result -= currentNumber;
break;
case '*':
result *= currentNumber;
break;
case '/':
result /= currentNumber;
break;
case '%':
result %= currentNumber;
break;
default:
result = currentNumber;
break;
}
display.setText(String.valueOf(result));
currentNumber = result;
startNewNumber = true;
lastOperator = ' ';
} else {
handleNumberInput((Integer) source);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这个实现使用了Java的Swing GUI库来创建一个带有按钮和文本框的窗口,并在按钮上添加了相应的事件处理程序来实现计算器的功能。
使用java编写以下程序自行设计一个计算器,完成简单计算器功能,如下图。 特别需要注意的几个操作: (1)小数点的处理 (2)连加,连减等连续运算 (3)清空(C键)和清零(CE键)的区别 (4)后退键的实现(采用两种方式实现:自己编写;调用键盘退格键) 界面可自行设计,不必拘泥于现有计算器。
以下是使用Java编写的简单计算器程序:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {"C", "CE", "<-", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "+/-", "0", ".", "="};
private boolean isDecimal = false; // 是否输入了小数点
private boolean isOperator = false; // 是否输入了操作符
private boolean isEqual = false; // 是否已经按下了等号
private String currentText = ""; // 当前输入的文本
private String operator = ""; // 当前的操作符
private double result = 0.0; // 计算结果
public Calculator() {
super("Calculator");
// 设置窗口大小
setSize(300, 400);
// 设置布局
setLayout(new BorderLayout());
// 创建文本框
display = new JTextField();
display.setEditable(false);
add(display, BorderLayout.NORTH);
// 创建按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4, 5, 5));
// 创建按钮
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
// 添加按钮面板
add(buttonPanel, BorderLayout.CENTER);
// 设置窗口可见
setVisible(true);
}
// 处理按钮事件
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// 清空
if (command.equals("C")) {
display.setText("");
isDecimal = false;
isOperator = false;
isEqual = false;
currentText = "";
operator = "";
result = 0.0;
}
// 清零
else if (command.equals("CE")) {
display.setText("");
isDecimal = false;
isOperator = false;
currentText = "";
}
// 后退
else if (command.equals("<-")) {
String text = display.getText();
if (text.length() > 0) {
text = text.substring(0, text.length() - 1);
display.setText(text);
}
}
// 数字和小数点
else if (command.equals("0") || command.equals("1") || command.equals("2") || command.equals("3") || command.equals("4") || command.equals("5") || command.equals("6") || command.equals("7") || command.equals("8") || command.equals("9") || command.equals(".")) {
if (isEqual) {
display.setText("");
isEqual = false;
}
if (command.equals(".")) {
if (!isDecimal) {
isDecimal = true;
} else {
return;
}
}
display.setText(display.getText() + command);
}
// 操作符
else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) {
if (isOperator) {
calculate();
}
operator = command;
result = Double.parseDouble(display.getText());
isDecimal = false;
isOperator = true;
isEqual = false;
}
// 取反
else if (command.equals("+/-")) {
String text = display.getText();
if (text.length() > 0 && !text.equals("0")) {
if (text.startsWith("-")) {
text = text.substring(1);
} else {
text = "-" + text;
}
display.setText(text);
}
}
// 计算结果
else if (command.equals("=")) {
calculate();
operator = "";
isDecimal = false;
isOperator = false;
isEqual = true;
}
}
// 计算结果
private void calculate() {
double num = Double.parseDouble(display.getText());
if (operator.equals("+")) {
result += num;
} else if (operator.equals("-")) {
result -= num;
} else if (operator.equals("*")) {
result *= num;
} else if (operator.equals("/")) {
result /= num;
}
display.setText(Double.toString(result));
isDecimal = false;
isOperator = false;
}
public static void main(String[] args) {
new Calculator();
}
}
```
该程序使用了Swing库来创建界面和按钮,并通过实现ActionListener接口来处理按钮事件。在处理按钮事件时,程序根据按钮的文本来执行相应的操作,例如清空、清零、后退、输入数字和小数点、输入操作符、取反和计算结果。程序还使用了一些布尔变量来记录当前的状态,例如是否输入了小数点、是否输入了操作符和是否已经按下了等号。在计算结果时,程序使用了一个result变量来保存中间结果,并根据当前的操作符来执行相应的运算。
阅读全文