用Java设计一个简单的计算器。
fionazhang77 1970-01-01
设计一个简单的计算器,能够对两个数据进行“加、减、乘、除”运算。要求:合理应用布局设计,注重界面美观、友好,要求处理NumberFormatException异常。
用JAVA实现一个计算器的应用程序
CSYMiracle 1970-01-01
用JAVA实现一个计算器的应用程序,要可视化界面的,要求实现基本的+,-,*,/运算急!!与windows自带的样式差不多··
最新问答
word转pdf,为什么不显示图片图片?
花花的老妈
2021-09-19
我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!
热门问答
word转pdf,为什么不显示图片图片?
花花的老妈
2021-09-19
我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!
public class Counter extends JFrame implements ActionListener{
/**
* Comment for
serialVersionUID
*/
private static final long serialVersionUID = -1379168350413982454L;
TextField num1;
TextField num2;
TextField result;
JButton clear;
JButton plus;
JTextArea j1;
JTextArea j2;
JButton aa;
public Counter(){
num1 = new TextField(5);//定义加数1
num2 = new TextField(5);//定义加数2
result = new TextField(5);//定义结果
plus=new JButton("加");//定义加法按钮
clear = new JButton("清空");//定义清空按钮
j1 = new JTextArea("+");//定义符号 +
j2 = new JTextArea("=");//定义符号 =
j1.setEditable(false);//定义属性只读
j2.setEditable(false);//定义加数1
result.setEditable(false);//定义加数1
aa = new JButton("隐藏");
this.add(num1);
this.add(j1);
this.add(num2);
this.add(j2);
this.add(result);
this.add(plus);
this.add(clear);
this.add(aa);
this.setLayout(new FlowLayout());
this.setBackground(Color.white);
this.setVisible(true);
this.setSize(500,200);
this.setTitle("计算器");
this.setLocationRelativeTo(null);//窗口初始位置在中间
plus.addActionListener(this);
clear.addActionListener(this);
aa.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==clear){//点击清空按钮
num1.setText("");
num2.setText("");
result.setText("");
}else if(e.getSource()==plus){//点击“加”按钮
result.setText(Integer.parseInt(num1.getText())+Integer.parseInt(num2.getText())+"");
}else if(e.getSource()==aa){
aa.setVisible(false);
}
}
public static void main(String[] args) {
new Counter();
}
/**
* 判断输入的是否是数字
* @param str
* @return
*/
public static boolean isInt(String str){
try {
Integer.parseInt(str);
} catch (Exception e) {
return false;
}
return true;
}
}