如何用JAVA语言编写计算器小程序?

如何用JAVA语言编写计算器小程序?,第1张

具体代码如下:

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()

}

}

窗体

package Calc

import java.awt.*

import java.awt.event.*

import javax.swing.*

/**

*

* 计算器程序

*

*/

public class CalcFrame extends JFrame {

JButton[] buttons = new JButton[16]// 16个按钮

StringBuffer sb = null//

JTextField text1 = null// 计算器结果显示框

String no1 = null

String sign = null// 表示+-*/符号

String[] s = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-",

  "*", "/", "=", "C" }// 面板上的字符

public CalcFrame() {

 setTitle("计算器")

 setResizable(false)

 setBounds(200, 200, 300, 350)

 setLayout(null)

 sb = new StringBuffer()

 text1 = new JTextField()

 text1.setBounds(10, 10, 250, 30)// 设置位置

 text1.setFont(new Font("Arial", Font.PLAIN, 20))// 设置字体

 text1.setForeground(Color.RED)// 设置颜色

 add(text1)

 for (int i = 0i <s.lengthi++) {

  buttons[i] = new JButton(s[i])

  buttons[i].setFont(new Font("Serif", Font.BOLD, 18))

  add(buttons[i])

 }// 生成面板上的按钮.

 buttons[0].setBounds(10, 50, 50, 40)

 buttons[0].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(0)

   text1.setText(sb.toString())

  }

 })

 buttons[1].setBounds(70, 50, 50, 40)

 buttons[1].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(1)

   text1.setText(sb.toString())

  }

 })

 buttons[2].setBounds(130, 50, 50, 40)

 buttons[2].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(2)

   text1.setText(sb.toString())

  }

 })

 buttons[3].setBounds(190, 50, 50, 40)

 buttons[3].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(3)

   text1.setText(sb.toString())

  }

 })

 buttons[4].setBounds(10, 100, 50, 40)

 buttons[4].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(4)

   text1.setText(sb.toString())

  }

 })

 buttons[5].setBounds(70, 100, 50, 40)

 buttons[5].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(5)

   text1.setText(sb.toString())

  }

 })

 buttons[6].setBounds(130, 100, 50, 40)

 buttons[6].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(6)

   text1.setText(sb.toString())

  }

 })

 buttons[7].setBounds(190, 100, 50, 40)

 buttons[7].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(7)

   text1.setText(sb.toString())

  }

 })

 buttons[8].setBounds(10, 150, 50, 40)

 buttons[8].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(8)

   text1.setText(sb.toString())

  }

 })

 buttons[9].setBounds(70, 150, 50, 40)

 buttons[9].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.append(9)

   text1.setText(sb.toString())

  }

 })

 buttons[10].setBounds(130, 150, 50, 40)

 buttons[10].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "+"

   no1 = text1.getText()

   sb.delete(0, sb.capacity())

  }

 })

 buttons[11].setBounds(190, 150, 50, 40)

 buttons[11].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "-"

   no1 = text1.getText()

   sb.delete(0, sb.capacity())

  }

 })

 buttons[12].setBounds(10, 200, 50, 40)

 buttons[12].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "*"

   no1 = text1.getText()

   sb.delete(0, sb.capacity())

  }

 })

 buttons[13].setBounds(70, 200, 50, 40)

 buttons[13].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sign = "/"

   no1 = text1.getText()

   sb.delete(0, sb.capacity())

  }

 })

 buttons[14].setForeground(Color.RED)

 buttons[14].setBounds(130, 200, 50, 40)

 buttons[14].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   int sum = 0//最终结果

   if (sign.equals("+")) {

    int no2 = Integer.parseInt(no1)//取得前一个数

    int no3 = Integer.parseInt(text1.getText())//取得现在的数

    sum = no2 + no3//累加

    text1.setText(String.valueOf(sum))

   }

   if (sign.equals("-")) {

    int no2 = Integer.parseInt(no1)

    int no3 = Integer.parseInt(text1.getText())

    sum = no2 - no3

    text1.setText(String.valueOf(sum))

   }

   if (sign.equals("*")) {

    int no2 = Integer.parseInt(no1)

    int no3 = Integer.parseInt(text1.getText())

    sum = no2 * no3

    text1.setText(String.valueOf(sum))

   }

   if (sign.equals("/")) {

    int no2 = Integer.parseInt(no1)

    int no3 = Integer.parseInt(text1.getText())

    sum = no2 / no3

    text1.setText(String.valueOf(sum))

   }

  }

 })

 buttons[15].setBounds(190, 200, 50, 40)

 buttons[15].addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

   sb.delete(0, sb.capacity())//清除sb的内容

   text1.setText("")//结果框设置为空

  }

 })

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//响应关闭窗口

 setVisible(true)//显示窗口

}

}

