今天老师布置了计算器的JAVA程序题,小弟平时贪玩,球高手帮忙……跪谢……
题目如下:
计算器模拟程序
功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。
界面要示:用图形界面实现。可参考下图。
小弟急啊,求高手尽快解答。
今天老师布置了计算器的JAVA程序题,小弟平时贪玩,球高手帮忙……跪谢……
题目如下:
计算器模拟程序
功能要求:该程序显示GUI用户界面,能实现整数的加、减、乘、除四则运算。
界面要示:用图形界面实现。可参考下图。
小弟急啊,求高手尽快解答。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CaculatorFrame extends Frame
{
String sresult = ""; //存放当前要计算的数据
double iresult = 0.0; //存放到当前为止的计算结果
int flag1 = 0,flag2 = 0; //flag1记录小数点是否已经输入了,不允许两个以上小数点,flag2记录运算符号
TextField tf = new TextField("0",10);
public CaculatorFrame()
{
this.setLayout(new GridLayout(9,2));
tf.setFont(new Font(null,Font.ITALIC|Font.BOLD,40));
tf.setEditable(false);
this.add(tf);
//生成0-9的数字按钮
for(int i=0;i<10;i++) this.add(MyButton(String.valueOf((char)('0' + i))));
this.add(MyButton("."));
this.add(MyButton("C"));
this.add(MyButton("+"));
this.add(MyButton("-"));
this.add(MyButton("*"));
this.add(MyButton("/"));
this.add(MyButton("="));
setTitle("Caculator");
setSize(new Dimension(200, 400));
pack();
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CaculatorFrame.this.windowClosed();
}
}
);
}
protected void windowClosed()
{
System.exit(0);
}
Button MyButton(String s) //按钮生成方法
{
Button bb= new Button(s);
bb.setBackground(Color.CYAN);
bb.setFont(new Font(null,Font.ITALIC|Font.BOLD,30));
bb.setSize(60,60);
bb.addActionListener(new BTlistener());
return bb;
}
class BTlistener implements ActionListener //事件处理监听类
{
public void actionPerformed(ActionEvent e)
{
Button bt = (Button)e.getSource();
String bn = bt.getLabel();
//输入数字处理
if (bn.compareTo("0") >= 0 && bn.compareTo("9") <=0)
{
if (bn == "0" && sresult.length() == 0)
tf.setText("0");
else
{
sresult=sresult + bn;
tf.setText(sresult);
}
}
//输入C,=,+,-,*,/等符号处理
if (bn == "C")
{
sresult = "";
iresult = 0;
tf.setText("0");
}
if ( bn == "=")
{
if (sresult.length() != 0 )
{
cacu(0);
}
}
if (bn == "+")
{
cacu(1);
}
if (bn == "-")
{
cacu(2);
}
if (bn == "*")
{
cacu(3);
}
if (bn == "/")
{
cacu(4);
}
if (bn == ".")
{
if(flag1 == 0 )
{ sresult=sresult + bn;flag1 = 1;}
tf.setText(sresult );
}
}
public void cacu(int x) //计算结果
{
if(iresult == 0 && sresult.length() != 0 )
{iresult = Double.parseDouble(sresult);}
else if(sresult.length() != 0 )
{
if (flag2 == 1) iresult = iresult + Double.parseDouble(sresult);
if (flag2 == 2) iresult = iresult - Double.parseDouble(sresult);
if (flag2 == 3) iresult = iresult * Double.parseDouble(sresult);
if (flag2 == 4 && Double.compare(Double.parseDouble(sresult), 0.0 ) != 0
)
iresult = iresult / Double.parseDouble(sresult);
else
{
if (Double.compare(Double.parseDouble(sresult) , 0.0 ) == 0
)
{
flag1=0;
sresult = "";
flag2=x;
iresult=0;
tf.setText("除数为0!" );
return;
}
}
}
flag1=0;
sresult = "";
tf.setText("" + iresult );
flag2 = x;
}
}
}
主类:
public class Caculator {
public static void main(String[] args) {
// Create application frame.
CaculatorFrame frame = new CaculatorFrame();
// Show frame
frame.setVisible(true);
}
}
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Calculator extends JApplet { public static void main(String[] args) { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public void init() { CalculatorPanel panel = new CalculatorPanel(); add(panel); } }
class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); } }
class CalculatorPanel extends JPanel { private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
public CalculatorPanel() { setLayout(new BorderLayout());
result = 0; lastCommand = "="; start = true;
// add the display
display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction(); ActionListener command = new CommandAction();
// add the buttons in a 4 x 4 grid
panel = new JPanel(); panel.setLayout(new GridLayout(4, 4));
addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command);
addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command);
addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command);
addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command);
add(panel, BorderLayout.CENTER); }
private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); }
private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } }
private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand();
if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { display.setText("" + calculate(Double.parseDouble(display.getText()),lastCommand)); lastCommand = command; start = true; } } }
public double calculate(double x,String action) { if (action.equals("+")) result += x; else if (action.equals("-")) result -= x; else if (action.equals("*")) result *= x; else if (action.equals("/")) result /= x; else if (action.equals("=")) result = x; return result; }
} 这个运行出来和你上面一样的 问问团队邯涵学友
随时回答您的问题import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.util.EventListener; import javax.swing.*; class TextFrame extends JFrame{ //窗体的标题若在方法中定义只能在方法中有效若在方法外用则可以整个类有效 //JFrame f = new JFrame("this is my first"); JTextField tf = new JTextField("0"); tf.setHorizontalAlignment(0); String cal;//操作符变量 String source;//第一个操作数 String second;//第二个操作数 Double result; JMenuBar jmb = new JMenuBar(); JMenu menuEidt = new JMenu("编辑"); JMenu menuView = new JMenu("查看"); JMenu menuHelp = new JMenu("帮助"); JMenuItem eidtCopy = new JMenuItem("复制"); JMenuItem eidtParse = new JMenuItem("粘贴"); JMenuItem helpAbout = new JMenuItem("关于计算机"); String copyText =""; //用init方法实现窗口的构造 public void init(){ //正常退出程序,虽然JFrame里面就集成了监听器,但是它只是触发监听的时候隐藏窗体,并不结束程序,此语句用来退出程序 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Jframe中不能添加容器,所以要用定义容器 Container c = this.getContentPane(); JPanel p = new JPanel(); p.setLayout(new GridLayout(4,5)); jmb.add(menuEidt); jmb.add(menuView); jmb.add(menuHelp); menuEidt.add(eidtCopy); menuEidt.add(eidtParse); menuHelp.add(helpAbout); this.setJMenuBar(jmb); JButton btOne = new JButton("1"); JButton btTwo = new JButton("2"); JButton btThree = new JButton("3"); JButton btAdd = new JButton("+"); JButton btFour = new JButton("4"); JButton btFive = new JButton("5"); JButton btSix = new JButton("6"); JButton btReduce = new JButton("-"); JButton btSeven = new JButton("7"); JButton btEight = new JButton("8"); JButton btNine = new JButton("9"); JButton btCheng = new JButton("*"); JButton btZero = new JButton("0"); JButton btPoint = new JButton("."); JButton btDeng = new JButton("="); JButton btChu = new JButton("/"); JButton btSqrt = new JButton("Sqrt"); JButton btReset = new JButton("%"); JButton btCbrt = new JButton("Cbrt"); JButton btFen = new JButton("1/x"); //把按钮添加容器上,然后再把容器添加到窗体上 p.add(btSqrt); p.add(btOne); p.add(btTwo); p.add(btThree); p.add(btAdd); p.add(btReset); p.add(btFour); p.add(btFive); p.add(btSix); p.add(btReduce); p.add(btCbrt); p.add(btSeven); p.add(btEight); p.add(btNine); p.add(btCheng); p.add(btFen); p.add(btZero); p.add(btPoint); p.add(btDeng); p.add(btChu); //把容器P添加到窗体上 c.add(p,"Center"); c.add(tf,"North"); this.setSize(300,300); this.setVisible(true); //b监听所有的事件源,用一个监听即可! Digit d = new Digit(); btOne.addActionListener(d); btTwo.addActionListener(d); btThree.addActionListener(d); btFour.addActionListener(d); btFive.addActionListener(d); btSix.addActionListener(d); btSeven.addActionListener(d); btEight.addActionListener(d); btNine.addActionListener(d); btZero.addActionListener(d); Oper O = new Oper(); btAdd.addActionListener(O); btReduce.addActionListener(O); btCheng.addActionListener(O); btChu.addActionListener(O); Res R = new Res(); btDeng.addActionListener(R); RestNum RS = new RestNum(); btReset.addActionListener(RS); SqrtNum SN = new SqrtNum(); btSqrt.addActionListener(SN); FenNum FN = new FenNum(); btFen.addActionListener(FN); CbrtNum CN = new CbrtNum(); btCbrt.addActionListener(CN); MyMenuListener MML = new MyMenuListener(); eidtCopy.addActionListener(MML); eidtParse.addActionListener(MML); helpAbout.addActionListener(MML); } //main方法直接调用Init方法 public static void main(String ar []){ new TextFrame().init(); }
//用来监视数字按钮,这是一个内部类 class Digit implements ActionListener{ public void actionPerformed(ActionEvent e){ //得到数字按钮的标签,得到的是字符串类型 String s = e.getActionCommand(); String a = tf.getText(); //显示文本框里的按钮内容 tf.setText(s); if(a.equals("0")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/")) tf.setText(s); else tf.setText(a+s); } } class Oper implements ActionListener{ public void actionPerformed(ActionEvent e){ //得到当前操作符 cal = e.getActionCommand(); //用于得到前一个操作数 source = tf.getText(); tf.setText(cal); } } class Res implements ActionListener{ public void actionPerformed(ActionEvent e){ try{ double num1; num1 = Double.parseDouble(source); second = tf.getText(); double num2; num2 = Double.parseDouble(second); if(cal.equals("+")) result = num1+num2; if(cal.equals("-")) result = num1-num2; if(cal.equals("*")) result = num1*num2; if(cal.equals("/")){ try{ result = num1/num2; }catch(ArithmeticException c){ System.out.println("除数不能为0!"); } } tf.setText(result.toString()); }catch(Exception a){ System.out.println("There is something wrong!!!"); } } } class RestNum implements ActionListener{ public void actionPerformed(ActionEvent e){ tf.setText("0"); } } class SqrtNum implements ActionListener{ public void actionPerformed(ActionEvent e){ source = tf.getText(); Double num = Double.parseDouble(source); //注意这里引用的sqrt是Math类里面的方法,必须要体现面向对象,所以要用Math来调用sqrt方法 tf.setText(Double.toString(Math.sqrt(num))); } } class FenNum implements ActionListener{ public void actionPerformed(ActionEvent e){ try{ source = tf.getText(); //如果在这里捕获异常,必须导正除数确实是0! Integer num = Integer.parseInt(source); tf.setText(Double.toString(1/num)); System.out.println(num); }catch(ArithmeticException b){ System.out.println("除数不能为0"); } } } class CbrtNum implements ActionListener{ public void actionPerformed(ActionEvent e){ source = tf.getText(); Double num = Double.parseDouble(source); tf.setText(Double.toString(Math.cbrt(num))); } } class MyMenuListener implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==eidtCopy) copyText = tf.getText(); else if(e.getSource()==eidtParse) tf.setText(copyText); else if(e.getSource()==helpAbout) JOptionPane.showMessageDialog(TextFrame.this, "*****************************************************\n"+ "| 程序作者:小女墙 |\n"+ "| 课 程:《JAVA程序设计》 |\n"+ "| 设计时间:2009年11月3日 |\n"+ "| 作者 QQ:785414943 |\n"+ "| 邮箱地址: |\n" "*****************************************************\n" ,"关于计算器",JOptionPane.INFORMATION_MESSAGE); } } }
我自己编写了一个,需要的话可以找我,支持括号,优先级等
用一个jframe,选网格布局,north上放一个jtextfield。south上放一个jpanel 在在上面放一堆按钮。给每个按钮加监听器,当点击按钮时,用jtextfield调用settext()方法,让jtextfield显示按钮对应的值。
大致思路就是这样。代码也就不到100行,很简单的。