Java暑期实训——简易计算器
生活随笔
收集整理的這篇文章主要介紹了
Java暑期实训——简易计算器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
任務(wù)一:簡(jiǎn)易計(jì)算器的設(shè)計(jì)
實(shí)訓(xùn)內(nèi)容:模仿Windows自帶的標(biāo)準(zhǔn)版計(jì)算器,設(shè)計(jì)并用Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器(根據(jù)自己的能力,可以適當(dāng)?shù)卦黾踊騽h除部分功能)。
最低要求:計(jì)算器運(yùn)行界面如下圖所示,包含二個(gè)文本框(分別顯示算式和運(yùn)算結(jié)果)、10個(gè)數(shù)字按鈕(0~9)、4個(gè)運(yùn)算按鈕、一個(gè)等號(hào)按鈕、一個(gè)清除按鈕,要求將按鍵和結(jié)果顯示在文本框中。
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;//第一個(gè)數(shù)private String x = "";//第二個(gè)數(shù)private String y = "";//運(yùn)算符private String fh = "";//輸出結(jié)果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() {//設(shè)置frame的標(biāo)題,坐標(biāo),大小。frame =new JFrame("splendid‘s Calculator");frame.setBounds(700,150,450,540);frame.setResizable(false);//絕對(duì)布局frame.setLayout(null);}private void MyTestField() {//設(shè)置文本框1 的大小位置,字體,顏色textField1 =new JTextField();textField1.setHorizontalAlignment(JTextField.LEFT);textField1.setFont(new Font("黑體",Font.BOLD,35));textField1.setBackground(new Color(195, 195, 232));//設(shè)置文本框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();//設(shè)置面板的布局方式panel2.setBounds(20,90,400,350);//表格布局panel2.setLayout(new GridLayout(4,4,8,8));for(int i =0;i<button.length;i++){//創(chuàng)建按鈕button[i] =new JButton(arr[i]);//設(shè)置按鈕字體button[i].setFont(new Font("黑體",Font.CENTER_BASELINE,20));//設(shè)置按鈕背景顏色button[i].setBackground(new Color(242,240,235));//添加監(jiān)聽事件button[i].addActionListener( this);panel2.add(button[i]);}frame.add(panel2,BorderLayout.SOUTH); }//計(jì)算器功能實(shí)現(xiàn)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("版權(quán)所有:splendid");//設(shè)置標(biāo)簽的字體,大小及顏色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();}}?
總結(jié)
以上是生活随笔為你收集整理的Java暑期实训——简易计算器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux 解决ssh连接慢的问题
- 下一篇: 使用JOTM实现分布式事务管理(多数据源