计算器及事件监听

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
public class 个人信息{
    public static void main(String arg[])
    {
        Frame f=new Frame("个人信息");
        f.setSize(250,300);
        f.setLocation(300,300);
        f.setBackground(Color.lightGray);
        f.setLayout(new FlowLayout(2));
        f.add(new JLabel("姓名:"));
        f.add(new TextField("葛远斌",20));
        f.add(new JLabel("班级:"));
        f.add(new TextField("计算机科学与技术16(D)",20));
        f.add(new JLabel("学号:"));
        f.add(new TextField("20163311121",20));
        f.add(new JLabel("性别:"));
        f.add(new TextField("男",20));
        JButton button1=new JButton("OK");
        JButton button2=new JButton("CLOSE");
        f.add(button1);
        f.add(button2);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {        //OK按钮事件
                JOptionPane.showMessageDialog(null,"你快乐吗?","提示",JOptionPane.PLAIN_MESSAGE) ;    
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {        //CLOSE按钮事件
                long d=e.getWhen();  //事件发生的时间
                Date date=new Date(d);   //转换为相应的时间
                System.out.println(date);
                System.exit(0);
            }
        });
        f.setVisible(true);
    }
}

package 图形界面;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
 
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel; 


import javax.swing.JTextField;

public class 计算器 {
      public static class Count extends JApplet implements ActionListener
      {
          private static final long serialVersionUID = 1L;
          private JTextField textField = new JTextField("开始输入数字"); //单行文本编辑区
          String operator = "";//操作
          String input = "";//输入的 式子
          boolean flag =  true;
      //  boolean flag1 = true;
      //  boolean flag2 = true;
          public void init()//覆写Applet里边的init方法
          {
              Container C = getContentPane();
              JButton b[] = new JButton[16];
              JPanel panel = new JPanel();
              C.add(textField, BorderLayout.NORTH);
              C.add(panel,BorderLayout.CENTER);
              panel.setLayout(new GridLayout(4, 4,5,5));
              String name[]={"1","2","3","+","4","5","6","-","7","8","9","*","0","C","=","/"};//设置 按钮
              for(int i=0;i<16;i++)//添加按钮
              {
                  b[i] = new JButton(name[i]);
                  b[i].setBackground(new Color(0,0,0));  //设置背景颜色为黑色
                  b[i].setForeground(Color.pink);//数字键 设置为粉色
                  if(i%4==3)
                      b[i].setForeground(Color.yellow); //设置旁边运算符的颜色
                  b[i].setFont(new Font("楷书",Font.PLAIN,25));//设置字体格式
                  panel.add(b[i]);
                  b[i].addActionListener(this); //监听事件
              }
              b[13].setForeground(Color.blue);//非数字键,即运算键设置为红颜色
          }
          public void actionPerformed(ActionEvent e)  //动作事件处理方法 实现 ActionListener接口
          {
              int cnt = 0;
              String actionCommand = e.getActionCommand();  //从键盘获得这个数
              if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))
                  input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子
              else if(actionCommand.equals("C"))
                  input = "";
              else if(actionCommand.equals("="))//当监听到等号时,则处理 input
              {
                  input+= "="+compute(input);
                  textField.setText(input);//显示对应的字符
                  input="";
                  cnt = 1;
              }
              else
                  input += actionCommand;//数字为了避免多位数的输入 不需要加空格
              if(cnt==0)  //没有这句不能输出结果
              textField.setText(input);  //得到数字
          }
          private String compute(String input)//即1237 的 样例
          {
              String str[];
              str = input.split(" ");
              Stack<Double> s = new Stack<Double>();
              double m = Double.parseDouble(str[0]);
              s.push(m);
              for(int i=1;i<str.length;i++)
              {
                  if(i%2==1)   
                  {   
                      if(str[i].compareTo("+")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          s.push(help);   
                      }   
                         
                      if(str[i].compareTo("-")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          s.push(-help);   
                      }   
                         
                      if(str[i].compareTo("*")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          double ans = s.peek();//取出栈顶元素   
                          s.pop();//消栈   
                          ans*=help;   
                          s.push(ans);   
                      }   
                         
                      if(str[i].compareTo("/")==0)   
                      {   
                          double help = Double.parseDouble(str[i+1]);   
                          double ans = s.peek();   
                          s.pop();   
                          ans/=help;   
                          s.push(ans);   
                      }   
                  }   
              }   
              double ans = 0d;   
              while(!s.isEmpty())   
              {   
                  ans+=s.peek();   
                  s.pop();   
              }   
              String result = String.valueOf(ans);
              return result;
          }
          public static void main(String args[])
          {
              JFrame frame = new JFrame("计算器");
              Count applet = new Count();
              frame.getContentPane().add(applet, BorderLayout.CENTER);
              applet.init();//applet的init方法
              applet.start();//线程开始
              frame.setSize(350, 350);//设置窗口大小
              frame.setVisible(true);//设置窗口可见
          }
       
      }
    
}

猜你喜欢

转载自www.cnblogs.com/maxue/p/9231638.html