目录
使用Java Swing的GUI图形用户界面编程设计并编写一个简易计算器程序,用户通过鼠标或者键盘操作输入参加运算的数值和运算符,要求能进行简单的四则运算。
效果展示:
当然,界面颜色,字体,文字均可自行调整,主要使用到awt和swing组件!
明确目标:
(1)学会分析“简易计算器”程序实现的逻辑思路。
(2)能够独立完成“简易计算器”程序的源代码编写、编译及运行。
(3)掌握Java Swing界面编程的应用。
(4)了解计算器逻辑运算实现。
案例实现:
(1)创建普通Java项目(引入版本为15.0.2的jdk)
(2)UI组件的创建和初始化
(3)在窗体中添加UI组件
(4)添加事件响应逻辑
(5)计算逻辑的实现
(6)注册监听器
程序运行环境为Windows10 ,编译环境为Eclipse 。
主方法
public static void main(String[] args) {
new MyFrame("计算器");
}
按钮的实现
private void SetButtonArea() {
addButton("7",33,28,20,178);
addButton("8",33,28,58,178);
addButton("9",33,28,96,178);
addButton("/",33,28,134,178);
addButton("%",33,28,172,178);
addButton("4",33,28,20,210);
addButton("5",33,28,58,210);
addButton("6",33,28,96,210);
addButton("*",33,28,134,210);
addButton("1/x",33,28,172,210);
addButton("1",33,28,20,242);
addButton("2",33,28,58,242);
addButton("3",33,28,96,242);
addButton("-",33,28,134,242);
addButton("0",71,28,20,274);
addButton(".",33,28,96,274);
addButton("+",33,28,134,274);
addButton("=",33,60,172,242);
}
逻辑实现
private void counts() {
if(ta.getText().equals("")&&(b.getActionCommand().equals("+")||
b.getActionCommand().equals("-")||
b.getActionCommand().equals("*")||
b.getActionCommand().equals("/")||
b.getActionCommand().equals("%")||
b.getActionCommand().equals("1/x")||
b.getActionCommand().equals("="))) {
}else if(ta.getText().equals(".")&&(b.getActionCommand().equals("+")||
b.getActionCommand().equals("-")||
b.getActionCommand().equals("*")||
b.getActionCommand().equals("/")||
b.getActionCommand().equals("%")||
b.getActionCommand().equals("1/x")||
b.getActionCommand().equals("="))){
}else {
if( b.getActionCommand().equals("+")||
b.getActionCommand().equals("-")||
b.getActionCommand().equals("*")||
b.getActionCommand().equals("%")||
b.getActionCommand().equals("1/x")||
b.getActionCommand().equals("/")){
if(flag2 = true) {
flag2 = false;
}
if(flag) {
n = new Double(ta.getText()).doubleValue();
flag = false;
}else {
if(k=="="){
}else {
m = new Double(ta.getText()).doubleValue();
if(k == "-") {
if(n==0)
n = m;
else
n=n-m;
}else if(k == "+") {
if(n==0)
n = m;
else
n=n+m;
}else if(k == "*") {
if(n==0)
n = m;
else
n=n*m;
}else if(k=="%"){
if(n==0)
n=m;
else
m=m*100;
}else if(k=="1/x"){
if(n==0)
n=m;
else
n=1/m;
}else if(k == "/") {
if(n==0)
n = m;
else
n=n/m;
}
}
}
k = b.getActionCommand();
ta.setText("");
}else if(b.getActionCommand().equals("=")) {
m = new Double(ta.getText()).doubleValue();
if(k == "+") {
ta.setText("");
ta.append(n+"+"+m);
ta.append(System.getProperty("line.separator"));
n = n+m;
ta.append("="+n);
}else if(k == "-") {
ta.setText("");
ta.append(n+"-"+m);
ta.append(System.getProperty("line.separator"));
n = n-m;
ta.append("="+n);
}else if(k == "*") {
ta.setText("");
ta.append(n+"*"+m);
ta.append(System.getProperty("line.separator"));
n = n*m;
ta.append("="+n);
}else if(k == "%") {
ta.setText("");
ta.append("%"+m);
ta.append(System.getProperty("line.separator"));
m = m*100;
ta.append("="+m+"%");
}else if(k == "1/x") {
ta.setText("");
ta.append(1+"/"+m);
ta.append(System.getProperty("line.separator"));
n = 1/m;
ta.append("="+n);
}else if(k == "/") {
ta.setText("");
ta.append(n+"/"+m);
ta.append(System.getProperty("line.separator"));
n= n/m;
ta.append("="+n);
}
k="=";
flag2 = true;
}else {
if(flag2) {
flag = true;
flag2 = false;
ta.setText("");
m = n =0;
}
ta.append(b.getActionCommand());
}
}
}
样式的实现
private void SetTextAreas() {
ta = new TextArea("0",8,52,3);
ta.setBackground(Color.red);
ta.setSize(190, 50);
ta.setFont(new Font("宋体", Font.PLAIN, 15));
ta.setLocation(20,60);
this.add(ta);
tb = new TextArea(" DESIGN BY 托马斯",8,52,3);
tb.setBackground(Color.yellow);
tb.setSize(190,25);
tb.setFont(new Font("宋体", Font.PLAIN, 16));
tb.setForeground(Color.blue);
tb.setLocation(20,130);
this.add(ta);
this.add(tb);
}