Java编写一个应用程序,有一个FlowLayout布局的窗口,实现加、减、乘、除、乘方、开方。
时间: 2023-02-26 09:02:35 浏览: 152
java应用编程实例
3星 · 编辑精心推荐
下面是 Java 代码实现的示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame {
private JTextField resultField;
private JButton addButton, subtractButton, multiplyButton, divideButton, powerButton, sqrtButton;
public Calculator() {
setTitle("计算器");
setSize(250, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
resultField = new JTextField(20);
resultField.setEditable(false);
add(resultField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
powerButton = new JButton("^");
sqrtButton = new JButton("√");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 加法实现
}
});
subtractButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 减法实现
}
});
multiplyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 乘法实现
}
});
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 除法实现
}
});
powerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 乘方实现
}
});
sqrtButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 开方实现
}
});
buttonPanel.add(addButton);
buttonPanel.add(subtractButton);
buttonPanel.add(multiplyButton);
buttonPanel.add(divideButton);
buttonPanel.add(powerButton);
buttonPanel.add(sqrtButton);
add(buttonPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
这是一个利用 FlowLayout 布局的简单计算器,可以实现加、减、乘、除、乘方和开方等操作。代码中给出了每个操作的
阅读全文