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

📄 exp.java

📁 简单的计算机程序。实现的功能比较简单。而且视线也比较简单。
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class exp extends JFrame
{
	static JTextField t=new JTextField("",20);
	public static void main(String args[])
	{
		exp c=new exp();
	}
	public exp()
	{
		setTitle("计算器");
		Container app=getContentPane();
		t.setHorizontalAlignment(JTextField.RIGHT);
		app.add(t,BorderLayout.NORTH);
		operationPanel op=new operationPanel();
		app.add(op,BorderLayout.CENTER);
		numberPanel np=new numberPanel();
		app.add(np,BorderLayout.SOUTH);
		app.validate();
		setResizable(true);
		pack();
		setVisible(true);
	}
}
class operationPanel extends JPanel
{
	public operationPanel()
	{
		setLayout(new FlowLayout());
		JButton b1=new JButton("cancel");
		JButton b2=new JButton("clear");
		b1.addActionListener(new ActionLis());
		b2.addActionListener(new ActionLis());
		add(b1);
		add(b2);
	}
}
class numberPanel extends JPanel
{
	String s[]={"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","="};
	JButton b;
	public numberPanel()
	{
		setLayout(new GridLayout(4,4));
		for(int i=0;i<=10;i++)
		{
			b=new JButton(s[i]);
			b.setActionCommand("number");
			b.addActionListener(new ActionLis());
			add(b);
		}
		for(int i=11;i<s.length-1;i++)
		{
			b=new JButton(s[i]);
			b.setActionCommand("caculate");
			b.addActionListener(new ActionLis());
			add(b);
		}
		b=new JButton("=");
		b.setActionCommand("result");
		b.addActionListener(new ActionLis());
		add(b);
	}
}
class ActionLis implements ActionListener
{
	static String ops;
	static boolean f=false;
	static double a,b,c;
	static String s,bText,text;
	public void actionPerformed(ActionEvent e)
	{
		s=e.getActionCommand();
		bText=((JButton)e.getSource()).getText();
		text=exp.t.getText();
		if(s=="number")
		{
			if(f)
			{
				exp.t.setText(bText);
				f=false;
			}
			else exp.t.setText(text+bText);
		}
		if(s=="caculate")
		{
			if(!text.equals(""))
			{
				ops=bText;
				a=Double.parseDouble(text);
				f=true;
			}
			else if(bText=="-")
			{
				exp.t.setText(bText);
			}
		}
		if(s=="result"&&!text.equals(""))
		{
			b=Double.parseDouble(text);
			if(ops=="+") c=a+b;
			if(ops=="-") c=a-b;
			if(ops=="*") c=a*b;
			if(ops=="/") c=a/b;
			exp.t.setText(String.valueOf(c));
			f=true;
		}
		if(s=="cancel")
		{
			exp.t.setText(text.substring(0,text.length()-1));
		}
		if(s=="clear")
		{
			exp.t.setText("");
			f=false;
		}
	}
}

⌨️ 快捷键说明

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