为javapackage com.company;import javax.swing.;import java.awt.;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;class Display { private final String[] keys = { "AC", "(", ")", "Delete", "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "", "0", ".", "^", "/", "sin", "cos", "=", "<--"}; private final JTextField result = new JTextField(); //文本框 显示计算结果 private final JButton[] button = new JButton[keys.length]; //按键数组,存放按钮组件 public Display() { / 设置文本框*/ result.setText("0"); //临时结果设置为零 result.setCaretColor(Color.white); /* 文本颜色 / result.setForeground(Color.white); / 背景颜色 */ result.setBackground(Color.black); } public void setResult(String str) { /*设置文本框中数字 */ result.setText(str); } public JTextField getResult() { /*从文本框中获取数字 */ return result; } public JButton[] getButton() { return button; } public void setButton(JButton button, int i) { this.button[i] = button; } public String getKey(int i) { return keys[i]; }}添加注释
时间: 2023-07-05 18:19:23 浏览: 189
//导入必要的Java类库
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//定义一个名为Display的类
class Display {
private final String[] keys = { "AC", "(", ")", "Delete", "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "", "0", ".", "^", "/", "sin", "cos", "=", "<--"}; //定义按键标签
private final JTextField result = new JTextField(); //定义文本框组件显示计算结果
private final JButton[] button = new JButton[keys.length]; //定义按键数组,存放按钮组件
//构造函数
public Display() {
//设置文本框
result.setText("0"); //临时结果设置为零
result.setCaretColor(Color.white); //设置文本框中光标的颜色
result.setForeground(Color.white); //设置文本颜色
result.setBackground(Color.black); //设置背景颜色
}
//设置文本框中数字
public void setResult(String str) {
result.setText(str);
}
//从文本框中获取数字
public JTextField getResult() {
return result;
}
//获取按键数组
public JButton[] getButton() {
return button;
}
//设置按键
public void setButton(JButton button, int i) {
this.button[i] = button;
}
//获取按键标签
public String getKey(int i) {
return keys[i];
}
}
阅读全文