欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)圖形化界面計算器

 更新時間:2020年05月18日 17:13:49   作者:PayneWoo  
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)圖形化界面計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)圖形化界面計算器的具體代碼,供大家參考,具體內(nèi)容如下

最終效果圖:

項目流程:

第一步:實現(xiàn)圖形化界面(添加計算器的 Button 和 用于顯示輸入數(shù)字、輸出結(jié)果的JTextField等)

第二步:給按鈕和文本框添加鼠標(biāo)監(jiān)聽事件。

第三步:實現(xiàn)加減乘除、開方、平方、清零和退格功能。

開方運算:

平方運算:

加法運算:

減法運算:

乘法運算:

除法運算:

完整項目代碼:

package First_App;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Caculator extends JFrame{
 /*
  * 圖形化界面設(shè)計
  * */
 private static final long serialVersionUID = 4907149509182425824L;
 public Caculator(){
  Container c = getContentPane(); //定義一個頂級容器c
  setLayout(new GridLayout(2,1));//新建網(wǎng)格布局管理器,2行1列
  JTextField jtf = new JTextField("0",40);//構(gòu)造一個用指定文本和列初始化的新文本框--jtf
   jtf.setHorizontalAlignment(JTextField.RIGHT);//設(shè)置水平對齊方式:居右對齊
  JButton data0 = new JButton("0");
  JButton data1 = new JButton("1");
  JButton data2 = new JButton("2");
  JButton data3 = new JButton("3");
  JButton data4 = new JButton("4");
  JButton data5 = new JButton("5");
  JButton data6 = new JButton("6");
  JButton data7 = new JButton("7");
  JButton data8 = new JButton("8");
  JButton data9 = new JButton("9");
  JButton point = new JButton(".");
  JButton equ = new JButton("=");
  JButton plus = new JButton("+");
  JButton minus = new JButton("-");
  JButton mtp = new JButton("*");
  JButton dvd = new JButton("/");
  JButton sqr = new JButton("sqrt");
  JButton root = new JButton("x^2");
  JButton tg = new JButton("退格");
  JButton ql = new JButton("清零");
  JPanel jp = new JPanel(); //新建JPanel面板--jp
  jp.setLayout(new GridLayout(4,5,5,5));//新建網(wǎng)格布局管理器(行數(shù),列數(shù),組件間的水平垂直間距)
  jp.add(data7);
  jp.add(data8);
  jp.add(data9);
  jp.add(plus);
  jp.add(sqr);
  jp.add(data4);
  jp.add(data5);
  jp.add(data6);
  jp.add(minus);
  jp.add(root);
  jp.add(data1);
  jp.add(data2);
  jp.add(data3);
  jp.add(mtp);
  jp.add(ql);
  jp.add(data0);
  jp.add(point);
  jp.add(equ);
  jp.add(dvd);
  jp.add(tg);
  c.add(jtf);//將文本框jtf添加到頂級容器c中
  c.add(jp);//將JPanel面板jp添加到頂級容器c中
  setSize(400,300);
  setTitle("計算器");
  setVisible(true);
  setResizable(false);//窗體大小由程序員決定,用戶不能自由改變大小
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  /*
   * *********************************************************
   *     
     相關(guān)計算功能的實現(xiàn)        
   * *********************************************************
   * */

  data0.addActionListener(new ActionListener(){//數(shù)字0的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){//將按鈕值與0作比較
     jtf.requestFocus();//把輸入焦點放在調(diào)用這個方法的控件上(即把光標(biāo)放在文本框jtf里)
    }
    else{
     String str = jtf.getText();//取得當(dāng)前按鈕的按鈕值
     jtf.setText(str+"0"); //將文本內(nèi)容后加上字符0
    }
   }
  });
  data1.addActionListener(new ActionListener(){//數(shù)字1的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){//將按鈕值與0作比較
     jtf.setText("");//將文本框初始化為空
     jtf.setText("1");//將文本框內(nèi)容置為 1
     jtf.requestFocus();//把輸入焦點放在調(diào)用這個方法的控件上(即把光標(biāo)放在文本框jtf里)
    }
    else{
     String str = jtf.getText();//取得當(dāng)前按鈕的按鈕值
     jtf.setText(str+"1"); //將文本內(nèi)容后加上字符1
    }
   }
  });
  data2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("2");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"2");
    }
   }
  });
  data3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("3");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"3");
    }
   }
  });
  data4.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("4");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"4");
    }
   }
  });
  data5.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("5");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"5");
    }
   }
  });
  data6.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("6");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"6");
    }
   }
  });
  data7.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("7");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"7");
    }
   }
  });
  data8.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("8");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"8");
    }
   }
  });
  data9.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("9");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"9");
    }
   }
  });
  point.addActionListener(new ActionListener(){ //點號的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText(".");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+".");
    }
   }
  });
  plus.addActionListener(new ActionListener(){ //+號的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("+");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"+");
    }
   }
  });
  minus.addActionListener(new ActionListener(){ //-號的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("-");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"-");
    }
   }
  });
  mtp.addActionListener(new ActionListener(){ //*號的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("*");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"*");
    }
   }
  });
  dvd.addActionListener(new ActionListener(){ //除號的輸入
   public void actionPerformed(ActionEvent arg0){
    if(jtf.getText().equals("0")){
     jtf.setText("");
     jtf.setText("/");
     jtf.requestFocus();
    }
    else{
     String str = jtf.getText();
     jtf.setText(str+"/");
    }
   }
  });
  //【**退格功能如下**】
  tg.addActionListener(new ActionListener(){//監(jiān)聽退格鍵
   public void actionPerformed(ActionEvent arg0){//處理退格鍵被按下的事件
    String text = jtf.getText();
    int i = text.length();
    if(i>0){
     text = text.substring(0,i-1);//去掉最后一個字符
     if (text.length() == 0) {// 如果文本沒有了內(nèi)容,則初始化計算器的各種值
      jtf.setText("0");
     } else { // 顯示新的文本
      jtf.setText(text);
    }
   }
   }
  });
  //【**清零功能如下**】
  ql.addActionListener(new ActionListener(){//監(jiān)聽清零鍵
   public void actionPerformed(ActionEvent e) {
    jtf.setText("0");//將文本框置為0(清零功能)
   }

  });
  //【**平方功能如下**】
  root.addActionListener(new ActionListener(){//監(jiān)聽root鍵
   public void actionPerformed(ActionEvent e){//root鍵被按事件
    String i = jtf.getText();
    Double j = Double.parseDouble(i);//將字符串i轉(zhuǎn)換成對應(yīng)的double類型的數(shù)值
    double ans = j*j; //求平方
    String answer =String.valueOf(ans);//將int型數(shù)據(jù)轉(zhuǎn)換成String類型
    jtf.setText(answer);//將文本框設(shè)置為平方后的結(jié)果
   }
  });
  //【**開方功能如下**】
  sqr.addActionListener(new ActionListener(){//監(jiān)聽sqrt鍵
   public void actionPerformed(ActionEvent e){//sqrt鍵被按事件
    String i = jtf.getText();
    Double j = Double.parseDouble(i);//將字符串轉(zhuǎn)換成對應(yīng)的double類型的數(shù)值
    double ans = (double)Math.sqrt(j);//求開方
    String answer = String.valueOf(ans);//將double型數(shù)據(jù)轉(zhuǎn)換成String類型
    jtf.setText(answer);//將文本框設(shè)置為開方后的結(jié)果
   }
  });

  //【等號實現(xiàn) 加減乘除 功能】
  equ.addActionListener(new ActionListener(){ //監(jiān)聽 “等號” 按鍵
   public void actionPerformed(ActionEvent arg0){//處理“等號” 按鍵被按下事件
    //【**加法運算**】
    if(jtf.getText().indexOf("+")!= -1){
     //將字符串分割為子字符串,然后將結(jié)果作為字符串?dāng)?shù)組返回
     String[] s = jtf.getText().split("[+]");//轉(zhuǎn)義字符,要用"[+]"或者"\+"
     Double d1 = Double.parseDouble(s[0]);//返回一個指定字符串表示的double值
     Double d2 = Double.parseDouble(s[1]);
     double ans = d1 + d2;
     String answer = String.valueOf(ans);//將結(jié)果轉(zhuǎn)換為字符串
     jtf.setText(answer);//將加法運算的結(jié)果以字符串形式在文本框中顯示
    }
    //【**減法運算**】
    else if(jtf.getText().indexOf("-")!= -1){
     String[] s = jtf.getText().split("-");
     jtf.setText("");
     Double d1 = Double.parseDouble(s[0]);
     Double d2 = Double.parseDouble(s[1]);
     double ans = d1-d2;
     String answer =String.valueOf(ans);
     jtf.setText(answer);
    }
    //【**乘法運算**】
    else if(jtf.getText().indexOf("*")!= -1){
     String[] s = jtf.getText().split("[*]");//*是轉(zhuǎn)義字符,要用"[*]",或者"\*"
     jtf.setText("");
     Double d1 = Double.parseDouble(s[0]);
     Double d2 = Double.parseDouble(s[1]);
     double ans = d1*d2;
     String answer =String.valueOf(ans);
     jtf.setText(answer);
    }
    //【**除法運算**】
    else if(jtf.getText().indexOf("/")!= -1){
     String[] s = jtf.getText().split("/");
     jtf.setText("");
     Double d1 = Double.parseDouble(s[0]);
     Double d2 = Double.parseDouble(s[1]);
     double ans = d1/d2;
     String answer =String.valueOf(ans);
     jtf.setText(answer);
    }
    else{
     jtf.setText("請選擇要進行的運算");
    }
   }
  });
 }
 public static void main(String[] args) {
  new Caculator();
 }
}

