java Swing计算器

Posted Felx_chu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Swing计算器相关的知识,希望对你有一定的参考价值。

作业要求,实现一个计算器,写的还不完善,仅供参考。

package AWTCalc;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Calc extends JFrame implements ActionListener 
	JMenuBar mb;
	JMenu jm1,jm2;
	JMenuItem mi11,mi12,mi13,mi21,mi22,mi23;	
	JTextField field;
	JButton[] b=new JButton[20];
	String[] s = new String[]"%","^","CE","←","7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+";//声明button上面的label
	 
	public Calc(int x,int y ,int w, int h)
		createMenu();
		createPanel();		
		setLocation(x,y);		
	    setSize(w,h);	    
	    setVisible(true);	    
	    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	
	    validate();
	

	 void createMenu() 			 //创建menu
		 mb = new JMenuBar();
		 jm1 = new JMenu("Edit");
		 jm2 = new JMenu("look");
		 mi11 = new JMenuItem("copy");
		 mi12 = new JMenuItem("paste");
		 mi13 = new JMenuItem("close");
		 mi21 = new JMenuItem("standerd");
		 mi22 = new JMenuItem("scientific");
		 mi23 = new JMenuItem("math");
		 
		 jm1.add(mi11);
		 jm1.add(mi12);
		 jm1.add(mi13);		 
		 jm2.add(mi21);
		 jm2.add(mi22);
		 jm2.addSeparator();   //在菜单之间增加分隔线
		 jm2.add(mi23);
		 
		 mb.add(jm1);
		 mb.add(jm2);
		 setJMenuBar(mb);
		
	
	 
	 
	 public void createPanel()		//创建menu下面的按钮面板

		 //JButtonListener  jbl=new JButtonListener(this);//当前窗口做监视器
		 
		 Container container = this.getContentPane();
		 JPanel jpanel1 = new JPanel();		 
		 jpanel1.setLayout(new FlowLayout());	
		 field=new JTextField(25);
		 field.setPreferredSize(new Dimension(500, 40));//添加文本域
		 field.addActionListener(this);
		 jpanel1.add(field);	
		 
		 JPanel jpanel2 = new JPanel();
		 jpanel2.setLayout(new GridLayout(5,4));
		 Font f=new Font("",Font.BOLD,36);
		 
		for(int i =0;i < 20 ; i++)					
			b[i] = new JButton(s[i]);				
			b[i].addActionListener(this);
			jpanel2.add(b[i]);//把b[i]添加到frame对象		
		
		 
		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		container.add(jpanel1);
		container.add(jpanel2);
	 
	

	 public void actionPerformed(ActionEvent e) 		
		 
		 Object target = e.getSource();
		 
		 String label = e.getActionCommand();
			
		 if ("0123456789.".indexOf(label) > 0)
	           handleNumber(label);
	     else
	           handleOperator(label);
	     


	 
	 boolean isFirstDigit = true;
	    /**
	     * Number handling.
	     * @param key the key of the button.
	     */
	    public void handleNumber(String key) 
	       if (isFirstDigit)
	           field.setText(key);
	       else if ((key.equals(".")) && (field.getText().indexOf(".") < 0))
	           field.setText(field.getText() + ".");
	       else if (!key.equals("."))
	           field.setText(field.getText() + key);
	       isFirstDigit = false;
	    
	   

	    double number = 0.0;
	    String operator = "=";
	   
	  void handleOperator(String key) 
	       if (operator.equals("+"))
	           number += Double.valueOf(field.getText());
	       else if (operator.equals("-"))
	           number -= Double.valueOf(field.getText());
	       else if (operator.equals("*"))
	           number *= Double.valueOf(field.getText());
	       else if (operator.equals("/"))
	           number /= Double.valueOf(field.getText());
	       else if (operator.equals("="))
	           number = Double.valueOf(field.getText());
		
	       field.setText(String.valueOf(number));
	       operator = key;
	       isFirstDigit = true;
	    
	   
	 
		 public static void main(String[] args) 				//main方法
				
				Calc c =new Calc(150,150,300,250);
			
		
		 	
	 
	


以上是关于java Swing计算器的主要内容,如果未能解决你的问题,请参考以下文章

java swing计算器布局

教你用java编写计算器

java中学习了swing可以做一个啥项目

基于Java Swing编写的简易运费计算工具

基于Java Swing编写的简易运费计算工具

基于Java Swing编写的简易运费计算工具