Chinaunix首页 | 论坛 | 博客
  • 博客访问: 396631
  • 博文数量: 70
  • 博客积分: 1919
  • 博客等级: 上尉
  • 技术积分: 1179
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 20:05
文章分类

全部博文(70)

文章存档

2014年(2)

2013年(29)

2012年(20)

2011年(1)

2010年(13)

2009年(5)

分类: Java

2010-03-27 13:07:26

这是一个特别简单的计算器程序,对于JAVA的初学者十分有用,可以做这个小项目

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class small_Caculator {
 JFrame frame_Main;
 JLabel jLabel_topic;
 JTextField jTextField_data;
 JButton jButton_add;
 JButton jButton_minus;
 JButton jButton_multiply;
 JButton jButton_divide;

 JButton ob0,ob1,ob2,ob3,ob4,ob5,ob6,ob7,ob8,ob9;
 JButton jButton_equal;
 JButton jButton_clear;
 JLabel jLabel_owner;
 
 Container content;
 
 public small_Caculator(){
  
  new start_windows();
  Init_screen();
  new ActionHandle();
 }
 
 
 //加载窗口程序设计 
 public class start_windows extends JWindow implements ActionListener{
  JFrame startframe_Main;
  //JLabel jLabel_image;
  JLabel jLabel_begin;
  JProgressBar jProgressBar_line;
  JLabel jLabel_timer;
  Timer timer;
  JButton jButton_image;
  int n=100;
  Container content;
  JPanel jp1;
 
  public start_windows(){
   startframe_Main=new JFrame("计算器加载程序");
   content=startframe_Main.getContentPane();
   startframe_Main.setLayout(null);
   jLabel_begin=new JLabel("BEGINING....",JLabel.CENTER);
   jLabel_timer=new JLabel();
   jLabel_timer.setBounds(4540/15,5060/15,375/15,255/15);
   content.add(jLabel_timer);
   
   jLabel_begin.setBounds(840/14,4440/15,2295/15,255/15);
   jLabel_begin.setFont(new Font("宋体",Font.PLAIN,18));
   content.add(jLabel_begin);
   timer=new Timer(100,this);
   timer.addActionListener(this);
   jProgressBar_line=new JProgressBar(1,100);
   jProgressBar_line.setBounds(540/15,5040/15,3735/15,355/15);
   jProgressBar_line.setStringPainted(true);
   jProgressBar_line.setString("正在加载程序....");
   content.add(jProgressBar_line);
   
   
   //Icon image=new ImageIcon("xujinglei.jpg");
   jButton_image=new JButton("图像标签",new ImageIcon("magictiger.jpg"));
   
   
   jButton_image.setBounds(1410/15,600/15,2655/15,3255/15);
   content.add(jButton_image);
   
   timer.start();
   
   startframe_Main.setVisible(true);
   startframe_Main.setSize(5505/15,6900/15);
   startframe_Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
   startframe_Main.setLocation((screenSize.width-startframe_Main.getWidth())/2,
      (screenSize.height-startframe_Main.getHeight())/2);
  }
  
  public void actionPerformed(ActionEvent e){
   if(--n>0){
    jLabel_timer.setText(Integer.toString(100-n)+"%");
    jProgressBar_line.setValue(100-n);
    
   }
   else{
    timer.stop();
    startframe_Main.dispose();
    frame_Main.setVisible(true);
   }
  }
 }

//初始化窗口 
 public void Init_screen(){
  frame_Main=new JFrame("简单计算器应用程序");
  frame_Main.setLayout(null);
  content=frame_Main.getContentPane();
  
  jLabel_topic=new JLabel("简单计算器");
  jLabel_topic.setFont(new Font("楷体",Font.PLAIN,25));
  jLabel_topic.setForeground(Color.BLUE);
  jLabel_topic.setBounds(1545/15,0,2175/15,375/15);
  content.add(jLabel_topic);
  
  jTextField_data=new JTextField("");
  jTextField_data.setEnabled(false);
  jTextField_data.setBounds(129/15,840/15,4695,495/15);
  jTextField_data.setBackground(Color.GRAY);
  content.add(jTextField_data);
  
  ob0=new JButton("0");
  ob1=new JButton("1");
  ob2=new JButton("2");
  ob3=new JButton("3");
  ob4=new JButton("4");
  ob5=new JButton("5");
  ob6=new JButton("6");
  ob7=new JButton("7");
  ob8=new JButton("8");
  ob9=new JButton("9");
  ob0.setBounds(480/15, 1800/15, 615/15, 615/15);
  ob1.setBounds(480/15, 2790/15, 615/15, 615/15);
  ob2.setBounds(1560/15, 2790/15, 615/15, 615/15);
  ob3.setBounds(2640/15, 2790/15, 615/15, 615/15);
  ob4.setBounds(480/15, 3780/15, 615/15, 615/15);
  ob5.setBounds(1560/15, 3780/15, 615/15, 615/15);
  ob6.setBounds(2640/15, 3780/15, 615/15, 615/15);
  ob7.setBounds(480/15, 4920/15, 615/15, 615/15);
  ob8.setBounds(1560/15, 4920/15, 615/15, 615/15);
  ob9.setBounds(2640/15, 4920/15, 615/15, 615/15);
  
  content.add(ob0);
  content.add(ob1);
  content.add(ob2);
  content.add(ob3);
  content.add(ob4);
  content.add(ob5);
  content.add(ob6);
  content.add(ob7);
  content.add(ob8);
  content.add(ob9);
  
  jButton_add=new JButton("+");
  jButton_add.setBounds(1560/15,1800/15, 615/15, 615/15);
  jButton_minus=new JButton("-");
  jButton_minus.setBounds(2640/15,1800/15,615/15, 615/15);
  jButton_multiply=new JButton("*");
  jButton_multiply.setBounds(3720/15,2790/15,615/15, 615/15);
  jButton_divide=new JButton("/");
  jButton_divide.setBounds(3720/15,3780/15,615/15, 615/15);
  
  content.add(jButton_add);
  content.add(jButton_minus);
  content.add(jButton_multiply);
  content.add(jButton_divide);
  
  jButton_equal=new JButton("=");
  jButton_equal.setBounds(3720/15,4920/15,615/15, 615/15);
  jButton_clear=new JButton("C");
  jButton_clear.setFont(new Font("楷体",Font.PLAIN,12));
  jButton_clear.setBounds(3720/15,1800/15,615/15, 615/15);
  content.add(jButton_equal);
  content.add(jButton_clear);
  
  jLabel_owner=new JLabel("版权声明   ");
  jLabel_owner.setBounds(840/15,5880/15,3135/15,255/15);
  jLabel_owner.setForeground(Color.gray);
  content.add(jLabel_owner);
  
  frame_Main.setVisible(false);
  frame_Main.setSize(5505/15,6900/15);
  frame_Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
  frame_Main.setLocation((screenSize.width-frame_Main.getWidth())/2,
    (screenSize.height-frame_Main.getHeight())/2);
 }
 public class ActionHandle implements ActionListener{
  public ActionHandle(){
   ob0.addActionListener(this);
   ob1.addActionListener(this);
   ob2.addActionListener(this);
   ob3.addActionListener(this);
   ob4.addActionListener(this);
   ob5.addActionListener(this);
   ob6.addActionListener(this);
   ob7.addActionListener(this);
   ob8.addActionListener(this);
   ob9.addActionListener(this);
   jButton_clear.addActionListener(this);
   jButton_add.addActionListener(this);
   jButton_minus.addActionListener(this);
   jButton_multiply.addActionListener(this);
   jButton_divide.addActionListener(this);
   jButton_equal.addActionListener(this);
  }
  public void actionPerformed(ActionEvent e){
   
   if(e.getSource().equals(ob0))
   {
    jTextField_data.setText(jTextField_data.getText()+"0");
   }
   if(e.getSource().equals(ob1))
   {
    jTextField_data.setText(jTextField_data.getText()+"1");
   }
   if(e.getSource().equals(ob2))
   {
    jTextField_data.setText(jTextField_data.getText()+"2");
   }
   if(e.getSource().equals(ob3))
   {
    jTextField_data.setText(jTextField_data.getText()+"3");
   }
   if(e.getSource().equals(ob4))
   {
    jTextField_data.setText(jTextField_data.getText()+"4");
   }
   if(e.getSource().equals(ob5))
   {
    jTextField_data.setText(jTextField_data.getText()+"5");
   }
   if(e.getSource().equals(ob6))
   {
    jTextField_data.setText(jTextField_data.getText()+"6");
   }
   if(e.getSource().equals(ob7))
   {
    jTextField_data.setText(jTextField_data.getText()+"7");
   }
   if(e.getSource().equals(ob8))
   {
    jTextField_data.setText(jTextField_data.getText()+"8");
   }
   if(e.getSource().equals(ob9))
   {
    jTextField_data.setText(jTextField_data.getText()+"9");
   }
   if(e.getSource().equals(jButton_clear))
   {
    
    jTextField_data.setText(null);
    
   }
   
   if(e.getSource().equals(jButton_add))
   {
    
    jTextField_data.setText(jTextField_data.getText()+"+");
   }
   if(e.getSource().equals(jButton_minus))
   {
    
    jTextField_data.setText(jTextField_data.getText()+"-");
   }
   if(e.getSource().equals(jButton_multiply))
   {
    
    jTextField_data.setText(jTextField_data.getText()+"*");
   }
   if(e.getSource().equals(jButton_divide))
   {
    
    jTextField_data.setText(jTextField_data.getText()+"/");
   }
   String mess;
   if(e.getSource().equals(jButton_equal))
   {
   
    if(jTextField_data.getText().contains("+"))
    {
     int t;
     double s;
     try{
      mess=jTextField_data.getText();
      t=mess.lastIndexOf("+");
      if(t==mess.length()-1){
       JOptionPane.showMessageDialog(frame_Main,"请在运算符后面继续输入!");
      }
      else{
       s=(double)Double.parseDouble(mess.substring(0,t))+(double)Double.parseDouble(mess.substring(t+1, mess.length()));
       jTextField_data.setText(String.valueOf(s));
      }
     }catch(NumberFormatException o1){
      jTextField_data.setText(null);
      JOptionPane.showMessageDialog(frame_Main,"输入数据不合法");
     }
    }
    if(jTextField_data.getText().contains("-"))
    {
     int t;
     double s;
     try{
      mess=jTextField_data.getText();
      t=mess.lastIndexOf("-");
      if(t==mess.length()-1){
       JOptionPane.showMessageDialog(frame_Main,"请在运算符后面继续输入!");
      }
      else{
       s=(double)Double.parseDouble(mess.substring(0,t))-(double)Double.parseDouble(mess.substring(t+1,mess.length()));
       jTextField_data.setText(String.valueOf(s));
      }
     }catch(NumberFormatException o1){
      jTextField_data.setText(null);
      JOptionPane.showMessageDialog(frame_Main,"输入数据不合法");
     }
    } 
    if(jTextField_data.getText().contains("*"))
    {
     int t;
     double s;
     try{
      mess=jTextField_data.getText();
      t=mess.lastIndexOf("*");
      if(t==mess.length()-1){
       JOptionPane.showMessageDialog(frame_Main,"请在运算符后面继续输入!");
      }
      else{
       s=(double)Double.parseDouble(mess.substring(0,t))*(double)Double.parseDouble(mess.substring(t+1,mess.length()));
       jTextField_data.setText(String.valueOf(s));
      }
     }catch(NumberFormatException o1){
      jTextField_data.setText(null);
      JOptionPane.showMessageDialog(frame_Main,"输入数据不合法");
     }
    }
    if(jTextField_data.getText().contains("/"))
    {
     int t;
     double s;
     try{
      mess=jTextField_data.getText();
      t=mess.lastIndexOf("/");
      if(t==mess.length()-1){
       JOptionPane.showMessageDialog(frame_Main,"请在运算符后面继续输入!");
      }
      else{
       if((double)Double.parseDouble(mess.substring(t+1,mess.length()))==0){
        jTextField_data.setText(null);
        JOptionPane.showMessageDialog(frame_Main,"除数不能为零");
       }
       else{
        s=(double)Double.parseDouble(mess.substring(0,t))/(double)Double.parseDouble(mess.substring(t+1,mess.length()));
        jTextField_data.setText(String.valueOf(s));
       }
      }
     }catch(NumberFormatException onum){
      jTextField_data.setText(null);
      JOptionPane.showMessageDialog(frame_Main,"输入数据不合法"); 
     }finally{
      
     }
    }
   }
  }
 }
 
 public static void main(String[] args){
  
  new small_Caculator();
 }
}

阅读(1603) | 评论(0) | 转发(0) |
0

上一篇:错误

下一篇:java小项目计算器

给主人留下些什么吧!~~