calc.java
import java.awt.*;
public class calc extends Frame {
Button[] b = new Button[25];
Pan tf = new Pan(1);
Font f = new Font("TimesRoman", Font.BOLD, 14);
double n = 0;
int op;
boolean flag = false;
Toolkit tool;
public static void main(String[] args) {
new calc();
}
public calc() {
setTitle("Calculator");
tool = getToolkit();
setBackground(new Color(38, 104, 165));
setForeground(new Color(255, 255, 255));
setResizable(false);
setIconImage(tool.getImage(GetResources("ico.gif")));
resize(350, 400);
setLayout(null);
add(tf);
tf.setBounds(50, 50, 240, 25);
tf.setFont(f);
tf.setForeground(new Color(0, 0, 0));
tf.setBackground(new Color(150, 150, 255));
tf.setText("0");
for (int i = 0; i < 25; i++) {
String s = "" + i;
if (i >= 10) {
switch (i) {
case 10:
s = "+";
break;
case 11:
s ="-";break;
case 12:
s = "*";
break;
case 13:
s = "/";
break;
case 14:
s = "=";
break;
case 15:
s = "C";
break;
case 16:
s = "sqrt";
break;
case 17:
s = "%";
break;
case 18:
s = "sin";
break;
case 19:
s = "cos";
break;
case 20:
s = "tan";
break;
case 21:
s = "Exp";
break;
case 22:
s = "Log";
break;
case 23:
s = "";
break;
case 24:
s = "";
break;
}
}
b[i] = new Button(s);
b[i].setFont(new Font("TimesRoman", 1, 20));
b[i].setForeground(new Color(0, 0, 0));
b[i].setBackground(new Color(255, 255, 222));
add(b[i]);
if (i < 5) {
b[i].setBounds(50 + 50 * i, 100, 40, 40);
} else if (i < 10) {
b[i].setBounds(50 + 50 * (i - 5), 150, 40, 40);
} else if (i < 15) {
b[i].setBounds(50 + 50 * (i - 10), 200, 40, 40);
} else if (i < 20) {
b[i].setBounds(50 + 50 * (i - 15), 250, 40, 40);
} else {
b[i].setBounds(50 + 50 * (i - 20), 300, 40, 40);
}
}
Dimension res = tool.getScreenSize();
move((int) ((res.width - 400) / 2 + 100), (int) ((res.height - 400) / 2 + 100));
setVisible(true);
}
public java.net.URL GetResources(String s) {
return this.getClass().getResource(s);
}
public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY) {
dispose();
}
if (e.id == Event.ACTION_EVENT) {
for (int i = 0; i < 10; i++) {
if ((e.target).equals(b[i])) {
String s = tf.getText();
if (s.equals("0")) {
s = "" + i;
} else if (flag) {
s = "" + i;
flag = false;
} else {
s += i;
}
tf.setText(s);
return true;
}
}
if ((e.target).equals(b[10])) {
n = Double.parseDouble(tf.getText());
op = 10;
flag = true;
return true;
}
if ((e.target).equals(b[11])) {
n = Double.parseDouble(tf.getText());
op = 11;
flag = true;
return true;
}
if ((e.target).equals(b[12])) {
n = Double.parseDouble(tf.getText());
op = 12;
flag = true;
return true;
}
if ((e.target).equals(b[13])) {
n = Double.parseDouble(tf.getText());
op = 13;
flag = true;
return true;
}
if ((e.target).equals(b[14])) {
switch (op) {
case 10:
n += Double.parseDouble(tf.getText());
break;
case 11:
n -= Double.parseDouble(tf.getText());
break;
case 12:
n *= Double.parseDouble(tf.getText());
break;
case 13:
n /= Double.parseDouble(
tf.getText());
break;
case 17:
n %= Double.parseDouble(tf.getText());
break;
}
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length() - 2);
}
tf.setText(ss);
return true;
}
if ((e.target).equals(b[15])) {
tf.setText("0");
flag = false;
return true;
}
if ((e.target).equals(b[16])) {
double d = Double.parseDouble(tf.getText());
if (d >= 0) {
n = Math.sqrt(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.
length()
- 2);
}
tf.setText(ss);
}
return true;
}
if ((e.target).equals(b[17])) {
n = Double.parseDouble(tf.getText());
op = 17;
flag = true;
return true;
}
if ((e.target).equals(b[18])) {
double d = Double.parseDouble(tf.getText());
n = Math.sin(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length()
- 2);
}
tf.setText(ss);
return true;
}
if ((e.target).equals(b[19])) {
double d = Double.parseDouble(tf.getText());
n = Math.cos(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length()
- 2);
}
tf.setText(ss);
return true;
}
if ((e.target).equals(b[20])) {
double d = Double.parseDouble(tf.getText());
n = Math.tan(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length() - 2);
}
tf.setText(ss);
return true;
}
if ((e.target).equals(b[21])) {
double d = Double.parseDouble(tf.getText());
n = Math.exp(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length() - 2);
}
tf.setText(ss);
return true;
}
if ((e.target).equals(b[22])) {
double d = Double.parseDouble(tf.getText());
if (d > 0) {
n = Math.log(d);
String ss = "" + n;
if (ss.endsWith(".0")) {
ss = ss.substring(0, ss.length() - 2);
}
tf.setText(ss);
}
return true;
}
}
return false;
}
}Pan.java
class Pan extends Panels {
public Font f = new Font("Helvetica", 1, 18);
FontMetrics fm = getFontMetrics(f);
public String s = "";
private int tip;
public Pan() {
super();
}
public Pan(int tip) {
this();
this.tip = tip;
}
public void setText(String s) {
this.s = s;
repaint();
}
public String getText() {
return s;
}
public void paint(Graphics g) {
super.paint(g);
g.setFont(f);
if (tip == 1) {
g.setColor(Color.white);
g.drawString(s, size().width - fm.stringWidth(s) - 6, 20);
} else {
g.setColor(Color.black);
g.drawString(s, 10, 20);
}
}
}Panels.java
class Panels extends Panel {
public Image im, im1;
public Panels(Image im) {
this.im = im;
}
public Panels() {
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
super.paint(g);
Dimension dimension = size();
im1 = createImage(dimension.width, dimension.height);
pan(im1.getGraphics());
g.drawImage(im1, 0, 0, this);
}
public void pan(Graphics g) {
Dimension dimension = size();
int w = dimension.width;
int h = dimension.height;
Color color = getBackground();
g.setColor(color);
g.fillRect(0, 0, w, h);
if (im != null) {
for (int k = 0; k < w; k += im.getWidth(this)) {
for (int l = 0; l < h; l += im.getHeight(this)) {
g.drawImage(im, k, l, this);
}
}
}
g.setColor(color.brighter());
g.drawRect(1, 1, w - 2, h - 2);
g.setColor(color.darker());
g.drawRect(0, 0, w - 2, h - 2);
}
}发布于 2018-01-22 11:59:52
calc类以小写字母开头。与所有类名大写的一致性对于可读性很重要,并且能够区分类和变量。Button[] b = new Button[25];应该是Button[] buttons = new Button[25];。Pan扩展了Panels,扩展了Panel。)要么试着想出更好的名字,要么重新考虑它们是否真的应该互相延伸。问问自己:Is a Pan也是Panels的一种吗?Panels是Panel的一种吗?for (int i = 0; i < 25; i++) { ... } )迭代数组。简单地删除一个按钮并将数组缩减到24长度将导致您的程序与IndexOutOfBoundsException崩溃。相反,动态访问数组的长度:myArray.length会给出它的长度。所以循环应该如下所示:for (int i = 0; i < b.length; i++) { ... }。new calc();启动GUI,这是意外的。相反,可以这样做:new calc().show();或类似的东西,这样您就可以始终知道它不仅会创建一个对象,而且还会显示GUI。发布于 2018-01-22 13:02:58
因为它看起来像是‘第五课’,我将在此基础上复习。
95%的时间使用缩写会降低代码的可读性。例如"tf“、"b”、"n“、"op”。如果我正在阅读代码,并不得不滚动以了解什么是'tf‘的实际含义,它禁止’阅读流程‘。
另外:类名应该是名词--正如在另一篇文章中已经提到的:它们应该是单数。方法应该包含一个动词。
总是给“事物”一个恰当的名字,越清晰越好。您有一个方法calc()。首先,我想,事情将在这里计算。但它实际上初始化了gui。所以,initializeGUI会帮上忙的。
为什么这很重要:根据你必须完成的任务,你花更多的时间阅读代码而不是编写代码。
重要的是,一种方法能做一件事。它做得越多,理解起来就越复杂。例如,handleEvent()方法会关闭窗口并执行计算。
http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
雷蒙德已经提到了像PascalCase这样的东西。很多事情都是自然发生的,但我认为这是一种很好的阅读方式,你可以避免养成非常规的习惯。
通常,主要的方法放在类的末尾。
99%的情况下,成员变量应该声明为私有变量。我建议养成一种习惯。如果您决定让变量更公开,您需要有一个很好的理由。
您已经重写了超类中的一些方法,始终用@Override注释对它们进行注释。首先,这向读者显示,它是从超类或接口中重写的,但如果您以某种方式破坏了重构,编译器将告诉您“您得到了一个@重写注释,但实际上,它没有被重写”。
希望这能帮上忙
慢慢来
https://codereview.stackexchange.com/questions/185679
复制相似问题