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

📄 calculator.java

📁 很完善的计算器哦
💻 JAVA
字号:
package calculator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame
{	

	private final int NUMBER_BUTTON = 46;
	
	private JPanel displayPanel; 
	private JPanel memoryPanel; 
	private JPanel formatPanel;
	private JPanel mainAreaPanel;	
	private JPanel informationPanel;
	private JPanel clockPanel;
	
	private JTextField display;
	private TextField memory;
	
	JButton b[] = new JButton[NUMBER_BUTTON];
		
	private JLabel author;
	private JLabel contact;
	
	private StringBuffer str1 = new StringBuffer();
	private StringBuffer str2 = new StringBuffer();
	
	private boolean newFlag = false;
	private boolean pointFlag = true;
	private boolean changeFlag = false;
	private boolean divZeroFlag = false;
	private boolean memoryFlag = false;
	private boolean memoryDisplayFlag = false;
	private boolean memoryPlusFlag = false;
	private boolean modZeroFlag = false;
	private boolean refreshFlag = false;
	
	private int doubleOperatorFlag = -1;
	private int singleOperatorFlag = -1;
	
	private double memoryValue = 0;

	public Calculator()
	{
		super("多功能计算器 v1.0 Beta");
		initDisplay();
		initMemory();
		initFormat();
		initMainArea();
		initInformation();
		Container con = getContentPane();
		displayPanel.setBounds(0, 0, 100, 10);	
		memoryPanel.setBounds(15, 32, 22, 20);
		formatPanel.setBounds(50, 33, 275, 30);
		mainAreaPanel.setBounds(10, 68, 316, 245);
		informationPanel.setBounds(15, 316, 300, 40);
		clockPanel.setBounds(130, 312, 200, 20);
				
		con.add(informationPanel);
		con.add(clockPanel);
		con.add(formatPanel);
		con.add(mainAreaPanel);		
		con.add(memoryPanel);		
		con.add(displayPanel);
		
		setLocation(200, 200);
		setSize(342,395);
		setResizable(false);
		setVisible(true);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent i)
			{
				System.exit(0);
			}
		});
	}
	
	private void initDisplay()
	{
		displayPanel = new JPanel();
		setDisplay(new JTextField("0",28));		
		getDisplay().setEditable(false);
		getDisplay().setBackground(Color.WHITE);
		getDisplay().setHorizontalAlignment(JTextField.RIGHT); 
		displayPanel.add(getDisplay());			
	}	
	
	private void initMemory()
	{
		memoryPanel = new JPanel();
		setMemory(new TextField(1));
		getMemory().setEditable(false);
		getMemory().setBackground(Color.WHITE);
		memoryPanel.add(getMemory());					
	}
	
	private void initFormat()
	{
		formatPanel = new JPanel();
		formatPanel.setLayout(new GridLayout(1,4,2,4));
		b[42] = new JButton(new ImageIcon(Resource.getImgResource("/images/about.gif")));
		b[43] = new JButton(new ImageIcon(Resource.getImgResource("/images/backspace.gif")));
		b[44] = new JButton(new ImageIcon(Resource.getImgResource("/images/ce.gif")));
		b[45] = new JButton(new ImageIcon(Resource.getImgResource("/images/c.gif")));	
		for(int i = 42; i < NUMBER_BUTTON; i++)
		{
			formatPanel.add(b[i]);
			b[i].addActionListener(new Listener(this));
		}
	}
	
	private void initMainArea()
	{
		mainAreaPanel = new JPanel();
		mainAreaPanel.setLayout(new GridLayout(7,6,2,2));
		
		b[0] = new JButton(new ImageIcon(Resource.getImgResource("/images/sin.gif")));
		b[1] = new JButton(new ImageIcon(Resource.getImgResource("/images/cos.gif")));
		b[2] = new JButton(new ImageIcon(Resource.getImgResource("/images/tan.gif")));
		b[3] = new JButton(new ImageIcon(Resource.getImgResource("/images/pi.gif")));
		b[4] = new JButton(new ImageIcon(Resource.getImgResource("/images/todegrees.gif")));
		b[5] = new JButton(new ImageIcon(Resource.getImgResource("/images/toradians.gif")));
		
		b[6] = new JButton(new ImageIcon(Resource.getImgResource("/images/square.gif")));	
		b[7] = new JButton(new ImageIcon(Resource.getImgResource("/images/pow.gif")));		
		b[8] = new JButton(new ImageIcon(Resource.getImgResource("/images/sqrt.gif")));
		b[9] = new JButton(new ImageIcon(Resource.getImgResource("/images/extract.gif")));
		b[10] = new JButton(new ImageIcon(Resource.getImgResource("/images/lg.gif")));
		b[11] = new JButton(new ImageIcon(Resource.getImgResource("/images/ln.gif")));		
		
		b[12] = new JButton(new ImageIcon(Resource.getImgResource("/images/e.gif")));
		b[13] = new JButton(new ImageIcon(Resource.getImgResource("/images/exp.gif")));
		b[14] = new JButton(new ImageIcon(Resource.getImgResource("/images/n!.gif")));
		b[15] = new JButton(new ImageIcon(Resource.getImgResource("/images/a.gif")));
		b[16] = new JButton(new ImageIcon(Resource.getImgResource("/images/cxy.gif")));
		b[17] = new JButton(new ImageIcon(Resource.getImgResource("/images/mod.gif")));
		
		b[18] = new JButton(new ImageIcon(Resource.getImgResource("/images/mc.gif")));
		b[19] = new JButton(new ImageIcon(Resource.getImgResource("/images/7.gif")));
		b[20] = new JButton(new ImageIcon(Resource.getImgResource("/images/8.gif")));
		b[21] = new JButton(new ImageIcon(Resource.getImgResource("/images/9.gif")));
		b[22] = new JButton(new ImageIcon(Resource.getImgResource("/images/div.gif")));
		b[23] = new JButton(new ImageIcon(Resource.getImgResource("/images/int.gif")));
		
		b[24] = new JButton(new ImageIcon(Resource.getImgResource("/images/mr.gif")));
		b[25] = new JButton(new ImageIcon(Resource.getImgResource("/images/4.gif")));
		b[26] = new JButton(new ImageIcon(Resource.getImgResource("/images/5.gif")));
		b[27] = new JButton(new ImageIcon(Resource.getImgResource("/images/6.gif")));
		b[28] = new JButton(new ImageIcon(Resource.getImgResource("/images/mul.gif")));
		b[29] = new JButton(new ImageIcon(Resource.getImgResource("/images/percent.gif")));
		
		b[30] = new JButton(new ImageIcon(Resource.getImgResource("/images/ms.gif")));
		b[31] = new JButton(new ImageIcon(Resource.getImgResource("/images/1.gif")));		
		b[32] = new JButton(new ImageIcon(Resource.getImgResource("/images/2.gif")));
		b[33] = new JButton(new ImageIcon(Resource.getImgResource("/images/3.gif")));
		b[34] = new JButton(new ImageIcon(Resource.getImgResource("/images/sub.gif")));
		b[35] = new JButton(new ImageIcon(Resource.getImgResource("/images/countbackwards.gif")));
		
		b[36] = new JButton(new ImageIcon(Resource.getImgResource("/images/mplus.gif")));
		b[37] = new JButton(new ImageIcon(Resource.getImgResource("/images/0.gif")));		
		b[38] = new JButton(new ImageIcon(Resource.getImgResource("/images/change.gif")));
		b[39] = new JButton(new ImageIcon(Resource.getImgResource("/images/point.gif")));
		b[40] = new JButton(new ImageIcon(Resource.getImgResource("/images/plus.gif")));
		b[41] = new JButton(new ImageIcon(Resource.getImgResource("/images/result.gif")));
		for(int i = 0; i < NUMBER_BUTTON - 4; i++)
		{
			mainAreaPanel.add(b[i]);
			b[i].addActionListener(new Listener(this));
		}		
	}
	
	private void initInformation()
	{		
		informationPanel = new JPanel();
		clockPanel = new JPanel();
		ClockLabel clock = new ClockLabel();
		author = new JLabel("作者 : meteor ");	
		clockPanel.add(clock);
		contact = new JLabel("E-mail : yehaibin57@163.com              QQ : 170095942");
		informationPanel.setLayout(new GridLayout(2,1));
		informationPanel.add(author);
		informationPanel.add(contact);
	}
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g1 = (Graphics2D) g;
		g1.setColor(Color.white);
	}
	
	public void setStr1(StringBuffer str1)
	{
		this.str1 = str1;
	}
	
	public StringBuffer getStr1()
	{
		return str1;
	}
	
	public void setStr2(StringBuffer str2)
	{
		this.str2 = str2;
	}
	
	public StringBuffer getStr2()
	{
		return str2;
	}	

	public void setDisplay(JTextField display)
	{
		this.display = display;
	}

	public JTextField getDisplay()
	{
		return display;
	}

	public void setNewFlag(boolean newFlag)
	{
		this.newFlag = newFlag;
	}

	public boolean getNewFlag()
	{
		return newFlag;
	}

	public void setPointFlag(boolean pointFlag)
	{
		this.pointFlag = pointFlag;
	}

	public boolean getPointFlag()
	{
		return pointFlag;
	}

	public void setDoubleOperatorFlag(int doubleOperatorFlag)
	{
		this.doubleOperatorFlag = doubleOperatorFlag;
	}

	public int getDoubleOperatorFlag()
	{
		return doubleOperatorFlag;
	}

	public void setChangeFlag(boolean changeFlag)
	{
		this.changeFlag = changeFlag;
	}

	public boolean getChangeFlag()
	{
		return changeFlag;
	}

	public void setMemoryDisplayFlag(boolean memoryDisplayFlag)
	{
		this.memoryDisplayFlag = memoryDisplayFlag;
	}

	public boolean getMemoryDisplayFlag()
	{
		return memoryDisplayFlag;
	}

	public void setMemoryPlusFlag(boolean memoryPlusFlag)
	{
		this.memoryPlusFlag = memoryPlusFlag;
	}

	public boolean getMemoryPlusFlag()
	{
		return memoryPlusFlag;
	}

	public void setRefreshFlag(boolean refreshFlag)
	{
		this.refreshFlag = refreshFlag;
	}

	public boolean getRefreshFlag()
	{
		return refreshFlag;
	}

	public void setDivZeroFlag(boolean divZeroFlag)
	{
		this.divZeroFlag = divZeroFlag;
	}

	public boolean getDivZeroFlag()
	{
		return divZeroFlag;
	}

	public void setSingleOperatorFlag(int singleOperatorFlag)
	{
		this.singleOperatorFlag = singleOperatorFlag;
	}

	public int getSingleOperatorFlag()
	{
		return singleOperatorFlag;
	}

	public void setMemory(TextField memory)
	{
		this.memory = memory;
	}

	public TextField getMemory()
	{
		return memory;
	}

	public void setMemoryValue(double memoryValue)
	{
		this.memoryValue = memoryValue;
	}

	public double getMemoryValue()
	{
		return memoryValue;
	}

	public void setMemoryFlag(boolean memoryFlag)
	{
		this.memoryFlag = memoryFlag;
	}

	public boolean getMemoryFlag()
	{
		return memoryFlag;
	}

	public void setModZeroFlag(boolean modZeroFlag)
	{
		this.modZeroFlag = modZeroFlag;
	}

	public boolean getModZeroFlag()
	{
		return modZeroFlag;
	}
	
	public static void main(String[] args)
	{
		new Calculator();	
	}
	
}


	

⌨️ 快捷键说明

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