⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 简单的java入门编程
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 public class Calculator extends JFrame
{
  private BorderLayout layout;
  private Container pane;
  private JPanel northPanel,centerPanel,southPanel;
  private JTextField display;
  private JMenuBar menuBar;
  private JMenu fileMenu;
  private long x,y;
  private int tag;
  
    public Calculator()
  {
    super("计算器");
    initDisplay();
    initKeyBoard();
    initOthers();
    init();
    this.x=0;
    this.y=0;
    this.tag=1;
  }
  
 private void init()
  {
    Font f = new Font("宋体",Font.BOLD,32);
    layout = new BorderLayout();  // 创建边框布局管理对象
    
    JMenuItem item;  	
  	fileMenu = new JMenu("编辑");    
    item = new JMenuItem("复制"); 
    fileMenu.setFont(new Font("宋体",Font.BOLD,14));
    item.setFont(new Font("宋体",Font.BOLD,9));
    fileMenu.add(item); 
    menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    this.setJMenuBar(menuBar);
      
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pane = this.getContentPane();
    pane.setLayout(layout);       // 设置内容窗格的布局
      
    pane.add(northPanel,BorderLayout.NORTH);
    pane.add(centerPanel,BorderLayout.CENTER);
    pane.add(southPanel,BorderLayout.SOUTH);
    
    this.setSize(200,240);
    this.setVisible(true);
  }
  
 private void initDisplay(){
 	Font f = new Font("宋体",Font.BOLD,24); 	
 	northPanel=new JPanel();
    northPanel.setFont(f);
    display=new JTextField(15);
    display.setHorizontalAlignment(SwingConstants.RIGHT); 
    display.setText("0");
    northPanel.add(display,BorderLayout.CENTER);
 	}
 	
 private void initKeyBoard(){
 	String keyLabels = "123+456-789*0 =/";
 	Font f = new Font("Dialog",Font.BOLD,18);
 	centerPanel=new JPanel();
    centerPanel.setFont(f);
    centerPanel.setLayout(new GridLayout(4,4));
    for(int i=0;i<keyLabels.length();i++){
         String label = keyLabels.substring(i,i+1);
         JButton b = new JButton(label);
         b.setFont(f);
         if(i==13){b.setFont(new Font("宋体",Font.BOLD,8));b.setText("+/-");}
         if(i==3||i==7||i==11||i==13||i==14||i==15)b.setForeground(Color.red);
         b.setActionCommand(""+i);
         ActionListener listener=new ActionListKeyBoard();
         b.addActionListener(listener);
         centerPanel.add(b);
        }
 	}	
 	
 private void initOthers(){
 	Font f = new Font("宋体",Font.BOLD,24); 	
 	southPanel=new JPanel();
    southPanel.setFont(f);
    JButton b=new JButton("BackSpace");
    southPanel.add(b);
    b.setForeground(Color.red);
    b.setActionCommand("BackSpace");
    ActionListener listener=new ActionListOthers();
    b.addActionListener(listener);
    b=new JButton("Clear");
    b.setForeground(Color.red);
    b.setActionCommand("Clear");
    southPanel.add(b);
    b.addActionListener(listener);
 	}
 	
 private class ActionListKeyBoard implements ActionListener{
    	public void actionPerformed(ActionEvent e){
       		 switch(Integer.parseInt(e.getActionCommand())){
    		 	case 0:y=y*10+1;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 1:y=y*10+2;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 2:y=y*10+3;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 4:y=y*10+4;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 5:y=y*10+5;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 6:y=y*10+6;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 8:y=y*10+7;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 9:y=y*10+8;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 10:y=y*10+9;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 12:y=y*10+0;
    		 	       display.setText(""+y);
    		 	       break;
    		 	case 3:
    		 	       calculate();
    		 	       tag=1;    		 	       
    		 	       y=0;	
    		 	       break;
    		 	case 7:
    		 	       calculate();
    		 	       tag=2;    		 	       
    		 	       y=0;	
    		 	       break;
    		 	case 11:
    		 	       calculate();
                       tag=3;    		 	       
    		 	       y=0;	
    		 	       break;
    		 	case 15:
    		 	       calculate();
    		 	       tag=4;    		 	       
    		 	       y=0;	
    		 	       break;
    		 	 case 14:
    		 	       calculate();
    		 	       tag=5;    		 	       
    		 	       y=0;	
    		 	       break;   
    		 	 case 13:
                       y=0-y;
    		           display.setText(""+y);    		 
    		           break;	       
    		 	}
    		}
    	
    	private void calculate(){
    		    y=Long.parseLong(display.getText());
    		 	switch(tag){
    		 	       	case 1:x=x+y;break;
    		 	       	case 2:x=x-y;break;
    		 	       	case 3:x=x*y;break;
    		 	       	case 4:
    		 	       	       try{
    		 	       	       	 if(y==0)throw new divideException();
    		 	       	       	 else x=x/y;
    		 	       	       	}
    		 	       	       catch(divideException e){
    		 	       	       	JOptionPane.showMessageDialog(new JFrame(),"除以0错");
    		 	       	       	}
    		 	       	       break;
    		 	       	}
    		 	display.setText(""+x);
    		}	
       }
            
  class divideException extends Exception{
	public divideException(){
	     super();
	     
		}	
	}
   	
  private class ActionListOthers implements ActionListener{
  	public void actionPerformed(ActionEvent e){
  		String cmd;
  		cmd=e.getActionCommand();
  		if(cmd.equals("BackSpace")){
  		     y=y/10;
    		 display.setText(""+y);
  			}
  		if(cmd.equals("Clear")){
  			 x=0;
  		     y=0;
  		     tag=1;
    		 display.setText(""+y);
  			}
  		}
  	}  		
}

class tester
{
  public static void main(String[] args)
  {
    Calculator fdemo1 = new Calculator();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -