一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 簡單的計算器程序實例代碼

java 簡單的計算器程序實例代碼

2020-11-23 13:00Allenalex Java教程

這篇文章主要介紹了java 簡單的計算器程序實例代碼的相關資料,需要的朋友可以參考下

java 簡單的計算器程序

實現實例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
public class Calculator
{
  public static void main(String[] args)
  {
   EventQueue.invokeLater(new Runnable()
     {
      public void run()
      {
        CalculatorFrame frame = new CalculatorFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
     });
  }
}
 
 
/**
 * A frame with a calculator panel.
 */
class CalculatorFrame extends JFrame
{
  public CalculatorFrame()
  {
    setTitle("Calculator");
    CalculatorPanel panel=new CalculatorPanel();
    add(panel);
    pack();
  }
}
 
 
class CalculatorPanel extends JPanel
{
  private JButton display;
    private JPanel panel;
    private double result;
    private String lastCommand;
    private boolean start;
  public CalculatorPanel()
  {
    setLayout(new BorderLayout());
     
    result=0;
    lastCommand="=";
    start=true;
     
     // add the display
    display=new JButton("0");
    display.setEnabled(false);
    add(display,BorderLayout.NORTH);
     
    ActionListener insert=new InsertAction();
    ActionListener command=new CommandAction();
     
    panel=new JPanel();
    panel.setLayout(new GridLayout(4,4));
     
     addButton("7", insert);
     addButton("8", insert);
     addButton("9", insert);
     addButton("/", command);
 
 
     addButton("4", insert);
     addButton("5", insert);
     addButton("6", insert);
     addButton("*", command);
 
 
     addButton("1", insert);
     addButton("2", insert);
     addButton("3", insert);
     addButton("-", command);
 
 
     addButton("0", insert);
     addButton(".", insert);
     addButton("=", command);
     addButton("+", command);
 
 
     add(panel, BorderLayout.CENTER);
     
    }
  private void addButton(String label,ActionListener listener)
  {
    JButton button=new JButton(label);
    button.addActionListener(listener);
    panel.add(button);
  }
  /**
    * This action inserts the button action string to the end of the display text.
    */
  private class InsertAction implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      String input=event.getActionCommand();
      if(start)
      {
        display.setText("");
        start=false;
      }
      display.setText(display.getText()+input);
    }
  }
   /**
    * This action executes the command that the button action string denotes.
    */
  private class CommandAction implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      String command=event.getActionCommand();
      if(start)
      {
        if (command.equals("-"))
        {
          display.setText(command);
          start = false;
        }
        else lastCommand = command;
      }else {
        calculate(Double.parseDouble(display.getText()));
        lastCommand=command;
        start=true;
      }
    }
  }
  /**
    * Carries out the pending calculation.
    * @param x the value to be accumulated with the prior result.
    */
  public void calculate(double x)
  {
     if (lastCommand.equals("+")) result += x;
     else if (lastCommand.equals("-")) result -= x;
     else if (lastCommand.equals("*")) result *= x;
     else if (lastCommand.equals("/")) result /= x;
     else if (lastCommand.equals("=")) result = x;
     display.setText("" + result);
  }
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/Allenalex/article/details/10211657

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91精品国产美女福到在线不卡 | 91国内精品久久久久影院优播 | 国产精品嫩草影院一二三区 | 国产aaaaa一级毛片 | 国产免费又粗又猛又爽视频国产 | 好大好深好涨好烫还要 | 全是女性放屁角色的手游 | 双性np玩烂了np欲之国的太子 | 经典三级四虎在线观看 | 初尝黑人巨大h文 | 天美网站传媒入口网址 | 视频免费看 | 91嫩草国产在线观看免费 | 大伊香蕉精品二区视频在线 | 情人我吃糖果小说 | 日本免费不卡在线一区二区三区 | 午夜精品久久久久久久2023 | 日韩视频在线免费 | 色5月婷婷| 日韩精品一区二区 | china精品对白普通话 | 亚洲国产五月综合网 | 91高跟丝袜 | 亚洲一区二区三区在线播放 | 久久精品人人做人人爽97 | 精品综合久久久久久8888 | 美女狂揉尿口揉到失禁 | 韩日一区二区三区 | 亚洲成综合人影院在院播放 | 亚洲成人mv| 高清视频在线播放 | 精品精品国产自在现拍 | 免费网站看v片在线香蕉 | 色戒完整版 | 青丝视频免费版在线看 | 国产探花在线观看 | gayrb免费漫画入口 | 狠狠干综合网 | 32pao强力打造免费高速高清 | 女教师系列三上悠亚在线观看 | 日本大学jalapsiki |