大家好,今天放了一天的假,无聊的我简单的敲了一下计算器的源代码,发现以前的东西都忘的差不多了,所以趁现在还有点记忆,先试着敲回以前的感觉。。。。
好了,废话不多说,先看原码:
先创建一个主类:
public class Test{
public static void main(String[ ] args){
Calculator calc=new Calculator();
calc.PaintingFrameUi();
}
}
在新建几个监听器:
第一个:
import javax.swing.*; (要引包,由于包名过多,可以 用 “*” 一笔带过。。。。)
import java.awt.event.*;
public class StringListener implements ActionListener{
//=============构造获取TextField===================
JTextField jtx = null;
public StringListener(JTextField textarea){
jtx = textarea;
}
//=============运算符监听器========================
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(command.equals("=")){
String content = jtx.getText();
int result1 = content.indexOf("+");
int result2 = content.indexOf("-");
int result3 = content.indexOf("*");
int result4 = content.indexOf("/");
jtx.setText("");
//=============开始判断============================
if (result1>0){
String value1 = content.substring(0, result1);
String value2 = content.substring(result1+1, content.length());
float f1 = Float.parseFloat(value1);
float f2 = Float.parseFloat(value2);
jtx.setText(""+(f1+f2));
}else if(result2>0){
String value1 = content.substring(0, result2);
String value2 = content.substring(result2+1, content.length());
float f1 = Float.parseFloat(value1);
float f2 = Float.parseFloat(value2);
jtx.setText(""+(f1-f2));
}else if(result3>0){
String value1 = content.substring(0, result3);
String value2 = content.substring(result3+1, content.length());
float f1 = Float.parseFloat(value1);
float f2 = Float.parseFloat(value2);
jtx.setText(""+(f1*f2));
}else if(result4>0){
String value1 = content.substring(0, result4);
String value2 = content.substring(result4+1, content.length());
float f1 = Float.parseFloat(value1);
float f2 = Float.parseFloat(value2);
jtx.setText(""+(f1/f2));
}
}else{
jtx.setText(jtx.getText()+command);
}
}
}
第二个监听事件:
import javax.swing.*;
import java.awt.event.*;
public class MyListener implements ActionListener{
//===============构造获取TextField================
JTextField jtx = null;
public MyListener(JTextField textarea){
jtx = textarea;
}
//===============监听数字控件======================
public boolean one=true;
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if (one){
jtx.setText(jtx.getText()+command);
}
}
}
最后一个包:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator{
public String number[] ={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","/","="};
public void PaintingFrameUi(){
//===========Draw Main frame==========================
JFrame frame=new JFrame();
frame.setSize(242,285);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setTitle("Calculator");
//===========Set Layout================================
FlowLayout layout=new FlowLayout(0,0,0);
frame.setLayout(layout);
//===========To instance TextField Control=============
JTextField textarea=new JTextField(20);
textarea.setHorizontalAlignment(JTextField.RIGHT);
frame.add(textarea);
JPanel jp = new JPanel();
Dimension dimen = new Dimension(225,225);
jp.setPreferredSize(dimen);
frame.add(jp);
GridLayout gl = new GridLayout(4,4);
jp.setLayout(gl);
//===========Add listenre==============================
ActionListener action_listener=new MyListener(textarea);
ActionListener string_listener=new StringListener(textarea);
//===========To instance Control=======================
for(int i=0;i<16;i++){
JButton button=new JButton(number[i]);
jp.add(button);
if (i==3 || i==7 || i==11 || i==15 || i==14 || i==13){
button.addActionListener(string_listener);
}else{
button.addActionListener(action_listener);
}
}
//===========ShowFrame=================================
frame.setVisible(true);
}
}
好了,看到这里 我相信你们都是看不是太懂,但是没关系,自己动手去操作一下就知道效果了
由于时间关系,今天的代码就先写到这里,现在继续每天的目标和计划吧:
1、每天锻炼指法一小时;
2、每天坚持写一篇博客或者日志;
3、每天上课提前十五分钟进教室;
4、给自己鼓励一句话-------不要太在意别人的眼光,最重要的是看自己的眼光,别人觉得好,证明你俩的爱好很像,他要是觉得你的眼光不好,也就证明了你俩爱好不一样而已;
原文地址:http://blog.csdn.net/my_wuya/article/details/46417931