2.主程序类

package Calc

public class CalcTest {

public static void main(String[] args) {

 CalcFrame f = new CalcFrame()

}

}

学习Java就到IT学习联盟

打开IED:打开自己java编程的软件,采用的是eclipse软件。

建立java工程。

编写类。

编写类的详细步骤

1.类的基本结构:

由于这里用到了界面,所以要进行窗口界面的编程,按钮事件的处理,和计算处理界面;

package MyCaculator

import java.awt.*

import java.awt.event.*

import javax.swing.*

public class MyCaculator extends JFrame {

private int add=1,sub=2,mul=3,div=4

private int op=0

boolean ifOp

private String output="0"

private Button[] jba=new Button[]{new Button("7"),new Button("8"),

new Button("9"),new Button("+"),

new Button("4"),new Button("5"),new Button("6"),new Button("-"),

new Button("1"),new Button("2"),new Button("3"),new Button("*"),

new Button("0"),new Button("."),new Button("="),new Button("/")}

private JPanel jpt=new JPanel()

private JPanel jpb=new JPanel()

private JTextField jtf=new JTextField("")

private MyCaculator(){

}

private void operate(String x){

}

public String add(String x){

return output

}

public String subtract(String x){

return output

}

public String multiply(String x){

return output

}

public String divide(String x){

return output

}

public String Equals(String x){

return output

}

public void opClean(){

}

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

public static void main(String[] args) {

}

}

2.建立构造方法

所谓构造方法就是,对自己的主类进行初始化,代码如下:

private MyCaculator(){

jpt.setLayout(new BorderLayout())

jpt.add(jtf)

this.add(jpt,BorderLayout.NORTH)

jpb.setLayout(new GridLayout(4,4))

for(int i=0i<jba.lengthi++){

jpb.add(jba[i])

if(i==3||i==7||i==11||i==15||i==14)

jba[i].addActionListener(new setOperate_Act())

else

jba[i].addActionListener(new setLabel_Act())

}

this.add(jpb,BorderLayout.CENTER)

this.setSize(250, 200)

this.setResizable(false)

this.setVisible(true)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

3.建立数据计算方法

这里的数据计算方法有6个,一个是主方法其他几个是加减乘除的处理方法,代码如下:

private void operate(String x){

double x1=Double.valueOf(x)

double y=Double.valueOf(output)

switch(op){

case 0:output=xbreak

case 1:output=String.valueOf(y+x1)break

case 2:output =String.valueOf(y-x1)break

case 3:output =String.valueOf(y*x1)break

case 4:

if(x1!=0) output=String.valueOf(y/x1)

else output="不能为0"

break

}

}

public String add(String x){

operate(x)

op=add

return output

}

public String subtract(String x){

operate(x)

op=sub

return output

}

public String multiply(String x){

operate(x)

op=mul

return output

}

public String divide(String x){

operate(x)

op=div

return output

}

public String Equals(String x){

operate(x)

op=0

return output

}

public void opClean(){

op=0

output ="0"

}

4.事件处理方法

这里的时间处理方法,没有建立一个整体的方法,二是在为了便于处理的方法,将按钮事件分成两个部分,并采用两个子类来实现,这两个类时内部类要写在主类内部的,代码如下:

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jba[3]){

jtf.setText(add(jtf.getText()))

ifOp=true

}

else if(e.getSource()==jba[7]){

jtf.setText(subtract(jtf.getText()))

ifOp=true

}

else if(e.getSource()==jba[11]){

jtf.setText(multiply(jtf.getText()))

ifOp=true

}

else if(e.getSource()==jba[15]){

jtf.setText(divide(jtf.getText()))

ifOp=true

}

else if(e.getSource()==jba[14]){

jtf.setText(Equals(jtf.getText()))

ifOp=true

}

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

Button tempb=(Button)e.getSource()

if(ifOp){

jtf.setText(tempb.getLabel())

ifOp=false

}else {

jtf.setText(jtf.getText()+tempb.getLabel())

}

}

}

5.建立main方法:

要想实现我们的代码,我们需在main方法中,实例化我们的对象。

public static void main(String[] args) {

new MyCaculator()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7726680.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存