簡易計算器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;
}
}