總結(jié):

1.掌握基本的GUI添加按鈕、文本框的方法
2.掌握字符串的處理,這里用到了indexOf()、split()等方法
3.注意Java中遇到的轉(zhuǎn)義字符。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringJPA?做分頁條件查詢的代碼實踐

    SpringJPA?做分頁條件查詢的代碼實踐

    相信小伙伴們的項目很多都用到SpringJPA框架的吧,對于單表的增刪改查利用jpa是很方便的,但是對于條件查詢并且分頁?是不是很多小伙伴不經(jīng)常寫到,今天給大家分享SpringJPA?做分頁條件查詢的案例代碼,感興趣的朋友一起看看吧
    2024-03-03
  • SpringBoot中處理日期的兩種方式小結(jié)

    SpringBoot中處理日期的兩種方式小結(jié)

    本文主要介紹了SpringBoot中處理日期的兩種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java實現(xiàn)經(jīng)典游戲2048的示例代碼

    Java實現(xiàn)經(jīng)典游戲2048的示例代碼

    2014年Gabriele Cirulli利用周末的時間寫2048這個游戲的程序。本文將用java語言實現(xiàn)這一經(jīng)典游戲,并采用了swing技術(shù)進行了界面化處理,需要的可以參考一下
    2022-02-02
  • Java使用I/O流讀取文件內(nèi)容的方法詳解

    Java使用I/O流讀取文件內(nèi)容的方法詳解

    這篇文章主要介紹了Java使用I/O流讀取文件內(nèi)容的方法,結(jié)合實例形式詳細(xì)分析了java使用I/O流讀取文件常見操作技巧,需要的朋友可以參考下
    2019-11-11
  • java 實現(xiàn)DES 加密解密的示例

    java 實現(xiàn)DES 加密解密的示例

    這篇文章主要介紹了java 實現(xiàn)DES 加密解密的示例代碼,幫助大家更好的理解和使用Java進行加解密,感興趣的朋友可以了解下
    2020-12-12
  • SpringBoot配置Redis自定義過期時間操作

    SpringBoot配置Redis自定義過期時間操作

    這篇文章主要介紹了SpringBoot配置Redis自定義過期時間操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringCloud Open feign 使用okhttp 優(yōu)化詳解

    SpringCloud Open feign 使用okhttp 優(yōu)化詳解

    這篇文章主要介紹了SpringCloud Open feign 使用okhttp 優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 解析SpringCloud簡介與微服務(wù)架構(gòu)

    解析SpringCloud簡介與微服務(wù)架構(gòu)

    這篇文章主要介紹了SpringCloud簡介與微服務(wù)架構(gòu),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java BigDecimal類用法詳解

    Java BigDecimal類用法詳解

    BigDecimal 由任意精度的整數(shù)非標(biāo)度值 和32 位的整數(shù)標(biāo)度 (scale) 組成。如果為零或正數(shù),則標(biāo)度是小數(shù)點后的位數(shù)。如果為負(fù)數(shù),則將該數(shù)的非標(biāo)度值乘以 10 的負(fù)scale 次冪。
    2016-06-06
  • Hibernate用ThreadLocal模式(線程局部變量模式)管理Session

    Hibernate用ThreadLocal模式(線程局部變量模式)管理Session

    今天小編就為大家分享一篇關(guān)于Hibernate用ThreadLocal模式(線程局部變量模式)管理Session,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論