使用JFrame作为主窗口,将计算器的各个组件添加到JFrame中。 使用JPanel来组织计算器的布局,将数字键、运算符键、清除键等分别放在不同的JPanel中。 使用JButton作为数字键、运算符键、清除键等的组件。 使用JTextField作为显示计算结果的组件。 使用ActionListener来监听按钮的点击事件,根据不同的按钮执行不同的操作。 在ActionListener中,使用if语句或switch语句来判断用户点击的是哪个按钮,然后执行相应的操作,例如进行加减乘除运算、清空文本框等。 在进行运算时,可以使用Java的数学库来进行计算。 最后,记得将计算器的各个组件添加到JFrame中,并设置JFrame的大小、标题等属性。
时间: 2023-05-18 07:05:32 浏览: 83
以下是一个简单的Java计算器的示例代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/"
};
private JPanel buttonPanel;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display = new JTextField(20);
display.setEditable(false);
buttons = new JButton[buttonLabels.length];
buttonPanel = new JPanel(new GridLayout(4, 4));
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
getContentPane().add(display, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
display.setText("");
} else if (command.equals("=")) {
String expression = display.getText();
double result = evaluate(expression);
display.setText(Double.toString(result));
} else {
display.setText(display.getText() + command);
}
}
private double evaluate(String expression) {
// TODO: Implement the evaluation logic using Java's math library
return 0.0;
}
public static void main(String[] args) {
new Calculator();
}
}
在这个示例代码中,我们使用了JFrame作为主窗口,并将计算器的各个组件添加到JFrame中。我们使用了JPanel来组织计算器的布局,并将数字键、运算符键、清除键等分别放在不同的JPanel中。我们使用了JButton作为数字键、运算符键、清除键等的组件,并使用ActionListener来监听按钮的点击事件。在ActionListener中,我们使用if语句或switch语句来判断用户点击的是哪个按钮,然后执行相应的操作,例如进行加减乘除运算、清空文本框等。在进行运算时,我们可以使用Java的数学库来进行计算。最后,我们将计算器的各个组件添加到JFrame中,并设置JFrame的大小、标题等属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)