简单的Swing做的计算器

哈哈哈,开心,第一次用所学到的知识完成了一个小小的作品吧。
本来不想学swing的,觉得没什么用,但是看了两本书都在java基础的结束讲解了swing的内容,想想还是看看吧,由于我安装了图形化操作模式,所以我直接看了事件处理部分,刚看完点击事件部分就想着是不是可以用这部分做个计算器?

想到就做!直接画图做了个框架,,如图所示,界面做好了,接下来就是功能的实现了,先想清楚操作流程:
1.输入一个数字:利用界面小键盘输入,还要显示在上面的文本框,网上查了关于button和textFile之间数据的传递的资料,直接在点击某个数字的时候进入点击事件,然后先gettext()得到文本框的数据,然后+settext()显示在文本框中,实现了数字的输入。

2.点击运算符+,-,*,/,等,那就需要在点击事件中先得到文本框的内容,赋值给number1,还要区别+,-,*,/,于是定义了一个flag,+,-,*,/,分别对应不同的值。

3.输入第二个数,和第一个数一样
4.点击=,文本框中显示结果,点击=进入点击事件,先判断之前flag的值,然后用if语句进行运算,然后把结果result显示在文本框中。

进行测试+=,但是没法进行第二次运算,于是又添加了一个AC按钮,进行清零。
整体代码如下:
package base.swing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class eventDemo1 extends JFrame {
 
 int number1;
 int number2;
 int flag;
 int result;
 private JPanel contentPane;
 private JTextField textField;
 private JButton button;
 private JButton button_1;
 private JButton button_2;
 private JButton button_3;
 private JButton button_4;
 private JButton button_5;
 private JButton button_6;
 private JButton button_7;
 private JButton button_8;
 private JButton button_9;
 private JButton button_10;
 private JButton button_11;
 private JButton button_12;
 private JButton button_13;
 private JButton button_14;
 private JButton btnClear;
 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     eventDemo1 frame = new eventDemo1();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 /**
  * Create the frame.
  */
 public eventDemo1() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  textField = new JTextField();
  textField.setBounds(104, 35, 236, 21);
  contentPane.add(textField);
  textField.setColumns(10);
  
  button = new JButton("-");
  button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    flag = 1;
    number1 = Integer.parseInt(textField.getText());
    textField.setText(null);
   }
  });
  button.setBounds(164, 66, 52, 23);
  contentPane.add(button);
  
  button_1 = new JButton("/");
  button_1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    flag = 3;
    number1 = Integer.parseInt(textField.getText());
    textField.setText(null);
   }
  });
  button_1.setBounds(286, 66, 50, 25);
  contentPane.add(button_1);
  
  button_2 = new JButton("*");
  button_2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    flag = 2;
    number1 = Integer.parseInt(textField.getText());
    textField.setText(null);
   }
  });
  button_2.setBounds(226, 66, 50, 23);
  contentPane.add(button_2);
  
  button_3 = new JButton("7");
  button_3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    
    textField.setText(textField.getText()+7);
    
   }
  });
  button_3.setBounds(104, 99, 50, 25);
  contentPane.add(button_3);
  
  button_4 = new JButton("8");
  button_4.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    
    textField.setText(textField.getText()+8);
   }
  });
  button_4.setBounds(164, 99, 50, 25);
  contentPane.add(button_4);
  
  button_5 = new JButton("9");
  button_5.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+9);
   }
  });
  button_5.setBounds(226, 99, 50, 25);
  contentPane.add(button_5);
  
  button_6 = new JButton("4");
  button_6.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+4);
   }
  });
  button_6.setBounds(104, 133, 50, 25);
  contentPane.add(button_6);
  
  button_7 = new JButton("5");
  button_7.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+5);
   }
  });
  button_7.setBounds(164, 133, 50, 25);
  contentPane.add(button_7);
  
  button_8 = new JButton("6");
  button_8.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+6);
   }
  });
  button_8.setBounds(226, 133, 50, 25);
  contentPane.add(button_8);
  
  button_9 = new JButton("1");
  button_9.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+1);
   }
  });
  button_9.setBounds(104, 165, 50, 25);
  contentPane.add(button_9);
  
  button_10 = new JButton("2");
  button_10.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+2);
   }
  });
  button_10.setBounds(164, 165, 50, 25);
  contentPane.add(button_10);
  
  button_11 = new JButton("3");
  button_11.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+3);
   }
  });
  button_11.setBounds(226, 165, 50, 25);
  contentPane.add(button_11);
  
  button_12 = new JButton("=");
  button_12.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    number2 = Integer.parseInt(textField.getText());
    if(flag==0) {
     result = number1+number2;
     textField.setText(String.valueOf(result));
    }else if (flag==1) {
     result = number1-number2;
     textField.setText(String.valueOf(result));
    }
    else if (flag==2) {
     result = number1*number2;
     textField.setText(String.valueOf(result));
    }
    else if (flag==3) {
     result = number1/number2;
     textField.setText(String.valueOf(result));
    }
    
    
    
    
    
   }
  });
  button_12.setBounds(286, 102, 50, 25);
  contentPane.add(button_12);
  
  button_13 = new JButton("+");
  button_13.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    flag = 0;
    number1 = Integer.parseInt(textField.getText());
    textField.setText(null);
    
   }
  });
  button_13.setBounds(104, 66, 50, 23);
  contentPane.add(button_13);
  
  button_14 = new JButton("0");
  button_14.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    textField.setText(textField.getText()+0);
   }
  });
  button_14.setBounds(286, 165, 50, 25);
  contentPane.add(button_14);
  
  btnClear = new JButton("AC");
  btnClear.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    number1 = 0;
    number2 = 0;
    result = 0;
    textField.setText(null);
    
   }
  });
  btnClear.setBounds(286, 134, 50, 25);
  contentPane.add(btnClear);
 }
}
突然发现自己没有写注释的习惯!!!!这很不好!!!一定要改!!!

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页