volcano 1 year ago
parent 3349281e93
commit c70a79b85f

@ -0,0 +1,14 @@
public class Fu implements MathComputer{
@Override
public void handle(Computer data) {
String s="";
double r=data.computerDataItem();
data.tempResult.push(r);
r=-r;
s=HandleEvent.get(r);
data.dataItem.removeAllElements();
for (int i = 0; i < s.length(); i++) {
data.dataItem.push(s.charAt(i));
}
}
}

@ -0,0 +1,13 @@
import java.awt.event.ActionEvent;
public class HandleBack extends HandleEvent{
public HandleBack(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
window.computer.backspace();
show();
}
}

@ -0,0 +1,13 @@
import java.awt.event.ActionEvent;
public class HandleClear extends HandleEvent{
public HandleClear(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
window.computer.init();
show();
}
}

@ -0,0 +1,21 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
public class HandleDight extends HandleEvent{
public HandleDight(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b=(JButton) e.getSource();
String btnName=b.getText().trim();//去除前后空白区
char dight=btnName.charAt(0);
window.computer.setDataItem(dight);
show();
if(dight=='.'){
String s=get(window.computer.computerDataItem());
window.showDataItem.setText(" "+s+".");
}
}
}

@ -0,0 +1,23 @@
import java.awt.event.ActionEvent;
public class HandleEquation extends HandleEvent{
public HandleEquation(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
String s ="";
if(window.computer.computerDataItem()==window.computer.getResult()){
if(window.computer.getTempResult()==-window.computer.getResult())
s=" "+window.computer.getTempResult()+" 取反为 "+window.computer.getResult();
if(1/window.computer.getTempResult()==window.computer.getResult())
s=" "+window.computer.getTempResult()+" 倒数为 "+window.computer.getResult();
if(Math.sqrt(window.computer.getTempResult())==window.computer.getResult())
s=" "+window.computer.getTempResult()+" 开方为 "+window.computer.getResult();
}else{
s=" "+window.computer.getTempResult()+" "+window.computer.getOperator()+
" "+window.computer.computerDataItem()+" = "+window.computer.getResult();}
window.computerProcess.append("\n"+s);
// show();
}
}

@ -0,0 +1,31 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HandleEvent implements ActionListener {
CalculatorWindow window;
public HandleEvent(CalculatorWindow window) {
this.window = window;
}
public void show(){
window.resultShow.setText(" = "+get(window.computer.getResult()));
window.showTempResult.setText(get(window.computer.getTempResult())+" ");
window.showOperator.setText(" "+window.computer.getOperator()+" ");
window.showDataItem.setText(" "+get(window.computer.computerDataItem()));
}
public static String get(double r){//返回浮点数的串表示
String s="";
Double d=new Double(r);
long n=d.longValue();//得到r的整数部分
if(Math.abs(r-n)>0)//取绝对值
s=""+r;
else
s=""+n;//小数部分为0则省略
return s;
}
@Override
public void actionPerformed(ActionEvent e) {
}
}

@ -0,0 +1,42 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
public class HandleFile extends HandleEvent{
public HandleFile(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==window.copyBtn){
window.computerProcess.copy();
}
if(e.getSource()==window.clearTextBtn){
window.computerProcess.setText(null);
}
if(e.getSource()==window.saveBtn){
JFileChooser chooser=new JFileChooser();
int state=chooser.showSaveDialog(null);
File file =chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION){
try {
String content=window.computerProcess.getText();
StringReader reader=new StringReader(content);
BufferedReader in=new BufferedReader(reader);
FileWriter fw=new FileWriter(file);
BufferedWriter out=new BufferedWriter(fw);
String s=null;
while ((s=in.readLine())!=null){
out.write(s);
out.newLine();
}
in.close();
out.close();
}catch (IOException exception){
}
}
}
}
}

@ -0,0 +1,13 @@
import java.awt.event.ActionEvent;
public class HandleFu extends HandleEvent{
public HandleFu(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
window.computer.initMath(new Fu());
show();
}
}

@ -0,0 +1,18 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
public class HandleOperator extends HandleEvent{
public HandleOperator(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b=(JButton) e.getSource();
String btnName;
btnName=b.getText().trim();//去除前后空白区
char operator=btnName.charAt(0);
window.computer.setOperator(operator);
show();
}
}

@ -0,0 +1,13 @@
import java.awt.event.ActionEvent;
public class HandleReciprocal extends HandleEvent{
public HandleReciprocal(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
window.computer.initMath(new Reciprocal());
show();
}
}

@ -0,0 +1,13 @@
import java.awt.event.ActionEvent;
public class HandleSqrt extends HandleEvent{
public HandleSqrt(CalculatorWindow window) {
super(window);
}
@Override
public void actionPerformed(ActionEvent e) {
window.computer.initMath(new Sqrt());
show();
}
}

@ -0,0 +1,3 @@
public interface MathComputer {
public void handle(Computer data);
}

@ -0,0 +1,14 @@
public class Reciprocal implements MathComputer{
@Override
public void handle(Computer data) {
String s="";
double r= data.computerDataItem();
data.tempResult.push(r);
r=1/r;
s=HandleEvent.get(r);
data.dataItem.removeAllElements();
for (int i = 0; i < s.length(); i++) {
data.dataItem.push(s.charAt(i));
}
}
}

@ -0,0 +1,15 @@
public class Sqrt implements MathComputer{
@Override
public void handle(Computer data) {
String s="";
double r=data.computerDataItem();
data.tempResult.push(r);
r=Math.sqrt(r);
s=HandleEvent.get(r);
data.dataItem.removeAllElements();
for (int i = 0; i < s.length(); i++) {
data.dataItem.push(s.charAt(i));
}
}
}

@ -0,0 +1,5 @@
public class test {
public static void main(String[] args) {
CalculatorWindow cw=new CalculatorWindow();
}
}

@ -1 +0,0 @@
测试git上传。。。
Loading…
Cancel
Save