100分求java科学计算器

cpucooler 2004-12-14 11:30:21
哪位有科学计算器

麻烦发给我

luckydog2008@msn.com


谢谢
...全文
275 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
beakham 2004-12-20
  • 打赏
  • 举报
回复
接上面
private class ClearAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("C")) {
result.setText("0");
lastCommand = "=";
start = true;
calResult = 0;
}

if (command.equals("CE")) {
result.setText("0");
start = true;
}
}
}

private class InvertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("1/x")) {
double temp = Double.parseDouble(result.getText());
double invertTemp = 1 / temp;
result.setText("" + invertTemp);
start = true;
}

if (command.equals("sqrt")) {
double temp = Double.parseDouble(result.getText());
double sqrtTemp = java.lang.Math.sqrt(temp);
result.setText("" + sqrtTemp);
start = true;
}
}
}

private class MemoryAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();

if (command.equals("MS")) {
memoryResult = Double.parseDouble(result.getText());
memoryLabel.setText("M");
}

if (command.equals("MR")) {
result.setText("" + memoryResult);
}

if (command.equals("MC")) {
memoryResult = 0;
memoryLabel.setText("");
}

if (command.equals("M+")) {
double temp = Double.parseDouble(result.getText());
memoryResult = memoryResult + temp;
}
}
}

private class BackAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();

if (command.equals("Backspace")) {
String temp = result.getText();
//System.out.println(temp);
int length = temp.length();
if (length > 1) {
temp = temp.substring(0, length - 1);
//System.out.println(temp);
result.setText(temp);
} else {
result.setText("0");
}
}
}
}

private class NegationAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();

if (command.equals("+/-")) {
if (result.getText().equals("0")
|| result.getText().equals("0.0")
|| result.getText().equals("0.")) {

} else {
String t = result.getText();
t = t.substring(0, 1);
if (t.equals("-")) {
String t1 = result.getText();
t1 = t1.substring(1, t1.length());
result.setText(t1);
} else {
String temp = result.getText();
temp = "-" + temp;
result.setText(temp);
}

}
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+"))
calResult += x;
else if (lastCommand.equals("-"))
calResult -= x;
else if (lastCommand.equals("*"))
calResult *= x;
else if (lastCommand.equals("/"))
calResult /= x;
else if (lastCommand.equals("="))
calResult = x;

result.setText("" + calResult);

}

//private JPanel panel;
private double calResult;
private JTextField result;
private JButton zero;
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
private JButton sqrt;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton point;
private JButton MC;
private JButton MR;
private JButton MS;
private JButton Mplus;
private JButton negation;
private JButton backspace;
private JButton CE;
private JButton C;
private JButton invert;
private JButton percent;
private boolean start;
private String lastCommand;
private double memoryResult;
private double copyResult;
private JLabel memoryLabel;

private AboutDialog dialog;

}

