用java编写一个计算器并且界面搜索栏下面加三个案件
时间: 2023-08-22 09:04:27 浏览: 94
用java编写一个计算器
5星 · 资源好评率100%
好的,下面是一个简单的Java计算器的实现,包括搜索栏和三个自定义按钮:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame {
private JTextField textField;
private JButton addButton, subtractButton, multiplyButton;
private JButton customButton1, customButton2, customButton3;
private JPanel buttonPanel;
public Calculator() {
super("Calculator");
// 创建搜索栏
JPanel searchPanel = new JPanel();
JLabel searchLabel = new JLabel("Search:");
JTextField searchField = new JTextField(20);
JButton searchButton = new JButton("Go");
searchPanel.add(searchLabel);
searchPanel.add(searchField);
searchPanel.add(searchButton);
// 创建显示屏
textField = new JTextField(20);
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
// 创建按钮面板
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
// 创建数字按钮
for (int i = 0; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(new DigitButtonActionListener(i));
buttonPanel.add(button);
}
// 创建运算符按钮
addButton = new JButton("+");
addButton.addActionListener(new OperationButtonActionListener('+'));
subtractButton = new JButton("-");
subtractButton.addActionListener(new OperationButtonActionListener('-'));
multiplyButton = new JButton("*");
multiplyButton.addActionListener(new OperationButtonActionListener('*'));
JButton divideButton = new JButton("/");
divideButton.addActionListener(new OperationButtonActionListener('/'));
JButton clearButton = new JButton("C");
clearButton.addActionListener(new ClearButtonActionListener());
JButton equalButton = new JButton("=");
equalButton.addActionListener(new EqualButtonActionListener());
// 添加自定义按钮
customButton1 = new JButton("Button1");
customButton1.addActionListener(new CustomButtonActionListener(1));
customButton2 = new JButton("Button2");
customButton2.addActionListener(new CustomButtonActionListener(2));
customButton3 = new JButton("Button3");
customButton3.addActionListener(new CustomButtonActionListener(3));
// 将按钮添加到面板
buttonPanel.add(addButton);
buttonPanel.add(subtractButton);
buttonPanel.add(multiplyButton);
buttonPanel.add(divideButton);
buttonPanel.add(customButton1);
buttonPanel.add(customButton2);
buttonPanel.add(customButton3);
buttonPanel.add(clearButton);
buttonPanel.add(new JButton(""));
buttonPanel.add(new JButton(""));
buttonPanel.add(new JButton(""));
buttonPanel.add(equalButton);
// 添加组件到窗口
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(searchPanel, BorderLayout.NORTH);
contentPane.add(textField, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// 设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
// 数字按钮的事件处理器
private class DigitButtonActionListener implements ActionListener {
private int digit;
public DigitButtonActionListener(int digit) {
this.digit = digit;
}
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + String.valueOf(digit));
}
}
// 运算符按钮的事件处理器
private class OperationButtonActionListener implements ActionListener {
private char operator;
public OperationButtonActionListener(char operator) {
this.operator = operator;
}
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + " " + operator + " ");
}
}
// 清除按钮的事件处理器
private class ClearButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
}
// 等号按钮的事件处理器
private class EqualButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String[] tokens = textField.getText().split(" ");
double result = Double.parseDouble(tokens[0]);
for (int i = 1; i < tokens.length; i += 2) {
char operator = tokens[i].charAt(0);
double operand = Double.parseDouble(tokens[i+1]);
switch (operator) {
case '+':
result += operand;
break;
case '-':
result -= operand;
break;
case '*':
result *= operand;
break;
case '/':
result /= operand;
break;
}
}
textField.setText(String.valueOf(result));
}
}
// 自定义按钮的事件处理器
private class CustomButtonActionListener implements ActionListener {
private int buttonId;
public CustomButtonActionListener(int buttonId) {
this.buttonId = buttonId;
}
@Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + " Button" + buttonId);
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
在上面的代码中,我们使用了Java Swing库来创建计算器的界面。在按钮的事件处理器中,我们实现了基本的加减乘除运算和清除操作。同时,我们添加了三个自定义按钮,并在自定义按钮的事件处理器中实现了它们的功能。
阅读全文