public
MyCalculator()
{
f
=
new
JFrame("计算器ByMdou");
Container
contentPane
=
fgetContentPane();
/菜单的创建开始/
JMenuBar
mBar
=
new
JMenuBar();
mBarsetOpaque(true);
mEdit
=
new
JMenu("编辑(E)");
mEditsetMnemonic(KeyEventVK_E);
mCopy
=
new
JMenuItem("复制(C)");
mEditadd(mCopy);
mPaste
=
new
JMenuItem("粘贴(P)");
mEditadd(mPaste);
mView
=
new
JMenu("查看(V)");
mViewsetMnemonic(KeyEventVK_V);
mViewadd(new
JMenuItem("标准型"));
mViewadd(new
JMenuItem("科学型"));
mViewaddSeparator();
mViewadd(new
JMenuItem("查看分组"));
mHelp
=
new
JMenu("帮助(H)");
mHelpsetMnemonic(KeyEventVK_H);
mHelpadd(new
JMenuItem("帮助主题"));
mHelpaddSeparator();
mHelpadd(new
JMenuItem("关于计算器"));
mBaradd(mEdit);
mBaradd(mView);
mBaradd(mHelp);
fsetJMenuBar(mBar);
contentPanesetLayout(new
BorderLayout
());
JPanel
pTop
=
new
JPanel();
tResult
=
new
JTextField("0",26);
tResultsetHorizontalAlignment(JTextFieldRIGHT
);
tResultsetEditable(false);
pTopadd(tResult);
contentPaneadd(pTop,BorderLayoutNORTH);
JPanel
pBottom
=
new
JPanel();
pBottomsetLayout(new
BorderLayout());
JPanel
pLeft
=
new
JPanel();
pLeftsetLayout(new
GridLayout(5,1,3,3));
bM
=
new
JButton("
");
bMsetEnabled(false);
pLeftadd(bM);
bOther
=
new
JButton("MC");
bOtheraddActionListener(this);
bOthersetForeground(ColorRED);
bOthersetMargin(new
Insets(3,2,3,2));
pLeftadd(bOther);
bOther
=
new
JButton("MR");
bOtheraddActionListener(this);
bOthersetForeground(ColorRED);
bOthersetMargin(new
Insets(3,2,3,2));
pLeftadd(bOther);
bOther
=
new
JButton("MS");
bOtheraddActionListener(this);
bOthersetForeground(ColorRED);
bOthersetMargin(new
Insets(3,2,3,2));
pLeftadd(bOther);
bOther
=
new
JButton("M+");
bOtheraddActionListener(this);
bOthersetForeground(ColorRED);
bOthersetMargin(new
Insets(3,2,3,2));
pLeftadd(bOther);
import javaxswing;
import javaawt;
import javaawtevent;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton(""),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton(""),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 00; // 计算的中间结果
private boolean operateValidFlag = true; //判断 *** 作是否合法
public Calculator()
{
super("Calculator");
thissetBounds(300, 300, 300, 300);
thissetResizable(false);
thissetBackground(Colororange);
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thisgetContentPane()setLayout(new BorderLayout());//设置布局
text_double=new JTextField("0",20);//设置文本区
text_doublesetHorizontalAlignment(JTextFieldRIGHT);//设置水平对齐方式未右对齐
thisgetContentPane()add(text_double,BorderLayoutNORTH);//将文本区添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
thisgetContentPane()add(panel);//添加panel面板
for(int i=0;i<buttonlength;i++)//在面板上添加按钮
paneladd(button[i]);
for(int i=0;i<buttonlength;i++)
button[i]addActionListener(this);//为按钮注册
text_doublesetEditable(false);//文本框不可编辑
text_doubleaddActionListener(this);//
thissetVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= egetActionCommand();//返回与此动作相关的命令字符串。
Systemoutprintln("##########command is "+c);
if(cequals("C")){
handleC(); //用户按了“C”键
}
else if (cequals("CE")) // 用户按了"CE"键
{
text_doublesetText("0");
}
else if ("0123456789"indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_doublesetText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_doublesetText(button);
} else if ((buttonequals("")) && (text_doublegetText()indexOf("") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_doublesetText(text_doublegetText() + "");
} else if (!buttonequals(""))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_doublesetText(text_doublegetText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {
if (operatorequals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 00){
// *** 作不合法
operateValidFlag = false;
text_doublesetText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operatorequals("+")){
// 加法运算
resultNum += getNumberFromText();
} else if (operatorequals("-")){
// 减法运算
resultNum -= getNumberFromText();
} else if (operatorequals("")){
// 乘法运算
resultNum = getNumberFromText();
} else if (operatorequals("sqrt")) {
// 平方根运算
if(getNumberFromText()<0){
operateValidFlag = false;
text_doublesetText("被开方数不能为负数");}
else
resultNum = Mathsqrt(resultNum);
}
else if (operatorequals("+/-")){
// 正数负数运算
resultNum = resultNum (-1);
} else if (operatorequals("=")){
// 赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
// 双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
text_doublesetText(StringvalueOf(t1));
} else {
text_doublesetText(StringvalueOf(resultNum));
}
}
operator = button; //运算符等于用户按的按钮
firstDigit = true;
operateValidFlag = true;
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0;
try {
result = DoublevalueOf(text_doublegetText())doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result;
}
public static void main(final String[] args) {
new Calculator();
}
}
import javautilregex;
public class Calculation {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
if (argslength != 3) {
Systemoutprintln("MyCal need 3 parameters the formate is:");
Systemoutprintln("MyCal num1 operate num2:");
return;
}
if (!isNumeric(args[0]) || !isNumeric(args[2])) {
Systemoutprintln("MyCal need 3 parameters the formate is:");
Systemoutprintln("MyCal num1 operate num2");
Systemoutprintln("num1 and num2 must be number:");
return;
}
int num1 = IntegerparseInt(args[0]);
int num2 = IntegerparseInt(args[2]);
if (args[1]length() == 1) {
switch (args[1]charAt(0)) {
case '+':
Systemoutprintln(num1 + " + " + num2 + " = " + (num1 + num2));
return;
case '-':
Systemoutprintln(num1 + " - " + num2 + " = " + (num1 - num2));
return;
case 'x':
Systemoutprintln(num1 + " x " + num2 + " = " + (num1 num2));
return;
case '/':
if (num2 == 0) {
Systemoutprintln("can't / 0");
return;
}
Systemoutprintln(num1 + " / " + num2 + " = " + (num1 / num2));
return;
}
}
Systemoutprintln("MyCal need 3 parameters the formate is:");
Systemoutprintln("MyCal num1 operate num2");
Systemoutprintln("operate be (+, -, x, /)");
return;
}
public static boolean isNumeric(String str) {
Pattern pattern = Patterncompile("[0-9]");
Matcher isNum = patternmatcher(str);
if(!isNummatches()) {
return false;
}
return true;
}
}
package comby3gtest;
import javaxswing;
import javaawt;
import javaawtevent;
/面板区/
public class Cale extends JFrame implements ActionListener //实现该接口
{
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
JPanel p5;
JPanel p6;//定义p1~p6 6个面板
JTextField tex;
JButton bt2[];
JButton bt3[];
JButton bt4[];
JButton bt5[];
JButton bt6[]; //按钮数组,把每行的按钮个数存储到数组中
String S,S1,S2,S3;//定义字符串
Cale(String title)
{
p1=new JPanel();//实例化面板
JLabel jl1=new JLabel("计算结果:");
jl1setForeground(Colorred);
tex=new JTextField(30);//创建文本框
p1add(jl1);//将文本框添加到面板上
p1add(tex);
bt2=new JButton[24];
p2=new JPanel();
bt2[0]=new JButton("Java");
bt2[0]addActionListener(this);//监听事件
bt2[1]=new JButton("Backspace");
bt2[1]addActionListener(this);//监听事件
bt2[1]setForeground(Colorred);
bt2[2]=new JButton("CE");
bt2[2]addActionListener(this);//监听事件
bt2[2]setForeground(Colorred);
bt2[3]=new JButton("C");
bt2[3]addActionListener(this);//监听事件
bt2[3]setForeground(Colorred);
p2setLayout(new GridLayout(1,4));//网格布局1行4列
p2add(bt2[0]);
p2add(bt2[1]);
p2add(bt2[2]);
p2add(bt2[3]);//将按钮添加到面板上
bt3=new JButton[36];
p3=new JPanel();//实例化面板
bt3[0]=new JButton("7");
bt3[0]addActionListener(this);//监听事件
bt3[0]setForeground(Colorblue);
bt3[1]=new JButton("8");
bt3[1]addActionListener(this);//监听事件
bt3[1]setForeground(Colorblue);
bt3[2]=new JButton("9");
bt3[2]addActionListener(this);//监听事件
bt3[2]setForeground(Colorblue);
bt3[3]=new JButton("/");
bt3[3]addActionListener(this);//监听事件
bt3[3]setForeground(Colorred);
Font l = new Font("",FontBOLD,30);//设置字体大小,并且设置为size=30
bt3[3]setFont(l);
bt3[4]=new JButton("sqrt");
bt3[4]addActionListener(this);//监听事件
bt3[4]setForeground(Colorblue);
p3setLayout(new GridLayout(1,5));//网格布局1行5列
p3add(bt3[0]);
p3add(bt3[1]);
p3add(bt3[2]);
p3add(bt3[3]);
p3add(bt3[4]);//将按钮添加到面板上
bt4=new JButton[36];
p4=new JPanel();
bt4[0]=new JButton("4");
bt4[0]addActionListener(this);//监听事件
bt4[0]setForeground(Colorblue);
bt4[1]=new JButton("5");//监听事件
bt4[1]addActionListener(this);
bt4[1]setForeground(Colorblue);
bt4[2]=new JButton("6");
bt4[2]addActionListener(this);//监听事件
bt4[2]setForeground(Colorblue);
bt4[3]=new JButton("");
bt4[3]addActionListener(this);//监听事件
bt4[3]setForeground(Colorred);
Font a = new Font("",FontBOLD,30);//设置字体大小,并且设置为size=30
bt4[3]setFont(a);
bt4[4]=new JButton("%");
bt4[4]addActionListener(this);//监听事件
bt4[4]setForeground(Colorred);
Font l1 = new Font("",FontBOLD,30);
bt4[4]setFont(l1);
p4setLayout(new GridLayout(1,5));//网格布局1行5列
p4add(bt4[0]);
p4add(bt4[1]);
p4add(bt4[2]);
p4add(bt4[3]);
p4add(bt4[4]);//将按钮添加到面板上
bt5=new JButton[36];
p5=new JPanel();//实例化面板
bt5[0]=new JButton("1");
bt5[0]addActionListener(this);//监听事件
bt5[0]setForeground(Colorblue);
bt5[1]=new JButton("2");
bt5[1]addActionListener(this);//监听事件
bt5[1]setForeground(Colorblue);
bt5[2]=new JButton("3");
bt5[2]addActionListener(this);//监听事件
bt5[2]setForeground(Colorblue);
bt5[3]=new JButton("-");
bt5[3]addActionListener(this);//监听事件
bt5[3]setForeground(Colorred);
Font l2 = new Font("",FontBOLD,30);//设置字体大小,并且设置为size=30
bt5[3]setFont(l2);
bt5[4]=new JButton("sin");
bt5[4]addActionListener(this);//监听事件
bt5[4]setForeground(Colorblue);
p5setLayout(new GridLayout(1,5));//网格布局1行5列
p5add(bt5[0]);
p5add(bt5[1]);
p5add(bt5[2]);
p5add(bt5[3]);
p5add(bt5[4]);//将按钮添加到面板上
bt6=new JButton[36];
p6=new JPanel();
bt6[0]=new JButton("0");
bt6[0]addActionListener(this);//监听事件
bt6[0]setForeground(Colorblue);
bt6[1]=new JButton("cos");
bt6[1]addActionListener(this);//监听事件
bt6[1]setForeground(Colorblue);
bt6[2]=new JButton("");
bt6[2]addActionListener(this);//监听事件
bt6[2]setForeground(Colorblue);
bt6[3]=new JButton("+");
bt6[3]addActionListener(this);//监听事件
bt6[3]setForeground(Colorred);
Font l3 = new Font("",FontBOLD,30);//设置字体大小,并且设置为size=30
bt6[3]setFont(l3);
bt6[4]=new JButton("=");
bt6[4]addActionListener(this);//监听事件
bt6[4]setForeground(Colorred);
Font l4 = new Font("",FontBOLD,30);//设置字体大小,并且设置为size=30
bt6[4]setFont(l4);
p6setLayout(new GridLayout(1,5));//网格布局1行5列
p6add(bt6[0]);
p6add(bt6[1]);
p6add(bt6[2]);
p6add(bt6[3]);
p6add(bt6[4]);//将按钮添加到面板上
thissetLayout(new GridLayout(6,1,0,5));
thisadd(p1);//将p1添加到窗口中
thisadd(p2);//将p2添加到窗口中
thisadd(p3);//将p3添加到窗口中
thisadd(p4);//将p4添加到窗口中
thisadd(p5);//将p5添加到窗口中
thisadd(p6);//将p6添加到窗口中
thissetTitle(title);
thissetBounds(300,150,450,350);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//关闭并退出程序
thissetVisible(true);
}
/ 判断第一个数是否为零 /
public void actionPerformed(ActionEvent e)//实现接口中的方法,处理事件
{
String str = egetActionCommand();// 返回与此动作相关的命令字符串。
if(strequals("C"))
{
S="";
texsetText("0");
}
if(strequals("0"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"0");
}
if(strequals("1"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"1");
}
if(strequals("2"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"2");
}
if(strequals("3"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"3");
}
if(strequals("4"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"4");
}
if(strequals("5"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"5");
}
if(strequals("6"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"6");
}
if(strequals("7"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"7");
}
if(strequals("8"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"8");
}
if(strequals("9"))
{
if(texgetText()equals("0") || texgetText()equals("00"))
texsetText("");
texsetText(texgetText()+"9");
}
/计算区/
if(strequals("+"))
{
S1 = texgetText();//获得文本中的内容并送给S1
S = "+";
texsetText("");//设置文本框的内容
}
if(strequals("-"))
{
S1 = texgetText();
S = "-";
texsetText("");
}
if(strequals(""))
{
S1 = texgetText();
S = "";
texsetText("");
}
if(strequals("/"))
{
S1 = texgetText();
S = "/";
texsetText("");
}
if(strequals("%"))
{
S1 = texgetText();
S = "%";
texsetText("");
}
if(strequals("Java"))
{
S1 = texgetText();
S = "+";
texsetText("Java");
}
if(strequals("CE"))
{
S = "";
texsetText("");
}
if(strequals(""))
{
S = "";
texsetText(texgetText()+"");
}
if(strequals("="))
{
S3 = "";
S2 = texgetText();
double s1=DoubleparseDouble(S1);
double s2=DoubleparseDouble(S2);
double s=0;
if(Sequals("+"))//计算方法,并把计算的值送给s
{
s = s1+s2;
}
if(Sequals("-"))
{
s = s1-s2;
}
if(Sequals(""))
{
s = s1s2;
}
if(Sequals("/"))
{
s = s1/s2;
}
if(Sequals("%"))
{
s = s1%s2;
}
texsetText(s+"");//获取s的值在文本框显示
}
/三角函数/
if(Sequals("sin"))
{
S1 = texgetText();
double d = DoubleparseDouble(S1);//将获得的数据转换成double
texsetText(Mathsin(d)+"");//调用数学类的方法
}
if(Sequals("cos"))
{
S1 = texgetText();
double d = DoubleparseDouble(S1);//将获得的数据转换成double
texsetText(Mathcos(d)+"");//调用数学类的方法
}
if(Sequals("sqrt"))
{
S1 = texgetText();
double d = DoubleparseDouble(S1);//将获得的数据转换成double
texsetText(Mathsqrt(d)+"");
}
if(Sequals("Backspace"))//Backspace为退格键
{
S1=texgetText();
char c[]=S1toCharArray();
if(S3equals("")) //当文本框为结算的结果时
{
}
if(S1equals("0")) //当文本框为0时时
{
}
else
{
String s=new String(c,0,clength-1);
texsetText(s);
}
if(S1length()==1) //当文本框只有1个数字时
texsetText("");
}
}
public static void main(String[] args)
{
Cale c=new Cale("计算器");
}
}
这个计算器你自己再去优化下吧 三角函数不能用 其他的加减乘除还是可以的
界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import javaawt;
import javaawtevent;
import javaxswing;
import javautil;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frameshow();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFilesetText("文件");
menuFile1setText("编辑");
menuFile2setText("查看");
menuFile3setText("帮助");
menuFileExitsetText("退出");
menuFileExitaddActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFramethiswindowClosed();
}
}
);
menuFileadd(menuFileExit);
menuBaradd(menuFile);
menuBaradd(menuFile1);
menuBaradd(menuFile2);
menuBaradd(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
thisgetContentPane()add(new Allpanel());
thisaddWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFramethiswindowClosed();
}
}
);
}
protected void windowClosed() {
Systemexit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
thisfield=new JTextField(30);
thisfieldsetHorizontalAlignment(JTextFieldRIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (thisfield);
}
}
class Allpanel extends JPanel {
public Allpanel() {
thissetLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
thisadd(np,BorderLayoutNORTH);
thisadd(cp,BorderLayoutCENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/
Method Centercenter
/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Toolgetinstance()getfield();
thissetLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","","%","1","2","3","-","1/x","0","+/-","","+","="};
for(int i=0;i<20;i++)
{
final JButton b=new JButton(arg[i]);
//thisadd(new JButton(arg[i]));
thisadd(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=bgetText();
String ma=textgetText();
if(vc3contains("v3"))
{
textsetText("0"+mark);
vc3clear();
}
else if(vccontains("a"))
{
if(vc2contains("v2"))
{
textsetText("0"+mark);
vcclear();
vc2clear();
}
else
{
textsetText(mark);
vcclear();
Vecclear();
Vecadd(mark);
}
}
else
{
textsetText(matrim()+mark);
Vecadd(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=bgetText();
String m=textgetText();
if("yes"equals(begin))
{
vc3add("v3");
}
if(vc1contains("v1"))
{
vc2add("v2");
vc1clear();
}
if(!Veccontains("")&&!vccontains("a"))
{
textsetText(mtrim()+mar);
Vecadd("");
}
}
});
}
if(i==18)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
add=DoubleparseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="+";
}
});
}
if(i==13)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
jq=DoubleparseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="-";
}
});
}
if(i==3)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="/";
}
});
}
if(i==4)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
textsetText(StringvalueOf(Mathsqrt(cq)));
}
});
}
if(i==8)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cs=DoubleparseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=toolcs;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="";
}
});
}
if(i==19)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
dy=DoubleparseDouble(ma);
if(what=="add")
{
jg=StringvalueOf((tool+dy));
}
if(what=="jq")
{
jg=StringvalueOf((tool-dy));
}
if(what=="cs")
{
jg=StringvalueOf((tooldy));
}
if(what=="cq")
{
jg=StringvalueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=StringvalueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=StringvalueOf(dy-tool);
}
else if(to=="")
{
tool=cs;
jg=StringvalueOf(dytool);
}
else if(to=="/")
{
tool=cq;
jg=StringvalueOf(dy/tool);
}
else
{
jg=StringvalueOf(dy);
}
}
textsetText(jg);
Vecclear();
Vecadd("");
vcadd("a");
vc1add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Toolgetinstance()getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
thisadd(jb1);
thisadd(jb2);
thisadd(jb3);
jb1addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Toolgetinstance()getfield()getText();
textsetText(backmethod(back));
CentercenterVecremove(CentercenterVecsize()-1);
}
});
jb3addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
textsetText("0");
CentercenterVecclear();
CentercenterVecadd("");
Centercentervcadd("a");
Centercenterbegin="yes";
Centercentervc1clear();
Centercenterwhat=null;
Centercentertool=0;
}
});
}
public String backmethod(String str)
{
return strsubstring(0,strlength()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
thissetLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
thisadd(cn,BorderLayoutNORTH);
thisadd(cc,BorderLayoutCENTER);
thisadd(cw,BorderLayoutWEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
thissetLayout(new GridLayout(4,1,3,3));
thisadd(new JButton("MC"));
thisadd(new JButton("MR"));
thisadd(new JButton("MS"));
thisadd(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Toolgetinstance()getfield();
thisadd(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import javaawt;
import javaawtevent;
import javaxswing;
import javautil;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frameshow();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
thisgetContentPane()add(new Allpanel());
thisaddWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFramethiswindowClosed();
}
}
);
}
protected void windowClosed() {
Systemexit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
thisfield=new JTextField(30);
thisfieldsetHorizontalAlignment(JTextFieldRIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (thisfield);
}
}
class Allpanel extends JPanel {
public Allpanel() {
thissetLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
thisadd(np,BorderLayoutNORTH);
thisadd(cp,BorderLayoutCENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/
Method Centercenter
/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Toolgetinstance()getfield();
thissetLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","","1","2","3","-","0","=","","+"};
for(int i=0;i<16;i++)
{
final JButton b=new JButton(arg[i]);
//thisadd(new JButton(arg[i]));
thisadd(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=bgetText();
String ma=textgetText();
if(vc3contains("v3"))
{
textsetText("0"+mark);
vc3clear();
}
else if(vccontains("a"))
{
if(vc2contains("v2"))
{
textsetText("0"+mark);
vcclear();
vc2clear();
}
else
{
textsetText(mark);
vcclear();
Vecclear();
Vecadd(mark);
}
}
else
{
textsetText(matrim()+mark);
Vecadd(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=bgetText();
String m=textgetText();
if("yes"equals(begin))
{
vc3add("v3");
}
if(vc1contains("v1"))
{
vc2add("v2");
vc1clear();
}
if(!Veccontains("")&&!vccontains("a"))
{
textsetText(mtrim()+mar);
Vecadd("");
}
}
});
}
if(i==15)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
add=DoubleparseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="+";
}
});
}
if(i==11)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
jq=DoubleparseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="-";
}
});
}
if(i==3)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="/";
}
});
}
if(i==7)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cs=DoubleparseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=toolcs;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="";
}
});
}
if(i==13)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
dy=DoubleparseDouble(ma);
if(what=="add")
{
jg=StringvalueOf((tool+dy));
}
if(what=="jq")
{
jg=StringvalueOf((tool-dy));
}
if(what=="cs")
{
jg=StringvalueOf((tooldy));
}
if(what=="cq")
{
jg=StringvalueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=StringvalueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=StringvalueOf(dy-tool);
}
else if(to=="")
{
tool=cs;
jg=StringvalueOf(dytool);
}
else if(to=="/")
{
tool=cq;
jg=StringvalueOf(dy/tool);
}
else
{
jg=StringvalueOf(dy);
}
}
textsetText(jg);
Vecclear();
Vecadd("");
vcadd("a");
vc1add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Toolgetinstance()getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
thissetLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
thisadd(cn,BorderLayoutNORTH);
thisadd(cc,BorderLayoutCENTER);
thisadd(cw,BorderLayoutWEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Toolgetinstance()getfield();
thisadd(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!
import javautil;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(Systemin);
Systemoutprintln("简易计算器");
Systemoutprintln("\t\t\t\t\t");
Systemoutprintln(" 使用说明: 1加法 2减法 3乘法 4除法 ");
Systemoutprintln("\t\t\t\t\t");
Systemoutprintln("");
for(int i=0;i<100;i++){
Systemoutprint("\n请选择运算规则:");
int num = inputnextInt();
switch(num){
case 1:
Systemoutprintln("\n你选择了加法\n");
Systemoutprint("请输入第1个加数:");
int jiashu1 = inputnextInt();
Systemoutprint("请输入第2个加数:");
int jiashu2 = inputnextInt();
Systemoutprintln("运算结果为:" + jiashu1 + " + " + jiashu1 + " = " + (jiashu1 + jiashu2));
break;
case 2:
Systemoutprintln("\n你选择了减法\n");
Systemoutprint("请输入被减数:");
int jianshu1 = inputnextInt();
Systemoutprint("请输入减数:");
int jianshu2 = inputnextInt();
Systemoutprintln("运算结果为:" + jianshu1 + " - " + jianshu2 + " = " + (jianshu1 - jianshu2));
break;
case 3:
Systemoutprintln("\n你选择了乘法\n");
Systemoutprint("请输入第1个因数:");
int chengfa1 = inputnextInt();
Systemoutprint("请输入第2个因数:");
int chengfa2 = inputnextInt();
Systemoutprintln("运算结果为:" + chengfa1 + " " + chengfa2 + " = " + (chengfa1 chengfa2));
break;
case 4:
Systemoutprintln("\n你选择了除法\n");
Systemoutprint("请输入被除数:");
double chufa1 = inputnextInt();
Systemoutprint("请输入除数:");
double chufa2 = inputnextInt();
Systemoutprintln("运算结果为:" + chufa1 + " / " + chufa2 + " = " + (chufa1 / chufa2) + " 余 " + (chufa1 % chufa2));
break;
default:
Systemoutprintln("\n你的选择有错,请重新选择!");
break;
}
}
}
}
import javaawt;
import javaawtevent;
public class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main; //主面板
private Panel p_num; //数字面板
private Panel p_oper; // *** 作符面板
private Panel p_show; //显示面板
private Button b_num[]; //数字按钮
private Button b_oper[]; // *** 作符面板
public CalcAppDemo(String title){
setTitle(title);
t_result = new TextField("00", 21);
p_main = new Panel();
p_num = new Panel();
p_oper = new Panel();
p_show = new Panel();
p_mainsetLayout(new BorderLayout());
p_numsetLayout(new GridLayout(4, 3, 1, 1));
p_opersetLayout(new GridLayout(4, 2, 1, 1));
b_num = new Button[12];
for(int i=0; i<9; i++)
{
b_num[i] = new Button(new Integer(i+1)toString());
}
b_num[9] = new Button("0");
b_num[10] = new Button("cls");
b_num[11] = new Button("");
for(int i=0; i<12; i++)
{
p_numadd(b_num[i]);
}
b_oper = new Button[8];
b_oper[0] = new Button("+");
b_oper[1] = new Button("-");
b_oper[2] = new Button("");
b_oper[3] = new Button("/");
b_oper[4] = new Button("pow");
b_oper[5] = new Button("sqrt");
b_oper[6] = new Button("+/-");
b_oper[7] = new Button("=");
for(int i=0; i<8; i++) //
{
p_operadd(b_oper[i]);
}
t_resultsetEditable(false);
p_showadd(t_result, BorderLayoutNORTH);
p_mainadd(p_show, BorderLayoutNORTH);
p_mainadd(p_num, BorderLayoutWEST);
p_mainadd(p_oper, BorderLayoutEAST);
thisadd(p_main, BorderLayoutCENTER);
setSize(400, 400);
setResizable(false);
pack();
thisaddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
Systemexit(0);
}
});
ButtonListener b1 = new ButtonListener();
for(int i=0; i<12; i++)
{
b_num[i]addActionListener(b1);
}
for(int i=0; i<8; i++)
{
b_oper[i]addActionListener(b1);
}
}
class ButtonListener implements ActionListener
{
private String lastOp; //存储上一此 *** 作符
private String strVal; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime; //判断是否第一次按下的是 *** 作符按钮
private boolean operatorPressed;//判断是否已经按过 *** 作符按钮
ButtonListener()
{
firsttime = true;
strVal = "";
}
//事件处理器
public void actionPerformed(ActionEvent e)
{
String s = ((Button)egetSource())getLabel()trim();
if(CharacterisDigit(scharAt(0)))
{//判断是 *** 作数还是 *** 作符
handleNumber(s);
}
else
{
calculate(s);
}
}
//判断是一元 *** 作符还是二元 *** 作符,并根据 *** 作符类型做计算
void calculate(String op)
{
operatorPressed = true;
if(firsttime&&! isUnary(op))
{
total = getNumberOnDisplay();
firsttime = false;
}
if(isUnary(op))
{
handleUnaryOp(op);
}
else if(lastOp != null)
{
handleBinaryOp(lastOp);
}
if(! isUnary(op))
{
lastOp = op;
}
}
//判断是否一元 *** 作符
boolean isUnary(String s)
{
return sequals("=")
||sequals("cls")||sequals("sqrt")
||sequals("+/-")||sequals("");
}
//处理一元 *** 作符
void handleUnaryOp(String op)
{
if(opequals("+/-"))
{//
number = negate(getNumberOnDisplay() + "");
t_resultsetText("");
t_resultsetText(number + "");
return;
}else if(opequals(""))
{
handleDecPoint();
return;
}else if(opequals("sqrt"))
{
number = Mathsqrt(getNumberOnDisplay());
t_resultsetText("");
t_resultsetText(number + "");
return;
}else if(opequals("="))
{//
if(lastOp!= null && !isUnary(lastOp))
{
handleBinaryOp(lastOp);
}
lastOp = null;
firsttime = true;
return;
}else
{
clear();
}
}
//处理二元运算符
void handleBinaryOp(String op)
{
if(opequals("+"))
{
total +=number;
}else if(opequals("-"))
{
total -=number;
}else if(opequals(""))
{
total =number;
}else if(opequals("/"))
{
try
{
total /=number;
}catch(ArithmeticException ae){}
}else if(opequals("pow"))
total = Mathpow(total, number);
//t_resultsetText("");
lastOp = null;
// strVal = "";
number = 0;
t_resultsetText(total + "");
}
//该方法用于处理数字按钮
void handleNumber(String s)
{
if(!operatorPressed)
{
strVal += s;
}else
{
operatorPressed = false;
strVal = s;
}
//
number = new Double(strVal)doubleValue();
t_resultsetText("");
t_resultsetText(strVal);
}
//该方法用于按下""按钮
void handleDecPoint()
{
operatorPressed = false;
//
if(strValindexOf("")<0)
{
strVal += "";
}
t_resultsetText("");
t_resultsetText(strVal);
}
//该方法用于将一个数求反
double negate(String s)
{
operatorPressed = false;
//如果是一个整数,去掉小数点后面的0
if(number == (int)number)
{
s = ssubstring(0,sindexOf(""));
}
//如果无"-"增加在该数的前面
if(sindexOf("-")<0)
{
strVal = "-" + s;
}
else
{
strVal = ssubstring(1);
}
return new Double(strVal)doubleValue();
}
//将显示框中的值转换成Double
double getNumberOnDisplay()
{
return new Double(t_resultgetText())doubleValue();
}
//清除屏幕并设置所有的标识
void clear()
{
firsttime = true;
lastOp = null;
strVal = "";
total = 0;
number = 0;
t_resultsetText("0");
}
}
public static void main(String[] args) {
CalcAppDemo c = new CalcAppDemo("简单的计算器程序");
csetVisible(true);
}
}
以上就是关于求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算。 (全部的内容,包括:求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算。 (、java:编写一个计算器小程序,要求可以做加减乘除运算、用Java编写计算器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)