/*
class CalculatorPanel extends JPanel {

private JPanel panel;
private JLabel result;
private JButton zero;
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
private JButton sqrt;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton point;
private JButton MC;
private JButton MR;
private JButton MS;
private JButton Mplus;
private JButton negation;
private JButton backspace;
private JButton CE;
private JButton C;
private JButton invert;
private JButton percent;

public CalculatorPanel() {
//setLayout(new BorderLayout());

panel = new JPanel();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);

result = new JLabel();
result.setText("0.");

GridBagConstraints constraints = new GridBagConstraints();

//do not resize the component
constraints.fill = GridBagConstraints.NONE;

//determine where to place the component
constraints.anchor = GridBagConstraints.CENTER;

constraints.weightx = 0;
constraints.weighty = 0;

add(result, constraints, 0, 0, 6, 1);

add(backspace, constraints, 1, 2, 2, 1);
add(CE, constraints, 3, 2, 2, 1);
add(C, constraints, 5, 2, 1, 1);

add(MC, constraints, 0, 3, 1, 1);
add(seven, constraints, 1, 3, 1, 1);
add(eight, constraints, 2, 3, 1, 1);
add(nine, constraints, 3, 3, 1, 1);
add(divide, constraints, 4, 3, 1, 1);
add(sqrt, constraints, 5, 3, 1, 1);

add(MR, constraints, 0, 4, 1, 1);
add(four, constraints, 1, 4, 1, 1);
add(five, constraints, 2, 4, 1, 1);
add(six, constraints, 3, 4, 1, 1);
add(multiply, constraints, 4, 4, 1, 1);
add(percent, constraints, 5, 4, 1, 1);

add(MS, constraints, 0, 5, 1, 1);
add(one, constraints, 1, 5, 1, 1);
add(two, constraints, 2, 5, 1, 1);
add(three, constraints, 3, 5, 1, 1);
add(minus, constraints, 4, 5, 1, 1);
add(invert, constraints, 5, 5, 1, 1);

add(Mplus, constraints, 0, 6, 1, 1);
add(zero, constraints, 1, 6, 1, 1);
add(negation, constraints, 2, 6, 1, 1);
add(point, constraints, 3, 6, 1, 1);
add(plus, constraints, 4, 6, 1, 1);
add(equal, constraints, 5, 6, 1, 1);

}

public void add(
Component c,
GridBagConstraints constraints,
int x,
int y,
int w,
int h) {
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
add(c, constraints);

}
}
*/

class AboutDialog extends JDialog {
public AboutDialog(JFrame owner) {
super(owner, "About Jalculator", true);
Container contentPane = getContentPane();

contentPane.add(
new JLabel(
"<HTML><H1><I>Jalculator</I></H1><HR>" + "By ZhuHua</HTML>"),
BorderLayout.CENTER);

JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setVisible(false);
}
});

JPanel panel = new JPanel();
panel.add(ok);
contentPane.add(panel,BorderLayout.SOUTH);

setSize(250,150);
}
}
beakham 2004-12-20
  • 打赏
  • 举报
回复
我也做了一个,基本仿照WINDOWS计算器
/*
* Created on 2004-11-18
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package basic;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
* @author zhuhua
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Jalculator {
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class CalculatorFrame extends JFrame {

public CalculatorFrame() {
setTitle("My Calculator");
setSize(320, 250);
Container contentPane = getContentPane();
//CalculatorPanel panel = new CalculatorPanel();
//contentPane.add(panel);
GridBagLayout layout = new GridBagLayout();
contentPane.setLayout(layout);

result = new JTextField(20);
//result.setColumns(20);
result.setText("0.");
result.setEditable(false);

start = true;

InsertAction insert = new InsertAction();
CommandAction command = new CommandAction();
ClearAction clear = new ClearAction();
InvertAction invertAction = new InvertAction();
MemoryAction memory = new MemoryAction();
BackAction back = new BackAction();
NegationAction negationAction = new NegationAction();

calResult = 0;
memoryResult = 0;
copyResult = 0;

lastCommand = "=";

zero = new JButton("0");
//zero.setSize(20,20);
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
plus = new JButton("+");
minus = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
MC = new JButton("MC");
MR = new JButton("MR");
MS = new JButton("MS");
Mplus = new JButton("M+");
point = new JButton(".");
equal = new JButton("=");
invert = new JButton("1/x");
percent = new JButton("%");
sqrt = new JButton("sqrt");
backspace = new JButton("Backspace");
CE = new JButton("CE");
C = new JButton("C");
negation = new JButton("+/-");
//negation.setSize(10,20);
memoryLabel = new JLabel();

GridBagConstraints constraints = new GridBagConstraints();

//do not resize the component
constraints.fill = GridBagConstraints.NONE;

//determine where to place the component
constraints.anchor = GridBagConstraints.CENTER;

constraints.weightx = 0;
constraints.weighty = 0;

add(result, constraints, 0, 0, 6, 1);

add(memoryLabel, constraints, 0, 2, 1, 1);
add(backspace, constraints, 1, 2, 2, 1);
add(CE, constraints, 3, 2, 2, 1);
add(C, constraints, 5, 2, 1, 1);

add(MC, constraints, 0, 3, 1, 1);
add(seven, constraints, 1, 3, 1, 1);
add(eight, constraints, 2, 3, 1, 1);
add(nine, constraints, 3, 3, 1, 1);
add(divide, constraints, 4, 3, 1, 1);
add(sqrt, constraints, 5, 3, 1, 1);

add(MR, constraints, 0, 4, 1, 1);
add(four, constraints, 1, 4, 1, 1);
add(five, constraints, 2, 4, 1, 1);
add(six, constraints, 3, 4, 1, 1);
add(multiply, constraints, 4, 4, 1, 1);
add(percent, constraints, 5, 4, 1, 1);

add(MS, constraints, 0, 5, 1, 1);
add(one, constraints, 1, 5, 1, 1);
add(two, constraints, 2, 5, 1, 1);
add(three, constraints, 3, 5, 1, 1);
add(minus, constraints, 4, 5, 1, 1);
add(invert, constraints, 5, 5, 1, 1);

add(Mplus, constraints, 0, 6, 1, 1);
add(zero, constraints, 1, 6, 1, 1);
add(negation, constraints, 2, 6, 1, 1);
add(point, constraints, 3, 6, 1, 1);
add(plus, constraints, 4, 6, 1, 1);
add(equal, constraints, 5, 6, 1, 1);

one.addActionListener(insert);
two.addActionListener(insert);
three.addActionListener(insert);
four.addActionListener(insert);
five.addActionListener(insert);
six.addActionListener(insert);
seven.addActionListener(insert);
eight.addActionListener(insert);
nine.addActionListener(insert);
zero.addActionListener(insert);
point.addActionListener(insert);

plus.addActionListener(command);
minus.addActionListener(command);
multiply.addActionListener(command);
divide.addActionListener(command);
equal.addActionListener(command);
percent.addActionListener(command);
//backspace.addActionListener(command);

C.addActionListener(clear);
CE.addActionListener(clear);

invert.addActionListener(invertAction);
sqrt.addActionListener(invertAction);

MC.addActionListener(memory);
MS.addActionListener(memory);
MR.addActionListener(memory);
Mplus.addActionListener(memory);

backspace.addActionListener(back);

negation.addActionListener(negationAction);

JMenuBar mBar = new JMenuBar();
setJMenuBar(mBar);
JMenu editMenu = new JMenu("编辑");
JMenuItem copyItem = new JMenuItem("复制");
copyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
copyResult = Double.parseDouble(result.getText());
}
});

JMenuItem pasteItem = new JMenuItem("粘贴");
pasteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
result.setText("" + copyResult);
}
});

editMenu.add(copyItem);
editMenu.add(pasteItem);

JMenu viewMenu = new JMenu("查看");
JMenu helpMenu = new JMenu("帮助");
//JMenuItem helpItem = new JMenuItem("帮助主题");
JMenuItem aboutItem = new JMenuItem("关于计算器");
//helpMenu.add(helpItem);
helpMenu.add(aboutItem);

mBar.add(editMenu);
mBar.add(viewMenu);
mBar.add(helpMenu);

aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(dialog == null)
dialog = new AboutDialog(CalculatorFrame.this);
dialog.show();
}
});

}

public void add(
Component c,
GridBagConstraints constraints,
int x,
int y,
int w,
int h) {
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
getContentPane().add(c, constraints);

}

private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
result.setText("");
start = false;
}
result.setText(result.getText() + input);
}
}

private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();

if (start) {
if (command.equals("%")) {
double temp = Double.parseDouble(result.getText());
double percentTemp = temp / 100;
temp = temp * percentTemp;
result.setText("" + temp);
start = true;
} else {
lastCommand = command;
}
} else {

calculate(Double.parseDouble(result.getText()));
lastCommand = command;
start = true;
}
}
}

shanshanliu 2004-12-20
  • 打赏
  • 举报
回复
做得不错
不过好像不能实现键盘操作吧
另外,你在按钮定义时可采用循环数组的形式,会大大减少代码行
使用令牌即可
chen9521 2004-12-20
  • 打赏
  • 举报
回复
哗,楼上的朋友,是很好用啊。我把源码保存了,你不会介意吧~?
virgin_killer 2004-12-20
  • 打赏
  • 举报
回复
长了点儿,但功能很全的哦
virgin_killer 2004-12-20
  • 打赏
  • 举报
回复
else if ( getSource == minus )

{
display.setText("减去");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else
tempNumber = numberCalculated;
operator = '-' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
else if( getSource == multiply )
{
display.setText("乘以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else
tempNumber = numberCalculated;
operator = '*' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
else if( getSource == divide )
{
display.setText("除以");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else
tempNumber = numberCalculated;
operator = '/' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
else if( getSource == sqrt )
{

display.setText("开方");
if(operaPressed)
calculatAction();
if(!operaPressed && !equalPressed)
numberCalculated = tempNumber ;
operator = 's' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
else if( getSource == equals )
{
calculatAction();
display.setText(""+numberCalculated);
equalPressed = true;
numberPressed = false;
operaPressed = false;
tempNumber = 0f;
dotFactorial = 0;
}
else if( getSource == MEM )
{
display.setText("保存这个数");
numberStored = numberCalculated;
operaPressed = false;
numberPressed = true;
equalPressed = false;
displayString = " ";
operator = ' ';
dotPressed = false;
tempNumber = 0;
numberCalculated = 0;
numAfterOperPressed = 0;
dotFactorial = 0;
}
else if( getSource == GEM )
{
display.setText("取出保存的数");
tempNumber = numberStored;
}
else if( getSource == Clear )
{

operaPressed = false;
numberPressed = true;
equalPressed = false;
displayString = " ";
display.setText("0");
operator = ' ';
dotPressed = false;
tempNumber = 0;
numberCalculated = 0;
numAfterOperPressed = 0;
dotFactorial = 0;
}
else if( getSource == dot )
{
dotPressed = true;
displayString += '.';
display.setText(displayString);

}
}
public void calculatAction()
{
switch(operator)
{
case '+' :
numberCalculated = numberCalculated + tempNumber;
break;
case '-' :
numberCalculated = numberCalculated - tempNumber;
break;
case '*' :
numberCalculated = numberCalculated * tempNumber;
break;
case '/' :
numberCalculated = numberCalculated / tempNumber;
break;
case 's' :
numberCalculated = (float) Math.sqrt(numberCalculated);
}
}
void actionAfterPressNum (int i)
{
if(numberPressed)
{
displayString += i;
display.setText(displayString);
}
else
{
displayString = Integer.toString(i);
display.setText("" + i);
}
if (dotPressed && numberPressed)
{
tempNumber += i*Math.pow(10,-(++dotFactorial));
}
else
tempNumber = tempNumber*10 + i;
numberPressed = true;
}
public static void main(String args[])
{
Calculator cal = new Calculator();
}

}
virgin_killer 2004-12-20
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
boolean operaPressed = false;
boolean numberPressed = true;
boolean equalPressed = false;
int singleNumber;
String displayString = "";
int dotFactorial;
char operator;
boolean dotPressed = false;
float tempNumber;
float numAfterOperPressed;
float numberCalculated;
float numberStored; // store the number wihc maybe used later
private JButton sqrt;
private JButton divide;
private JButton MEM; // store the number which may be used later
private JButton GEM; // get the number which has been stroed
private JButton Clear; // reset the caculator,
private JButton seven7;
private JButton eight8;
private JButton nine9;
private JButton plus;
private JButton four4;
private JButton five5;
private JButton six6;
private JButton multiply;
private JButton one1;
private JButton two2;
private JButton three3;
private JButton minus;
private JButton zero0;
private JButton dot;
private JButton equals;
private JButton plus1;
private JPanel topPanel;
private JPanel backPanel;
private JTextField display;
public Calculator()
{
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(5,4));
sqrt = new JButton("sqrt");
topPanel.add(sqrt);
MEM = new JButton("MEM");
topPanel.add(MEM);
GEM = new JButton("GEM");
topPanel.add(GEM);
Clear = new JButton("C");
topPanel.add(Clear);
seven7 = new JButton("7");
topPanel.add(seven7);
eight8 = new JButton("8");
topPanel.add(eight8);
nine9 = new JButton("9");
topPanel.add(nine9);
plus = new JButton ("+");
topPanel.add(plus);
four4 = new JButton("4");
topPanel.add(four4);
five5 = new JButton("5");
topPanel.add(five5);
six6 = new JButton ("6");
topPanel.add(six6);
multiply = new JButton("*");
topPanel.add(multiply);
one1 = new JButton("1");
topPanel.add(one1);
two2 = new JButton("2");
topPanel.add(two2);
three3 = new JButton("3");
topPanel.add(three3);
minus = new JButton("-");
topPanel.add(minus);
zero0 = new JButton("0");
topPanel.add(zero0);
dot = new JButton(".");
topPanel.add(dot);
equals = new JButton("=");
topPanel.add(equals);
divide = new JButton("/");
topPanel.add(divide);
display = new JTextField(20);
backPanel = new JPanel();
backPanel.setLayout(new BorderLayout());
backPanel.setBackground(Color.red);
backPanel.add(display,BorderLayout.NORTH);
backPanel.add(topPanel,BorderLayout.CENTER);
getContentPane().add(backPanel);
setTitle("Calculator");
setSize(300,300);
setLocation(400,400);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
nine9.addActionListener(this);
eight8.addActionListener(this);
seven7.addActionListener(this);
six6.addActionListener(this);
five5.addActionListener(this);
four4.addActionListener(this);
three3.addActionListener(this);
two2.addActionListener(this);
one1.addActionListener(this);
zero0.addActionListener(this);
Clear.addActionListener(this);
dot.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
equals.addActionListener(this);
sqrt.addActionListener(this);
MEM.addActionListener(this);
GEM.addActionListener(this);
Clear.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object getSource = e.getSource();
if( getSource == nine9 )
{
actionAfterPressNum(9);
}
if( getSource == eight8 )
{
actionAfterPressNum(8);
}
if( getSource == seven7 )
{
actionAfterPressNum(7);
}
if( getSource == six6 )
{
actionAfterPressNum(6);
}
if( getSource == five5 )
{
actionAfterPressNum(5);
}
if( getSource == four4 )
{
actionAfterPressNum(4);
}
if( getSource == three3 )
{
actionAfterPressNum(3);
}
if( getSource == two2 )
{
actionAfterPressNum(2);
}
if( getSource == one1 )
{
actionAfterPressNum(1);
}
if( getSource == zero0 )
{
actionAfterPressNum(0);
}
if( getSource == plus )
{
display.setText("加上");
if(operaPressed)
calculatAction();
else if (!equalPressed)
numberCalculated = tempNumber;
else
tempNumber = numberCalculated;
operator = '+' ;
operaPressed = true;
numberPressed = false;
tempNumber = 0;
dotFactorial = 0;
}
zealVampire 2004-12-18
  • 打赏
  • 举报
回复
应该是要用到什么stack之类的来写的吧
如果要支持() [] {}这些的呢
御南 2004-12-18
  • 打赏
  • 举报
回复
我有个很简单的
classjava 2004-12-15
  • 打赏
  • 举报
回复
MyApp.java
/****** 本软件支持整型运算,实型运算,整型实型混合运算
及连续运算,是一款功能较强的计算器*******/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyApp extends JFrame//定义主类 MyApp
implements ActionListener//接口继承 常用事件处理
{
JButton Button_Add=new JButton("+"); //定义按钮“+”
JButton Button_Del=new JButton("-"); //定义按钮"-"
JButton Button_Div=new JButton("/"); //定义按钮"/"
JButton Button_Mul=new JButton("*"); //定义按钮"*"
JButton Button_Equ=new JButton("="); //定义按钮"="

JTextField TextField=new JTextField(10);//创建输入单行文本区域对象
JPanel Panel=new JPanel();
float Left,Right,Result; //保存数值
int Type;//运算类型标示
int temp;
//super("原始野人计算器");

public void actionPerformed(ActionEvent e)//对用户输入做出响应
{
if(e.getSource()==Button_Add)
{
Left=Float.parseFloat(TextField.getText()); //保存第一操作数
TextField.setText("");//窗口内容重填
Type=1;
}
if(e.getSource()==Button_Equ)
{
switch(Type)
{
case 1:
Right=Float.parseFloat(TextField.getText());//保存第二操作数
Result=Left+Right; //计算结果
temp=(int)Result;
if((float)temp==Result)
TextField.setText(Integer.toString(temp));
else
TextField.setText(Float.toString(Result));//输出结果
break;
case 2:
Right=Float.parseFloat(TextField.getText());//保存第二操作数
Result=Left-Right; //计算结果
temp=(int)Result;
if((float)temp==Result)
TextField.setText(Integer.toString(temp));
else
TextField.setText(Float.toString(Result));//输出结果
break;
case 3:
Right=Float.parseFloat(TextField.getText());//保存第二操作数
Result=Left*Right; //计算结果
temp=(int)Result;
if((float)temp==Result) //判断浮点数是否可以直接转换为整形数
TextField.setText(Integer.toString(temp));
else
TextField.setText(Float.toString(Result));//输出结果
break;
case 4:
Right=Float.parseFloat(TextField.getText());//保存第二操作数
Result=Left/Right; //计算结果
temp=(int)Result;
if((float)temp==Result)
TextField.setText(Integer.toString(temp));
else
TextField.setText(Float.toString(Result));//输出结果

break;
}
}
if(e.getSource()==Button_Del)
{
Left=Float.parseFloat(TextField.getText()); //保存第一操作数
TextField.setText("");//窗口内容重填
Type=2;
}
if(e.getSource()==Button_Mul)
{
Left=Float.parseFloat(TextField.getText()); //保存第一操作数
TextField.setText("");//窗口内容重填
Type=3;
}
if(e.getSource()==Button_Div)
{
Left=Float.parseFloat(TextField.getText()); //保存第一操作数
TextField.setText("");//窗口内容重填
Type=4;
}

}
public MyApp()//对对象初始化
{
super("原始野人计算器");
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
Button_Add.addActionListener(this);//事件监听
Button_Del.addActionListener(this);//事件监听
Button_Div.addActionListener(this);//事件监听
Button_Mul.addActionListener(this);//事件监听
Panel.setLayout(new FlowLayout());//流水式布局风格
Panel.add(Button_Add);//将Button_Add加入窗口
Panel.add(Button_Del);//将Button_Del加入窗口
Panel.add(Button_Div);//将Button_Div加入窗口
Panel.add(Button_Mul);//将Button_Mut加入窗口
Button_Equ.addActionListener(this);//事件监听
Panel.add(Button_Equ);//将Button_Equ加入窗口
Panel.add(TextField);//将TextField加入窗口
getContentPane().add(Panel);//将Panel加入窗口


}

public static void main(String args[])//定义主类中的main方法
{
System.out.println("Starting App");
MyApp f = new MyApp();
f.setSize(650,150);

f.show();
}
}

御南 2004-12-15
  • 打赏
  • 举报
回复
自己做嘛
superman421 2004-12-15
  • 打赏
  • 举报
回复
自己做了
cosio 2004-12-15
  • 打赏
  • 举报
回复
www.flying2008.com/bbs
wangtianyang 2004-12-15
  • 打赏
  • 举报
回复
我有个普通计算器

要吗?

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