主题:[讨论]帮忙怎么做这个计算器用JAVA语言
asdsk
[专家分:0] 发布于 2009-04-27 19:26:00
编写程序完成简单计算器的功能:加、减、乘、除,程序界面如下:
Number1、Number2、Result 是三个JLabel 控件,它们后面的是三个 JTextField控件,用来输入操作数并显示运算结果;窗体下边是四个JButton控件Add、Subtract、Multiply、Divide,分别代表加、减、乘、除运算,点击是进行相应的运算并显示结果;窗体上边的菜单完成同样的功能。
[em3]
回复列表 (共3个回复)
沙发
chwen822 [专家分:1540] 发布于 2009-04-29 21:56:00
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.math.*;
import java.util.*;
/**
* <p>Title: 用户应用界面计算器</p>
* <p>Description: 包含简易计算与功能更加强大的科学计算</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: 四川大学×××学院</p>
* @author 陈文聪
* @version 1.0
*/
public class Calculat {
static JTextField show;//设置输入区,显示区
static double opNum = 0;//存放操作数
static String opChar = "=";//存放操作符
static boolean flag = true;//设置标志
public static void main(String[] args) {
new EasyFrame();//产生首界面——简易计算器
}
}
详情点击
[url=http://qxbs.blog.hexun.com/27923364_d.html]http://qxbs.blog.hexun.com/27923364_d.html[/url]
是我曾经的一个课程设计
板凳
asdsk [专家分:0] 发布于 2009-04-30 11:09:00
谢谢诶
3 楼
wangzhezichuan [专家分:0] 发布于 2009-04-30 16:23:00
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Computer extends JFrame implements ActionListener{
private JTextField Number1;
private JTextField Number2;
private JTextField Result;
private JLabel market;//选择运算符
private JLabel equale;//==
private JButton BResult;
private JButton Add;
private JButton Subtract;
private JButton Multiply;
private JButton Divide;
public Computer(){
this.setSize(350, 180);
this.setTitle("计算器");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initGUI();
}
public void initGUI(){
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
Number1 = new JTextField(8);
Number2 = new JTextField(8);
Result = new JTextField(8);
Result.setText("0.0");
market = new JLabel(" ");
equale = new JLabel("=");
BResult = new JButton("Result");
p1.add(Number1);
p1.add(market);
p1.add(Number2);
p1.add(equale);
p1.add(Result);
p1.add(BResult);
BResult.addActionListener(this);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER,25,10));
Add = new JButton("+");
Subtract = new JButton("-");
Multiply = new JButton("*");
Divide = new JButton("/");
p2.add(Add);
p2.add(Subtract);
p2.add(Multiply);
p2.add(Divide);
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
Add.addActionListener(this);
Subtract.addActionListener(this);
Multiply.addActionListener(this);
Divide.addActionListener(this);
}
public void go(){
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(Add)){
market.setText("+");
}
if(e.getSource().equals(Subtract)){
market.setText("-");
}
if(e.getSource().equals(Multiply)){
market.setText("*");
}
if(e.getSource().equals(Divide)){
market.setText("/");
}
if(e.getSource().equals(BResult)){
double n1 = Double.parseDouble(Number1.getText());
double n2 = Double.parseDouble(Number2.getText());
if(market.getText().equals("+")){
Result.setText(String.valueOf(n1+n2));
}
if(market.getText().equals("-")){
Result.setText(String.valueOf(n1-n2));
}
if(market.getText().equals("*")){
if(Number2.getText().equals(0))
Result.setText("Infinity");
else
Result.setText(String.valueOf(n1*n2));
}
if(market.getText().equals("/")){
Result.setText(String.valueOf(n1/n2));
}
}
}
public static void main(String args[]){
Computer c = new Computer();
c.go();
}
}
我来回复