十、设计一个计算器界面窗口

一、代码

package jisuanqi;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

class counter1 extends JFrame
{
public counter1()
{
super("计算器");
this.setSize(400,100);
this.setLocation(300,240);
this.setLayout(new FlowLayout());
TextField text1=new TextField(4);
text1.setText("1");
this.add(text1);

String proList[] = { "+","-","x" ,"%"};
TextField text;
JComboBox comboBox;
Container conPane = getContentPane();
comboBox = new JComboBox(proList);
comboBox.setEditable(true);
conPane.add(comboBox);

TextField text2=new TextField(4);
text2.setText("1");
this.add(text2);
JButton button = new JButton("=");
this.add(button);
TextField text3=new TextField(4);
text3.setText("2");

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String s=comboBox.getEditor().getItem().toString();
double a= Integer.parseInt(text1.getText());
double b= Integer.parseInt(text2.getText());
if(s.equals("+")) {
double t=a+b;
String m=String.valueOf(t);

text3.setText(m);
}
else if(s.equals("-"))
{double t=a-b;
String m=String.valueOf(t);

text3.setText(m);}
else if(s.equals("x"))
{double t=a*b;
String m=String.valueOf(t);

text3.setText(m);}
else
{double t=a/b;
String m=String.valueOf(t);

text3.setText(m);}

}});
conPane.add(text3);
this.setVisible(true);
}

 

}

public class Counter {
public static void main(String[] args)
{
new counter1();
}
}

二、运行结果

三、心得体会

此次代码有些欠缺,未能实现更多的功能,前期有很多的问题,在同学的帮助下完成代码。

转载于:https://www.cnblogs.com/zy2023/p/10968363.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值