Java暑期实训——简易计算器
生活随笔
收集整理的這篇文章主要介紹了
Java暑期实训——简易计算器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
任務一:簡易計算器的設計
實訓內容:模仿Windows自帶的標準版計算器,設計并用Java語言實現簡易的計算器(根據自己的能力,可以適當地增加或刪除部分功能)。
最低要求:計算器運行界面如下圖所示,包含二個文本框(分別顯示算式和運算結果)、10個數字按鈕(0~9)、4個運算按鈕、一個等號按鈕、一個清除按鈕,要求將按鍵和結果顯示在文本框中。
package Mycalculator; import javax.swing.*; import java.awt.*; import java.awt.event.*;public class Calculator implements ActionListener {private JFrame frame;//組合原理private ImageIcon icon;private JTextField textField1;private JTextField textField2;private JButton[] button;private JPanel panel1;private JPanel panel2;private JLabel label;//第一個數private String x = "";//第二個數private String y = "";//運算符private String fh = "";//輸出結果private double answer;//初始化 public void init(){MyFrame();MyIcon();MyTestField();MyButton();MyLabel();display(); }private void display() {frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.setVisible(true);}private void MyFrame() {//設置frame的標題,坐標,大小。frame =new JFrame("splendid‘s Calculator");frame.setBounds(700,150,450,540);frame.setResizable(false);//絕對布局frame.setLayout(null);}private void MyTestField() {//設置文本框1 的大小位置,字體,顏色textField1 =new JTextField();textField1.setHorizontalAlignment(JTextField.LEFT);textField1.setFont(new Font("黑體",Font.BOLD,35));textField1.setBackground(new Color(195, 195, 232));//設置文本框2 的大小位置,字體,顏色textField2 =new JTextField();textField2.setHorizontalAlignment(JTextField.RIGHT);textField2.setFont(new Font("黑體",Font.BOLD,35));textField2.setBackground(new Color(189, 189, 232));//將文本框添加到面板上panel1 = new JPanel();panel1.add(textField1);panel1.add(textField2);panel1.setLayout(new GridLayout(2,1));panel1.setBounds(20,15,400,60);//將文本框放在容器上面frame.add(panel1,BorderLayout.NORTH);}private void MyIcon() {}private void MyButton() {// 按鈕文本String[] arr ={"7","8","9","*","4","5","6","/","1","2","3","-","0","CE","+","=", };// 按鈕button = new JButton[arr.length];panel2 =new JPanel();//設置面板的布局方式panel2.setBounds(20,90,400,350);//表格布局panel2.setLayout(new GridLayout(4,4,8,8));for(int i =0;i<button.length;i++){//創建按鈕button[i] =new JButton(arr[i]);//設置按鈕字體button[i].setFont(new Font("黑體",Font.CENTER_BASELINE,20));//設置按鈕背景顏色button[i].setBackground(new Color(242,240,235));//添加監聽事件button[i].addActionListener( this);panel2.add(button[i]);}frame.add(panel2,BorderLayout.SOUTH); }//計算器功能實現public void calculate(String z) {if (z.equals("+")) answer = Double.parseDouble(x) + Double.parseDouble(y);else if (z.equals("-")) answer = Double.parseDouble(x) - Double.parseDouble(y);else if (z.equals("*")) answer = Double.parseDouble(x) * Double.parseDouble(y);else if (z.equals("/")) answer = Double.parseDouble(x) / Double.parseDouble(y);else answer = Double.parseDouble(x);//將答案顯示x = Double.toString(answer);if(x.length()>6) textField2.setText(x.substring(0,10));else textField2.setText(x);y = "";answer = 0;fh = "";}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("0")|| e.getActionCommand().equals("1")|| e.getActionCommand().equals("2")|| e.getActionCommand().equals("3")|| e.getActionCommand().equals("4")|| e.getActionCommand().equals("5")|| e.getActionCommand().equals("6")|| e.getActionCommand().equals("7")|| e.getActionCommand().equals("8")|| e.getActionCommand().equals("9")) {if (fh.equals("")) {x = x + e.getActionCommand();if (x.startsWith("00")) x.substring(1);textField1.setText(x);} else {y = y + e.getActionCommand();if (y.startsWith("00")) y.substring(1);textField1.setText(x+fh+y);}}//清空if (e.getActionCommand().equals("CE")) {x = "";y = "";fh = "";textField1.setText("");textField2.setText("");}if (e.getActionCommand().equals("+")) {if (!fh.equals("")) calculate(fh);fh = "+";textField1.setText(x+fh);}if (e.getActionCommand().equals("-")) {if (!fh.equals("")) calculate(fh);fh = "-";textField1.setText(x+fh);}if (e.getActionCommand().equals("*")) {if (!fh.equals("")) calculate(fh);fh = "*";textField1.setText(x+fh);}if (e.getActionCommand().equals("/")) {if (!fh.equals("")) calculate(fh);fh = "/";textField1.setText(x+fh);}if (e.getActionCommand().equals("=")) {calculate(fh);}}private void MyLabel() {label = new JLabel();label.setText("版權所有:splendid");//設置標簽的字體,大小及顏色label.setFont(new Font("黑體",Font.CENTER_BASELINE,25));label.setForeground(Color.RED);label.setBounds(4, 470, 300, 40);frame.add(label);}public static void main(String[] args) {Calculator cal =new Calculator();cal.init();}}?
總結
以上是生活随笔為你收集整理的Java暑期实训——简易计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 解决ssh连接慢的问题
- 下一篇: java美元兑换,(Java实现) 美元