码迷,mamicode.com
首页 > 其他好文 > 详细

个人界面 计算器

时间:2018-06-21 23:38:44      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:val   sed   ack   compareto   显示   final   ast   操作   init   

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 Add {
 
 
 
   
  
   
  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.red); //设置旁边运算符的颜色
              b[i].setFont(new Font("隶书",Font.PLAIN,35));//设置字体格式 
              panel.add(b[i]); 
              b[i].addActionListener(this); //监听事件
          } 
          b[13].setForeground(Color.pink);//非数字键,即运算键设置为红颜色 
      } 
      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"); 
          Count applet = new Count(); 
          frame.getContentPane().add(applet, BorderLayout.CENTER); 
          applet.init();//applet的init方法 
          applet.start();//线程开始 
          frame.setSize(400, 400);//设置窗口大小 
          frame.setVisible(true);//设置窗口可见 
      } 
   
  } 
 }
 技术分享图片技术分享图片

 

技术分享图片技术分享图片

 

 

 

package 个人界面;
import java.awt.*;
//import java.awt.event.ActionEvent;
//import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener;
//import java.awt.Button;
//import java.awt.Color;
//import java.awt.FlowLayout;
//import java.awt.Frame;
//import java.awt.Label;
//import java.awt.TextField;
import java.awt.event.*;
public class Person {
 
 
 
 
   public static void main(String[] args)
 {
  Frame f1=new Frame("我的信息");
  f1.setSize(300,200);
  f1.setLocation(300, 240);
  f1.setBackground(Color.blue);
  f1.setLayout(new FlowLayout());
  
  f1.add(new Label("姓名"));
  f1.add(new TextField("infinite",25));
  f1.add(new Label("学号"));
  f1.add(new TextField("13456789",25));
  f1.add(new Label("性别"));
  f1.add(new TextField("女",25));
  f1.add(new Label("政治面貌"));
  f1.add(new TextField("团员",25));
  Button button1=new Button("ok");
  button1.addActionListener(new abc());
  f1.add( button1,BorderLayout.SOUTH);
  Button button2=new Button("EXIT");
  f1.add(button2,BorderLayout.EAST);
  button2.addActionListener(new abc());
  
  //f1.addWindowListener(new close());
  f1.setVisible(true);
  
 }
 }
 
 
package 个人界面;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
 
 public class abc extends Person implements ActionListener
 {
    
  @Override
  public void actionPerformed(ActionEvent ev) {
   // TODO Auto-generated method stub
   
         System.out.println("欢迎使用!");
  }
 
    
  
 

}
 

技术分享图片

 

 

 

 

个人界面 计算器

标签:val   sed   ack   compareto   显示   final   ast   操作   init   

原文地址:https://www.cnblogs.com/infinite14/p/9211288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!