import java.awt.; import java.awt.event.; import javax.swing.; public class Calculator implements ActionListener { // 声明组件 JFrame frame;//窗口 JTextField textField; JButton[] numberBs = new JButton[10];//0-9键 JButton[] functionBs = new JButton[9];//功能键 JButton addB, subB, mulB, divB; JButton decB, equB, delB, clrB; JPanel panel;//面板 // 声明变量 double num1 = 0, num2 = 0, zhi = 0; char operator; // 构造函数 Calculator() { // 初始化组件 frame = new JFrame("计算器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//当执行关闭窗口操作时,退出应用程序 frame.setSize(420, 550); frame.setLayout(null);//setLayOut是设置布局的意思,当我们传入null,那后面新建的组件的位置就完全由我们所定义,不受Frame约束。 textField = new JTextField();//输入框 textField.setBounds(50, 25, 200, 50); textField.setFont(new Font("Arial", Font.PLAIN, 25));//文字 textField.setEditable(false);//非编辑文本框 addB = new JButton("+"); subB = new JButton("-"); mulB = new JButton(""); divB = new JButton("/"); decB = new JButton("."); equB = new JButton("="); delB = new JButton("删除"); clrB = new JButton("清除"); functionBs[0] = addB; functionBs[1] = subB; functionBs[2] = mulB; functionBs[3] = divB; functionBs[4] = decB; functionBs[5] = equB; functionBs[6] = delB; functionBs[7] = clrB; for (int i = 0; i < 8; i++) {//8个功能键 functionBs[i].addActionListener(this);//将ActionListener添加到组件中。this关键字表示当前对象是实现ActionListener接口的类的实例 functionBs[i].setFont(new Font("Arial", Font.PLAIN, 18)); } for (int i = 0; i < 10; i++) {//0-9 numberBs[i] = new JButton(String.valueOf(i));//String.valueOf():String 类中的将基本数据型态转换成String的static方法,参数多载的方法 。 numberBs[i].addActionListener(this); numberBs[i].setFont(new Font("Arial", Font.PLAIN, 18)); } delB.setBounds(50, 415, 300, 50); clrB.setBounds(250,25, 100, 50); panel = new JPanel();//JPanel是轻量级容器,按钮、文本框等都放在这个容器中 panel.setBounds(50, 100, 300, 300); panel.setLayout(new GridLayout(4, 4, 10, 10));//布局管理器,GridLayout网格布局 panel.add(numberBs[1]); panel.add(numberBs[2]); panel.add(numberBs[3]); panel.add(addB); panel.add(numberBs[4]); panel.add(numberBs[5]); panel.add(numberBs[6]); panel.add(subB); panel.add(numberBs[7]); panel.add(numberBs[8]); panel.add(numberBs[9]); panel.add(mulB); panel.add(decB); panel.add(numberBs[0]); panel.add(equB); panel.add(divB); // 添加组件到窗口 frame.add(panel); frame.add(delB); frame.add(clrB); frame.add(textField); frame.setVisible(true);//模型需要显示则使用true,需要隐藏则使用false。 }请注释每一段,并写出整体在做什么
时间: 2024-03-04 10:50:18 浏览: 19
这段代码是一个简单的计算器程序,主要功能是实现数字和运算符的输入,以及对其进行计算并显示结果。
1. 导入必要的包和类。
2. 声明组件和变量。
3. 构造函数中初始化组件和设置布局。
4. 添加数字键、功能键和输入框,并设置监听器。
5. 设置面板和添加组件到窗口中。
6. 显示窗口。
具体注释见代码注释。
相关问题
设计一个简单java计算器包括java.awt类和java.swing类,用到数组
好的,下面是一个简单的Java计算器程序,它包括Java AWT和Swing类,并使用了数组。
```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 double currentVal = 0, previousVal = 0;
private String operator = "";
public Calculator() {
setTitle("Java Calculator");
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create the display field
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
// Create the buttons
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
}
// Add the components to the content pane
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5));
for (int i = 0; i < buttons.length; i++) {
buttonPanel.add(buttons[i]);
}
contentPane.add(buttonPanel, BorderLayout.CENTER);
setContentPane(contentPane);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
switch (label) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
display.setText(display.getText() + label);
break;
case ".":
if (display.getText().indexOf(".") == -1) {
display.setText(display.getText() + ".");
}
break;
case "+":
case "-":
case "*":
case "/":
operator = label;
previousVal = Double.parseDouble(display.getText());
display.setText("");
break;
case "=":
currentVal = Double.parseDouble(display.getText());
switch (operator) {
case "+":
currentVal += previousVal;
break;
case "-":
currentVal = previousVal - currentVal;
break;
case "*":
currentVal *= previousVal;
break;
case "/":
currentVal = previousVal / currentVal;
break;
}
display.setText(Double.toString(currentVal));
break;
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序使用数组来存储计算器按钮的标签,并使用循环来创建和添加这些按钮。它还使用了两个变量来跟踪先前的值和当前的值,并根据所选操作计算结果。最后,它使用Java AWT和Swing类来创建用户界面。
/** * An extension to JButton that binds the button action to a Driver object, * which must feature a method called buttonPressed() * * @author M. Allen */ import javax.swing.*; import java.awt.event.*; @SuppressWarnings( "serial" ) public class EventButton extends JButton implements ActionListener { private Calculator calculator; /** * pre: Driver d must be instantiated && must contain a method called * buttonPressed(). * post: This button can respond to action events and run the Driver method * buttonPressed(). * * @param d Driver with which this button communicates. */ public EventButton( Calculator c ) { super(); addActionListener( this ); calculator = c; } /** * post: If this button is pressed, the driver method buttonPressed() is * called to respond to the button presss event. * * @param e : ActionEvent from associated objects */ public void actionPerformed( ActionEvent e ) { calculator.buttonPressed( this ); } }
这段代码是一个继承自JButton的类EventButton,它绑定了按钮的操作到一个Calculator对象上,并要求该对象具有名为buttonPressed()的方法。当按钮被按下时,将调用Calculator对象的buttonPressed()方法来响应按钮按下事件。这段代码实现了基本的MVC模式中的控制器部分。