borderlayout计算器
时间: 2023-09-26 10:11:19 浏览: 49
以下是一个基于BorderLayout布局的计算器示例代码:
```java
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.JTextField;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField text;
private JButton zero, one, two, three, four, five, six, seven, eight, nine;
private JButton add, subtract, multiply, divide, equals, clear;
private double num1, num2;
private String operator;
public Calculator() {
super("Calculator");
// Set up the text field
text = new JTextField();
text.setEditable(false);
// Set up the number buttons
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
// Set up the operator buttons
add = new JButton("+");
subtract = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equals = new JButton("=");
clear = new JButton("C");
// Add action listeners to all buttons
zero.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
equals.addActionListener(this);
clear.addActionListener(this);
// Set up the button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(seven);
buttonPanel.add(eight);
buttonPanel.add(nine);
buttonPanel.add(divide);
buttonPanel.add(four);
buttonPanel.add(five);
buttonPanel.add(six);
buttonPanel.add(multiply);
buttonPanel.add(one);
buttonPanel.add(two);
buttonPanel.add(three);
buttonPanel.add(subtract);
buttonPanel.add(zero);
buttonPanel.add(new JPanel());
buttonPanel.add(equals);
buttonPanel.add(add);
// Add components to the frame using BorderLayout
add(text, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(clear, BorderLayout.SOUTH);
// Set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Get the button that was clicked
JButton button = (JButton) e.getSource();
// Handle number buttons
if (button == zero) {
text.setText(text.getText() + "0");
} else if (button == one) {
text.setText(text.getText() + "1");
} else if (button == two) {
text.setText(text.getText() + "2");
} else if (button == three) {
text.setText(text.getText() + "3");
} else if (button == four) {
text.setText(text.getText() + "4");
} else if (button == five) {
text.setText(text.getText() + "5");
} else if (button == six) {
text.setText(text.getText() + "6");
} else if (button == seven) {
text.setText(text.getText() + "7");
} else if (button == eight) {
text.setText(text.getText() + "8");
} else if (button == nine) {
text.setText(text.getText() + "9");
}
// Handle operator buttons
else if (button == add) {
num1 = Double.parseDouble(text.getText());
operator = "+";
text.setText("");
} else if (button == subtract) {
num1 = Double.parseDouble(text.getText());
operator = "-";
text.setText("");
} else if (button == multiply) {
num1 = Double.parseDouble(text.getText());
operator = "*";
text.setText("");
} else if (button == divide) {
num1 = Double.parseDouble(text.getText());
operator = "/";
text.setText("");
} else if (button == equals) {
num2 = Double.parseDouble(text.getText());
if (operator.equals("+")) {
text.setText("" + (num1 + num2));
} else if (operator.equals("-")) {
text.setText("" + (num1 - num2));
} else if (operator.equals("*")) {
text.setText("" + (num1 * num2));
} else if (operator.equals("/")) {
if (num2 == 0) {
text.setText("Error");
} else {
text.setText("" + (num1 / num2));
}
}
} else if (button == clear) {
text.setText("");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器使用BorderLayout布局,将文本框放在北部,按钮面板放在中间,清除按钮放在南部。按钮面板使用GridLayout布局,以4行4列显示数字和操作符按钮。每个按钮都添加了一个ActionListener,以便在单击时执行相应的操作。这个计算器可以处理加法、减法、乘法和除法运算,以及清除当前输入和显示结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)