简易计算器java课程设计
A. java 计算器课程设计报告
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;//导包
class MyClass extends JFrame
//创建一个MyClass类继承JFrame框架的窗口类,
//也就是说JFrame里有的功能MyClass都能实现
{
a1=new JLabel("第一个数");
//创建一个显示“第一个数”的标签
JLabel a2=new JLabel("第二个数");
JLabel a3=new JLabel("运算结果");
JTextField b1=new JTextField(5);
//创建一个文本框、默认长度为5,用来输入运算数字,当然也可以默认为空
JTextField b2=new JTextField(5);
JTextField b3=new JTextField(5);
//创建一个用于显示运算结果的标签,也可以创建一个标签来显示
JButton a=new JButton("加");
//创建一个用于加法计算的按钮,点击时进行加法运算
JButton b=new JButton("减");
JButton c=new JButton("乘");
JButton d=new JButton("除");
JPanel jp1=new JPanel();//创建一个面板,用来放控件
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
MyClass()//构造函数,用来初始化的
{
setLayout(new GridLayout(3,1));//添加一个四行四列的布局管理器
jp1.setLayout(new FlowLayout());//设置JP1面板为流式布局管理器
jp1.setLayout(new FlowLayout());
//将a1,b1,a2,b2四个控件添加到jp1面板中
jp1.add(a1);
jp1.add(b1);
jp1.add(a2);
jp1.add(b2);
jp1.add(a3);
//将a,b,c,d四个控件添加到jp2面板中
jp2.add(a);
jp2.add(b);
jp2.add(c);
jp2.add(d);
jp3.add(a3);
jp3.add(b3);
//将jp1,jp2,jp3三个面板添加到窗口中
add(jp1);
add(jp3);
add(jp2);
Object e;
a.addActionListener(new ActionListener()
//创建一个匿名的事件监听器
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double x=Double.valueOf(b1.getText().toString());
//获取第一个输入数,并将其由String型转换成double型
double y=Double.valueOf(b2.getText().toString());
//获取第二个输入数,并将其由String型转换成double型
b3.setText(""+(x+y));
//将运算结果在b3这个文本框中显示
}
});
b.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double x=Double.valueOf(b1.getText().toString());
double y=Double.valueOf(b2.getText().toString());
b3.setText(""+(x-y));
}
});
c.addActionListener(new ActionListener()//创建一个匿名的事件监听器
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double x=Double.valueOf(b1.getText().toString());
double y=Double.valueOf(b2.getText().toString());
b3.setText(""+(x*y));
}
});
d.addActionListener(new ActionListener()//创建一个匿名的事件监听器
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double x=Double.valueOf(b1.getText().toString());
double y=Double.valueOf(b2.getText().toString());
//因为0不能做除数,所以在这里需要进行判断
if(y==0)
{
b3.setText("错误");
}
else
{
b3.setText(""+(x/y));
}
}
});
//下面的是设置窗口的属性
this.setTitle("计算器");//设置窗口的标题
//this.setSize(400,400);//设置窗口的大小,也可以改成this.pack()
this.pack();
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);//设置关闭属性
this.setVisible(true);//设置窗口的可见性
}
public static void main(String[] args)//主函数
{
new MyClass();
}
}
B. java课程设计---计算器 要求如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList;
import java.io.*;
public class CalculatorWindow extends JFrame implements ActionListener{
NumberButton numberButton[];
OperationButton operationButton[];
JButton 小数点操作,正负号操作,退格操作,等号操作,清零操作,sin;
JTextField resultShow; //显示计算结果
JTextField showComputerProcess; //显示当前计算过程
JTextArea saveComputerProcess; //显示计算步骤
JButton saveButton,Button,clearButton;
LinkedList<String> list; //链表用来存放第一个运算数、运算符号和第二个运算数
HandleDigit handleDigit; //负责处理ActionEvent事件
HandleOperation handleOperation ;
HandleBack handleBack;
HandleClear handleClear;
HandleEquality handleEquality;
HandleDot handleDot;
HandlePOrN handlePOrN;
HandleSin handleSin;
public CalculatorWindow(){
setTitle("计算器");
JPanel panelLeft,panelRight;
list=new LinkedList<String>();
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.BOLD,16));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setEditable(false);
resultShow.setBackground(Color.white);
showComputerProcess=new JTextField();
showComputerProcess.setHorizontalAlignment(JTextField.CENTER);
showComputerProcess.setFont(new Font("Arial",Font.BOLD,16));
showComputerProcess.setBackground(Color.cyan);
showComputerProcess.setEditable(false);
saveComputerProcess=new JTextArea();
saveComputerProcess.setEditable(false);
saveComputerProcess.setFont(new Font("宋体",Font.PLAIN,16));
numberButton=new NumberButton[10];
handleDigit=new HandleDigit(list,resultShow,showComputerProcess);
for(int i=0;i<=9;i++){
numberButton[i]=new NumberButton(i);
numberButton[i].setFont(new Font("Arial",Font.BOLD,20));
numberButton[i].addActionListener(handleDigit);
}
operationButton=new OperationButton[4];
handleOperation=new HandleOperation(list,resultShow,
showComputerProcess,saveComputerProcess);
String 运算符号[]={"+","-","*","/"};
for(int i=0;i<4;i++){
operationButton[i]=new OperationButton(运算符号[i]);
operationButton[i].setFont(new Font("Arial",Font.BOLD,20));
operationButton[i].addActionListener(handleOperation);
}
小数点操作=new JButton(".");
handleDot=new HandleDot(list,resultShow,showComputerProcess);
小数点操作.addActionListener(handleDot);
正负号操作=new JButton("+/-");
handlePOrN=new HandlePOrN(list,resultShow,showComputerProcess);
正负号操作.addActionListener(handlePOrN);
等号操作=new JButton("=");
handleEquality=new HandleEquality(list,resultShow,
showComputerProcess,saveComputerProcess);
等号操作.addActionListener(handleEquality);
sin=new JButton("sin");
handleSin=new HandleSin(list,resultShow,
showComputerProcess,saveComputerProcess);
sin.addActionListener(handleSin);
退格操作=new JButton("退格");
handleBack=new HandleBack(list,resultShow,showComputerProcess);
退格操作.addActionListener(handleBack);
清零操作=new JButton("C");
handleClear=new HandleClear(list,resultShow,showComputerProcess);
清零操作.addActionListener(handleClear);
清零操作.setForeground(Color.red);
退格操作.setForeground(Color.red);
等号操作.setForeground(Color.red);
sin.setForeground(Color.blue);
正负号操作.setForeground(Color.blue);
小数点操作.setForeground(Color.blue);
panelLeft=new JPanel();
panelRight=new JPanel();
panelLeft.setLayout(new BorderLayout());
JPanel centerInLeft=new JPanel();
panelLeft.add(resultShow,BorderLayout.NORTH);
panelLeft.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(4,5));
centerInLeft.add(numberButton[1]);
centerInLeft.add(numberButton[2]);
centerInLeft.add(numberButton[3]);
centerInLeft.add(operationButton[0]);
centerInLeft.add(清零操作);
centerInLeft.add(numberButton[4]);
centerInLeft.add(numberButton[5]);
centerInLeft.add(numberButton[6]);
centerInLeft.add(operationButton[1]);
centerInLeft.add(退格操作);
centerInLeft.add(numberButton[7]);
centerInLeft.add(numberButton[8]);
centerInLeft.add(numberButton[9]);
centerInLeft.add(operationButton[2]);
centerInLeft.add(sin);
centerInLeft.add(numberButton[0]);
centerInLeft.add(正负号操作);
centerInLeft.add(小数点操作);
centerInLeft.add(operationButton[3]);
centerInLeft.add(等号操作);
panelRight.setLayout(new BorderLayout());
panelRight.add(showComputerProcess,BorderLayout.NORTH);
saveButton=new JButton("保存");
Button=new JButton("复制");
clearButton=new JButton("清除");
saveButton.setToolTipText("保存计算过程到文件");
Button.setToolTipText("复制选中的计算过程");
clearButton.setToolTipText("清除计算过程");
saveButton.addActionListener(this);
Button.addActionListener(this);
clearButton.addActionListener(this);
panelRight.add(new JScrollPane(saveComputerProcess),BorderLayout.CENTER);
JPanel southInPanelRight=new JPanel();
southInPanelRight.add(saveButton);
southInPanelRight.add(Button);
southInPanelRight.add(clearButton);
panelRight.add(southInPanelRight,BorderLayout.SOUTH);
JSplitPane split=new JSplitPane
(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,50,528,258);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Button)
saveComputerProcess.();
if(e.getSource()==clearButton)
saveComputerProcess.setText(null);
if(e.getSource()==saveButton){
JFileChooser chooser=new JFileChooser();
int state=chooser.showSaveDialog(null);
File file=chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION){
try{ String content=saveComputerProcess.getText();
StringReader read=new StringReader(content);
BufferedReader in= new BufferedReader(read);
FileWriter outOne=new FileWriter(file);
BufferedWriter out= new BufferedWriter(outOne);
String str=null;
while((str=in.readLine())!=null){
out.write(str);
out.newLine();
}
in.close();
out.close();
}
catch(IOException e1){}
}
}
}
public static void main(String args[]){
new CalculatorWindow();
}
}
C. 求java课程设计运算器程序
很简单的,加、减、乘、除,开方,清除功能。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator3() {
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (btn == equal) {
operate('=');
equaled = true;
} else if (btn == plus) {
operate('+');
equaled = false;
} else if (btn == minus) {
operate('-');
equaled = false;
} else if (btn == multi) {
operate('*');
equaled = false;
} else if (btn == div) {
operate('/');
equaled = false;
} else if (btn == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (btn == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText("" + i);
else if (!operated)
operation.setText(operation.getText() + i);
else {
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && !dotExist) {
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator) {
double currentNumber = Double.valueOf(operation.getText())
.doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args) {
Calculator3 calc = new Calculator3();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
D. java计算器课设
我有,给你一个
import java.awt.*;
import java.awt.event.*;
class Calculator extends WindowAdapter implements ActionListener
{
private double result=0,data1=0,radixPointDepth=1;//小数点位数
private boolean radixPointIndicate=false,resultIndicate=false;//小数点和运算结果
private char prec='+';
private Frame f;
private TextField tf;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
private Panel p;
static public void main(String args[])
{
Calculator de=new Calculator();
de.go();
}
public void go()
{
f=new Frame("计算器");
p=new Panel();
p.setLayout(new GridLayout(4,4));
tf=new TextField(30);
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North");
f.add(p,"Center");
f.add(b17,"South");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this);
f.setSize(250,190);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();
switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': //按了“0-9”,就执行下面
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+':
case '-':
case '*':
case '/':
case '=':
if(s.charAt(0)!='='&&resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{
switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());
if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
radixPointIndicate=false;
tf.setText("0.0");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
给分!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
E. java设计一个简单的计算器,要求根据输入的操作数和运算符(+-*/)计算其值
include<iostream>
float a,b,m=1;
string n;
while(m==1){
cin>>a>>b;
cin>>n;
switch(n){
case `+` : cout<<a<<"+"<<b<<"="<<a+b
<<end1;
break;
case `-` : cout<<a<<"-"<<b<<"="<<a-b
<<end1;
break;
case `*` : cout<<a<<"*"<<b<<"="<<a*b
<<end1;
break;
case `/` : cout<<a<<"/"<<b<<"="<<a/b
<<end1;
break;
case `%` : cout<<a<<"%"<<b<<"="<<a%b
<<end1;
break;
defult: cout<<"输入错误,请检查"
<<end1;
}
cout<<"按1继续,其他键退出:";
cin>>m;
}
F. 求JAVA课程设计计算器源代码~~~急要能用的
网上找到来一个,不把代自码贴过来了,自己看吧
http://www.blogjava.net/yangcai/archive/2006/11/14/81105.html
本人自己没有试过,纯粹想捞分
G. JAVA课程设计--简易计算器. 求源代码.
import javax.swing.*;//新的窗口组件包
import java.awt.*;
import java.awt.event.*;
public class Jisuanqi implements ActionListener
{ JFrame jf=new JFrame();
boolean dotExist, operated, equaled; // 帮助运算的布尔变量
double result; // 目前的结果
char lastOperator; // 表示上一运算符
JTextField jtf; // 显示栏
JButton dot, plus, minus, multi, div, sqrt, equal, change, clear,quyu; // 运算符
JButton[] numbers;
Panel p=new Panel();
// 构造者
public Jisuanqi()
{
jf.setTitle("陈巳伟牌计算器");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
result = 0;
lastOperator = '?';
// 初始化窗口变量
jtf = new JTextField("0");
jtf.setEditable(false); //设置文本框的可编辑性
jf.setSize(250,200);
jf.setVisible(true);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
change= new JButton("±");
clear = new JButton("NC");
quyu=new JButton("%");
p.setLayout(new GridLayout(5,4));
p.add(numbers[1]);
p.add(numbers[2]);
p.add(numbers[3]);
p.add(plus);
p.add(numbers[4]);
p.add(numbers[5]);
p.add(numbers[6]);
p.add(minus);
p.add(numbers[7]);
p.add(numbers[8]);
p.add(numbers[9]);
p.add(multi);
p.add(dot);
p.add(numbers[0]);
p.add(sqrt);
p.add(div);
p.add(equal);
p.add(change);
p.add(clear);
p.add(quyu);
jf.add(jtf,BorderLayout.NORTH);
jf.add(p,BorderLayout.CENTER);
numbers[0].addActionListener(this);
numbers[1].addActionListener(this);
numbers[2].addActionListener(this);
numbers[3].addActionListener(this);
plus.addActionListener(this);
numbers[4].addActionListener(this);
numbers[5].addActionListener(this);
numbers[6].addActionListener(this);
minus.addActionListener(this);
numbers[7].addActionListener(this);
numbers[8].addActionListener(this);
numbers[9].addActionListener(this);
multi.addActionListener(this);
dot.addActionListener(this);
sqrt.addActionListener(this);
div.addActionListener(this);
equal.addActionListener(this);
change.addActionListener(this);
clear.addActionListener(this);
quyu.addActionListener(this);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
jtf.setText("0");
dotExist = false;
operated = false;
equaled = false;
result = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn==quyu){
operate('%');
equaled=false;
}
else if (btn == change)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
result = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (jtf.getText().equals("0"))
jtf.setText("" + i);
else if(! operated)
jtf.setText(jtf.getText() + i);
else
{
jtf.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
jtf.setText(jtf.getText() + ".");
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(jtf.getText()).doubleValue();
if (lastOperator == '?')
result = currentNumber;
else if (lastOperator == '+')
result += currentNumber;
else if (lastOperator == '-')
result -= currentNumber;
else if (lastOperator == '*')
result *= currentNumber;
else if (lastOperator == '/')
result /= currentNumber;
else if(lastOperator=='%')
result %=currentNumber;
else if (lastOperator == 'p')
result *= -1;
else if (lastOperator == 's')
result = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
result = currentNumber;
jtf.setText("" + result);
operated = true;
lastOperator = operator;
}
public static void main(String[] args)
{new Jisuanqi();<br> <br> }
}
H. 三、课程设计内容 题目:编写简易计算器 仿照Windows自带的计算器编写一个Java简易计算器,
public class PrivateOverride {
private void f(){
System.out.println("private f()");
}
public static void main(String[] args){
PrivateOverride po = new Derived();
po.f();
}
}
class Derived extends PrivateOverride{
public void f(){
System.out.println("public f()");
}
}
I. JAVA图形用户界面设计一个简易计算器
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao="";
public YunSuan()
{
jf=new JFrame("加减乘除运算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");
}
public void Jiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args[])
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}