📄 calculator.java
字号:
//按下'CE'按钮,将当前文本框内数据清除
if (temp == buttonCe)
{
textResult = "0";
clickable = true;
}
//按下'C'按钮,文本框内数据清除,同时var,answer清0
if (temp == buttonC)
{
vard = answerd = 0;
textResult = "0";
clickable = true;
}
//按下'退出'菜单栏
if (temp == exitItem)
{
System.exit(0);
}
//按下'复制'菜单栏
if (temp == copyItem)
{
copy = textResult;
}
//按下'粘贴'菜单栏
if (temp == pasteItem)
{
textResult = copy;
}
if (temp == sItem)
{
JOptionPane.showMessageDialog(panel, "当前是标准型计算器,\n科学型计算器有待更新。");
}
//按下'帮助主题'菜单栏
if (temp == topHelp)
{
JOptionPane.showMessageDialog(panel, scrollHelp);
}
//按下'关于'菜单栏
if (temp == aboutCal)
{
JOptionPane.showMessageDialog(panel, "计算器\n开发者:钱晨笛");
}
}
//输入中如果有操作非法,比如按下两次'+',捕获异常
catch (Exception e)
{
textResult = "E";
clickable = false;
}
display.str = textResult;
display.repaint();
}
//主函数
public static void main(String args [])
{
new Calculator();
}
}
class DPanel extends JPanel
{
private int x;
public static String str = "0";
public void paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < str.length(); i++)
{
x = 79 * i;
int rH = 20 * getHeight() / 145;
int rW = 20 * getHeight() / 145;
int Poly1_x [] =
{
(x + 11) * getHeight() / 145,
(x + 60) * getHeight() / 145,
(x + 55) * getHeight() / 145,
(x + 11) * getHeight() / 145
};
int Poly1_y [] =
{
0 * getHeight() / 145,
0 * getHeight() / 145,
10 * getHeight() / 145,
10 * getHeight() / 145
};
int Poly2_x [] =
{
(x + 0) * getHeight() / 145,
(x + 10) * getHeight() / 145,
(x + 10) * getHeight() / 145,
(x + 0) * getHeight() / 145
};
int Poly2_y [] =
{
7 * getHeight() / 145,
13 * getHeight() / 145,
60 * getHeight() / 145,
60 * getHeight() / 145
};
int Poly3_x [] =
{
(x + 59) * getHeight() / 145,
(x + 69) * getHeight() / 145,
(x + 69) * getHeight() / 145,
(x + 59) * getHeight() / 145
};
int Poly3_y [] =
{
10 * getHeight() / 145,
10 * getHeight() / 145,
60 * getHeight() / 145,
60 * getHeight() / 145
};
int Poly4_x [] =
{
(x + 0) * getHeight() / 145,
(x + 10) * getHeight() / 145,
(x + 10) * getHeight() / 145,
(x + 0) * getHeight() / 145
};
int Poly4_y [] =
{
75 * getHeight() / 145,
75 * getHeight() / 145,
132 * getHeight() / 145,
135 * getHeight() / 145
};
int Poly5_x [] =
{
(x + 59) * getHeight() / 145,
(x + 69) * getHeight() / 145,
(x + 69) * getHeight() / 145,
(x + 59) * getHeight() / 145
};
int Poly5_y [] =
{
75 * getHeight() / 145,
75 * getHeight() / 145,
135 * getHeight() / 145,
135 * getHeight() / 145
};
int Poly6_x [] =
{
(x + 11) * getHeight() / 145,
(x + 57) * getHeight() / 145,
(x + 62) * getHeight() / 145,
(x + 57) * getHeight() / 145,
(x + 11) * getHeight() / 145,
(x + 6) * getHeight() / 145
};
int Poly6_y [] =
{
62 * getHeight() / 145,
62 * getHeight() / 145,
67 * getHeight() / 145,
72 * getHeight() / 145,
72 * getHeight() / 145,
67 * getHeight() / 145
};
int Poly7_x [] =
{
(x + 11) * getHeight() / 145,
(x + 55) * getHeight() / 145,
(x + 62) * getHeight() / 145,
(x + 11) * getHeight() / 145
};
int Poly7_y [] =
{
135 * getHeight() / 145,
135 * getHeight() / 145,
145 * getHeight() / 145,
145 * getHeight() / 145
};
switch (str.charAt(i))
{
case '0':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly4_x, Poly4_y, 4);
g.fillArc(Poly4_x[0], Poly4_y[0] - rH / 2, rW, rH, 180, -45);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '1':
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
break;
case '2':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly4_x, Poly4_y, 4);
g.fillArc(Poly4_x[0], Poly4_y[0] - rH / 2, rW, rH, 180, -45);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '3':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '4':
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
break;
case '5':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '6':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly4_x, Poly4_y, 4);
g.fillArc(Poly4_x[0], Poly4_y[0] - rH / 2, rW, rH, 180, -45);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '7':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
break;
case '8':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly4_x, Poly4_y, 4);
g.fillArc(Poly4_x[0], Poly4_y[0] - rH / 2, rW, rH, 180, -45);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '9':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly3_x, Poly3_y, 4);
g.fillArc(Poly3_x[0] - rW / 2, Poly3_y[0] - rH / 2, rW, rH, 0, 63);
g.fillArc(Poly3_x[3] - rW / 2, Poly3_y[3] - rH / 2, rW, rH, 0, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly5_x, Poly5_y, 4);
g.fillArc(Poly5_x[0] - rW / 2, Poly5_y[0] - rH / 2, rW, rH, 0, 45);
g.fillArc(Poly5_x[3] - rW / 2, Poly5_y[3] - rH / 2, rW, rH, 0, -57);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
break;
case '.':
g.fillOval(Poly2_x[0], getHeight() - rH, rW, rH);
break;
case '-':
g.fillPolygon(Poly6_x, Poly6_y, 6);
break;
case 'E':
g.fillPolygon(Poly1_x, Poly1_y, 4);
g.fillArc(Poly1_x[0] - rW / 2, 0, rW, rH, 90, 60);
g.fillPolygon(Poly2_x, Poly2_y, 4);
g.fillArc(Poly2_x[3], Poly2_y[3] - rH / 2, rW, rH, 180, 45);
g.fillPolygon(Poly4_x, Poly4_y, 4);
g.fillArc(Poly4_x[0], Poly4_y[0] - rH / 2, rW, rH, 180, -45);
g.fillPolygon(Poly6_x, Poly6_y, 6);
g.fillPolygon(Poly7_x, Poly7_y, 4);
g.fillArc(Poly7_x[0] - rW / 2, Poly7_y[0] - rH / 2, rW, rH, -90, -70);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -