国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

java實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

2024-07-14 08:41:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文為大家分享了java實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,具體內(nèi)容如下

題目:

編寫一個(gè)模擬計(jì)算器的程序。在面板中添加一個(gè)文本框(顯示按鍵及運(yùn)算結(jié)果)、

10個(gè)數(shù)字按鈕(0~9)、4個(gè)運(yùn)算按鈕(加、減、乘、除)、一個(gè)等號(hào)按鈕、一個(gè)清除按鈕,

要求將按鍵和結(jié)果顯示在文本框中。

代碼過(guò)程展示:

import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class Exercise1 extends JFrame implements ActionListener{ private JPanel p1 = new JPanel(); //創(chuàng)建面板   private JPanel p2 = new JPanel(); //創(chuàng)建面板   private JTextField t1;  //文本框1用來(lái)顯示輸入信息   StringBuffer str;//輸入的字符串  JButton[] b=new JButton[10];  JButton b1,b2,b3,b4,b5,b6;//16個(gè)按鈕  double x,y;  int n; public Exercise1() {   super("假隊(duì)長(zhǎng)的大目標(biāo)");    setSize(350,300); //設(shè)置窗口大小   setLocationRelativeTo(null); //顯示到中間         Container c = getContentPane(); //創(chuàng)建內(nèi)容面板對(duì)象       t1 = new JTextField(25);     t1.setEditable(false); //只能顯示,不能編輯         p2.add(t1);  //添加文本框到面板     p2.setLayout(new GridLayout(3,2)); //把面扳布局為4行1列     str=new StringBuffer();     //實(shí)例化各個(gè)按鈕     for(int i=0;i<10;i++) //分別為數(shù)組中0~9號(hào)的按鈕設(shè)置標(biāo)簽,并注冊(cè)監(jiān)聽器     {      String s=""+i;      b[i]= new JButton(s);       b[i].addActionListener(this);      }         b1=new JButton("+");    b2=new JButton("-");    b3=new JButton("*");    b4=new JButton("/");      b5=new JButton("=");    b6=new JButton("delete");          //添加到面板    p1.add(b[7]);    p1.add(b[8]);    p1.add(b[9]);    p1.add(b1);    p1.add(b[4]);    p1.add(b[5]);    p1.add(b[6]);     p1.add(b2);    p1.add(b[1]);    p1.add(b[2]);    p1.add(b[3]);      p1.add(b3);    p1.add(b[0]);    p1.add(b5);    p1.add(b6);    p1.add(b4);       p1.setLayout(new GridLayout(4,5,10,10));         //注冊(cè)監(jiān)聽器          b1.addActionListener(this);    b2.addActionListener(this);    b3.addActionListener(this);    b4.addActionListener(this);    b5.addActionListener(this);    b6.addActionListener(this);      //將內(nèi)容添加到面板上然后添加到容器里    c.add(p2);     c.add(p1);     c.setLayout(new FlowLayout()); //設(shè)置為順序布局     //設(shè)置窗口關(guān)閉動(dòng)作     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗口關(guān)閉動(dòng)作     setVisible(true); //設(shè)置為可見     setResizable(false); //禁止調(diào)整框架大小     }  public static void main(String[] args) { // TODO Auto-generated method stub @SuppressWarnings("unused") Exercise1 calculate=new Exercise1(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub   if(e.getSource()==b6){    t1.setText("0");//清零    t1.setHorizontalAlignment(JTextField.RIGHT);//右對(duì)齊    str.setLength(0); }             //Double.parseDouble  將字符串轉(zhuǎn)化為double類型       //t1.getText().trim()  獲取字符保存后并且清除      else if (e.getSource()==b1)//單擊加號(hào)按鈕獲得x的值并清空y的值    {     x=Double.parseDouble(t1.getText().trim());     str.setLength(0);     y=0d;     n=0;  }else if(e.getSource()==b2)//減法運(yùn)算  {  x=Double.parseDouble(t1.getText().trim());     str.setLength(0);     y=0d;    n=1; }else if(e.getSource()==b3)//乘法運(yùn)算 {  x=Double.parseDouble(t1.getText().trim());     str.setLength(0);     y=0d;    n=2; }else if(e.getSource()==b4)//除法運(yùn)算 {  x=Double.parseDouble(t1.getText().trim());     str.setLength(0);     y=0d;    n=3; }else if(e.getSource()==b5)//等號(hào) {  str.setLength(0);  switch(n){  case 0:t1.setText(""+(x+y));break;  case 1:t1.setText(""+(x-y));break;  case 2:t1.setText(""+(x*y));break;  case 3:t1.setText(""+(x/y));break;  }  }else{ if(e.getSource()==b[0]) {  if(t1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作      {}      else  t1.setText(str.append(e.getActionCommand()).toString());  t1.setHorizontalAlignment(JTextField.RIGHT);   y=Double.parseDouble(t1.getText().trim());  } else     {     t1.setText(str.append(e.getActionCommand()).toString());     t1.setHorizontalAlignment(JTextField.RIGHT);     y=Double.parseDouble(t1.getText().trim());       } } } }

總結(jié):代碼有點(diǎn)冗長(zhǎng),但是真正的看懂之后其實(shí)并不復(fù)雜。當(dāng)然了,這還只是一個(gè)簡(jiǎn)易的模擬計(jì)算器,

也可以在其中加入其他功能。比如說(shuō)加入指數(shù)運(yùn)算,冪運(yùn)算,開方運(yùn)算,或者為了使界面美觀,

再加入一個(gè)結(jié)果文本框,上面顯示輸入的數(shù)字,下面顯示出結(jié)果。當(dāng)然了說(shuō)這么多,還是要靠讀者自己去鉆研。

以上全部為本篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鹿泉市| 甘谷县| 卫辉市| 息烽县| 分宜县| 漾濞| 北票市| 定结县| 定南县| 漾濞| 登封市| 景宁| 常山县| 衡山县| 台南市| 祁门县| 隆安县| 太白县| 锡林郭勒盟| 塘沽区| 孝义市| 耒阳市| 阳朔县| 湘潭市| 厦门市| 梅河口市| 望奎县| 佛教| 聂拉木县| 广水市| 浑源县| 五华县| 卫辉市| 萨嘎县| 白沙| 辽阳市| 瓦房店市| 左权县| 泾源县| 汉中市| 乌拉特前旗|