具体代码如下:
import javax.swing.*
import java.awt.event.*
import java.awt.*
public class Calculator extends JFrame implements ActionListener {
private JFrame jf
private JButton[] allButtons
private JButton clearButton
private JTextField jtf
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版")
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0)
}
})
allButtons=new JButton[16]
clearButton=new JButton("清除")
jtf=new JTextField(25)
jtf.setEditable(false)
String str="123+456-789*0.=/"
for(int i=0i<allButtons.lengthi++){
allButtons[i]=new JButton(str.substring(i,i+1))
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout())
JPanel northPanel=new JPanel()
JPanel centerPanel=new JPanel()
JPanel southPanel=new JPanel()
northPanel.setLayout(new FlowLayout())
centerPanel.setLayout(new GridLayout(4,4))
southPanel.setLayout(new FlowLayout())
northPanel.add(jtf)
for(int i=0i<16i++){
centerPanel.add(allButtons[i])
}
southPanel.add(clearButton)
jf.add(northPanel,BorderLayout.NORTH)
jf.add(centerPanel,BorderLayout.CENTER)
jf.add(southPanel,BorderLayout.SOUTH)
addEventHandler()
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this)
for(int i=0i<allButtons.lengthi++){
allButtons[i].addActionListener(this)
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("")
}
})
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand()
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24)
jtf.setFont(f)
jtf.setBackground(new Color(0x8f,0xa0,0xfb))
for(int i=0i<16i++){
allButtons[i].setFont(f)
allButtons[i].setForeground(Color.RED)
}
}
public void showMe(){
init()
setFontAndColor()
jf.pack()
jf.setVisible(true)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args){
new Calculator().showMe()
}
}
import java.awt.*import java.awt.event.*
class CalcAppDemo extends Frame{
private TextField t_result
private Panel p_main//主面板
private Panel p_num//数字面板
private Panel p_oper// *** 作符面板
private Panel p_show//显示面板
private Button b_num[]//数字按钮
private Button b_oper[]// *** 作符按钮
public CalcAppDemo(String title){
setTitle(title)
t_result = new TextField("0.0",21)
p_main = new Panel()
p_num = new Panel()
p_oper = new Panel()
p_show = new Panel()
p_main.setLayout(new BorderLayout())
p_num.setLayout(new GridLayout(4,3,1,1))
p_oper.setLayout(new GridLayout(4,2,1,1))
b_num = new Button[12]
for(int i = 0i <9i++){
b_num[i] = new Button(new Integer(i+1).toString())
}
b_num[9] = new Button("0")
b_num[10] = new Button("cls")
b_num[11] = new Button(".")
for(int i = 0i <12i++){
p_num.add(b_num[i])
}
b_oper = new Button[8]
b_oper[0] = new Button("+")
b_oper[1] = new Button("-")
b_oper[2] = new Button("*")
b_oper[3] = new Button("/")
b_oper[4] = new Button("pow")
b_oper[5] = new Button("sqrt")
b_oper[6] = new Button("+/-")
b_oper[7] = new Button("=")
for(int i = 0i <8i++){
p_oper.add(b_oper[i])
}
t_result.setEditable(false)
p_show.add(t_result,BorderLayout.NORTH) //文本框在显示面板的北边
p_main.add(p_show,BorderLayout.NORTH) //显示面板在主面板上的北边
p_main.add(p_num,BorderLayout.WEST)
p_main.add(p_oper,BorderLayout.EAST)// *** 作符面板在主面板上的东边
this.add(p_main,BorderLayout.CENTER)//主面板在框架(主窗口)的中间
setSize(400,400)
setResizable(false)
pack()
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0)
}
})
ButtonListener bl = new ButtonListener() //监听器对象
for(int i=0i<12i++){
b_num[i].addActionListener(bl) //注册监听器
}
for(int i = 0i <8i++){
b_oper[i].addActionListener(bl)
}
}
class ButtonListener implements ActionListener { //监听器类是内部类
private String lastOp//存储上一次 *** 作符
private String strVal//存储数字对应的字符串
private double total//总数
private double number//存储新输入的数
private boolean firsttime//判断是否第一次按下的是 *** 作符按钮
private boolean operatorPressed//判断是否已经按过 *** 作符按钮
ButtonListener( ){
firsttime = true
strVal = ""
}
//事件处理器
public void actionPerformed( ActionEvent e ){
String s = ((Button)e.getSource()).getLabel().trim()
if(Character.isDigit(s.charAt(0))){//判断是 *** 作数还是 *** 作符
handleNumber(s)
}else{
calculate(s) //计算
}
}
//判断是一元 *** 作符还是二元 *** 作符,并根据 *** 作符类型做计算
void calculate( String op ){
operatorPressed = true
if(firsttime&&!isUnary(op)){
total = getNumberOnDisplay()
firsttime = false
}
if (isUnary(op)){
handleUnaryOp(op)
}
else if (lastOp != null){
handleBinaryOp(lastOp)
}
if (!isUnary(op)){ //存储上一次按下的 *** 作符
lastOp = op
}
}
//判断是否是一元 *** 作符
boolean isUnary(String s){
returns.equals( "=" )
|| s.equals( "cls" )
|| s.equals( "sqrt" )
|| s.equals( "+/-" )
|| s.equals( "." )
}
//处理一元 *** 作符
void handleUnaryOp( String op ){
if ( op.equals( "+/-" ) ){
//将显示框中的数字取反
number = negate(getNumberOnDisplay()+"")
t_result.setText("")
t_result.setText( number + "")
return
}else if (op.equals(".")){
handleDecPoint()
return
}else if(op.equals("sqrt")){
number = Math.sqrt(getNumberOnDisplay())
t_result.setText("")
t_result.setText(number+"")
return
}else if(op.equals("=")){
//在按下"="前已经按下一个二元运算符
if(lastOp!=null&&!isUnary(lastOp)){
handleBinaryOp(lastOp)
}
lastOp=null
firsttime=true
return
}else{
clear()
}
}
//处理二元运算符
void handleBinaryOp(String op){
if(op.equals("+")){
total += number
}else if(op.equals("-")){
total -= number
}else if(op.equals("*")){
total *= number
}else if(op.equals("/")){
try{ //异常处理
total /=number
}catch(ArithmeticException ae){}
}else if(op.equals("pow"))
total=Math.pow(total,number)
// t_result.setText("")
lastOp = null
// strVal = ""
number = 0
t_result.setText(total+"")
}
//该方法用于处理数字按钮
void handleNumber(String s){
if (!operatorPressed){ //连接按下的数字按钮的值
strVal+=s
}
else{//当按下 *** 作符按钮时,清除strVal并存储输入的第一个数值
operatorPressed = false
strVal = s
}
//将strVal转换为double
number=new Double(strVal).doubleValue()
t_result.setText("")
t_result.setText(strVal)
}
//该方法用于按下"."按钮
void handleDecPoint(){
operatorPressed = false
//如果该字符串中无".",放置一个"."在字符串末尾
if ( strVal.indexOf( "." ) <0 ) {
strVal+="."
}
t_result.setText("")
t_result.setText( strVal )
}
//该方法用于将一个数求反
double negate( String s ){
operatorPressed = false
//如果是一个整数,去掉小数点后面的0
if ( number == ( int ) number ){
s = s.substring(0, s.indexOf( "." ) )
}
//如果无"-"增加在该数的前面
if ( s.indexOf( "-" ) <0 ){
strVal = "-"+s
}
else{//如果有"-"则去掉
strVal = s.substring( 1 )
}
return new Double( strVal ).doubleValue()
}
//将显示框中的值转换为Double
double getNumberOnDisplay() {
return new Double(t_result.getText()).doubleValue()
}
//清除屏幕并设置所有的标识
void clear(){
firsttime = true
lastOp = null
strVal = ""
total = 0
number = 0
t_result.setText("0")
}
}
public static void main(String args[]){
CalcAppDemo c = new CalcAppDemo("简单的计算器程序")
c.setVisible(true)
}
}
//这是我以前写的,参考一下吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)