编写java application程序实现一个简易计算器
//布局没有调整,需要你自己去调整代码如下:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class JieMian extends JFrame implements ActionListener{JTextField c1;JTextField c2;JLabel c;JLabel equal=new JLabel("=");JLabel result;JLabel choose=new JLabel("请选择");JButton add=new JButton("+");JButton sub=new JButton("-");JButton mul=new JButton("×");JButton div=new JButton("÷");JButton cal=new JButton("计算");JButton clear=new JButton("清除");public JieMian(){setLayout(new FlowLayout());c1=new JTextField(5);c2=new JTextField(5);c=new JLabel("+");result=new JLabel("");add(c1);add(c);add(c2);add(equal);add(result);add(choose);add(add);add(sub);add(mul);add(div);add(cal);add(clear);add.addActionListener(this);sub.addActionListener(this);mul.addActionListener(this);div.addActionListener(this);cal.addActionListener(this);clear.addActionListener(this);setVisible(true);pack();}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==add){c.setText("+");}else if(e.getSource()==sub){c.setText("-");}else if(e.getSource()==mul){c.setText("×");}else if(e.getSource()==div){c.setText("÷");}else if(e.getSource()==cal){double cc1,cc2;try{cc1=Double.parseDouble(c1.getText());}catch(Exception ex){cc1=0;}try{cc2=Double.parseDouble(c2.getText());}catch(Exception ex){cc2=0;}if(c.getText()=="+"){result.setText(String.valueOf(cc1+cc2));}else if(c.getText()=="-"){result.setText(String.valueOf(cc1-cc2));}else if(c.getText()=="×"){result.setText(String.valueOf(cc1*cc2));}else if(c.getText()=="÷"){if(cc2!=0){result.setText(String.valueOf(cc1/cc2));}else{result.setText("NAN");}}}else if(e.getSource()==clear){c1.setText("");c2.setText("");result.setText("");}}public static void main(String args[]){JieMian jiemian=new JieMian();}}
下面的例子实现了界面,你可以实现ActionListener接口,为对应按钮添加事件,实现计算器的计算功能!~
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
JFrame f = new JFrame("计算器");
Container c = f.getContentPane();
// 菜单栏
JMenuBar menubar = new JMenuBar();
JMenu menu1 = new JMenu("查看");
JMenu menu2 = new JMenu("编辑");
JMenu menu3 = new JMenu("帮助");
JMenuItem item1 = new JMenuItem("标准型");
JMenuItem item2 = new JMenuItem("复制");
JMenuItem item3 = new JMenuItem("粘贴");
JMenuItem item4 = new JMenuItem("关于计算机");
// 显示器面板
JPanel panel1 = new JPanel();
JTextField label = new JTextField(25);
// 按钮面板
JPanel panel2 = new JPanel();
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
// 按钮内容
String[] cal = {"MC", "MR", "MS", "M+", "M-", "←", "CE", " C ", " ± ", "√", " 7 ", " 8 ", " 9 ", " / ", "%", " 4 ",
" 5 ", " 6 ", " * ", "1/x", " 1 ", " 2 ", " 3 ", "-", "=", "0", ".", "+", };
private JButton[] bs = new JButton[28];
// 建立两个BigDecimal为了精准运算
BigDecimal a = new BigDecimal("0");
BigDecimal b = new BigDecimal("0");
public void init() {
//为JFrame设置容器
c = f.getContentPane();
//设置JFrame布局方式
c.setLayout(new BorderLayout());
// 初始化界面
label.setEditable(false);
label.setText("0");
label.setHorizontalAlignment(JTextField.RIGHT);
panel1.add(label);
panel2.setLayout(gb);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
for (int i = 0; i < bs.length; i++) {
bs[i] = new JButton(cal[i]);
int x = 1;
String actionCommand = bs[i].getActionCommand().trim();
if("0".equals(actionCommand)) {
gbc.gridwidth = 2;
x = 2;
} else if("=".equals(actionCommand)) {
gbc.gridheight = 2;
}
gb.setConstraints(bs[i], gbc);
panel2.add(bs[i]);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx += x;
if((i+1)%5 == 0) {
gbc.gridx = 0;
gbc.gridy += 1;
}
}
menu1.add(item1);
menu2.add(item2);
menu2.add(item3);
menu3.add(item4);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
c.add(panel1, BorderLayout.NORTH);
c.add(panel2, BorderLayout.CENTER);
f.setJMenuBar(menubar);
f.pack();
f.setResizable(false);
f.setLocation(400, 350);
f.setVisible(true);
//关闭窗口时,停止程序
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] arg) {
new Calculator().init();
}
}
Java计算器 源代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**********************Java计算器 主类*********************/
public class SunnyCalculator implements ActionListener {
JFrame f;
JMenu mEdit;
JMenu mView;
JMenu mHelp;
JMenuItem mCopy;
JMenuItem mPaste;
JTextField tResult;
JButton bNumber;
JButton bOperator;
JButton bOther;
JButton bM;
boolean isDouble=false;//是否为实数
int opFlag=-1;
static double t1=0,t2=0,t3=0,result=0;
static int opflag1=-1,opflag2=-1,flag=0,resflag=1;
int preOp,currentOp=0;//标准位
double op1=0,op2=0;//操作数
double n3;
StringBuffer buf=new StringBuffer(20);
StringBuffer copyBoard=new StringBuffer(20);//剪贴板
StringBuffer memory=new StringBuffer(20);//M系列
StringBuffer str=new StringBuffer();
//Java计算器 构造器
public SunnyCalculator()
{
f = new JFrame("Sunny计算器_杨梅树的盔甲");
Container contentPane = f.getContentPane();
/**************************Java计算器 菜单的创建*****************************/
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mEdit = new JMenu("编辑(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy = new JMenuItem("复制(C)");
mEdit.add(mCopy);
mPaste = new JMenuItem("粘贴(P)");
mEdit.add(mPaste);
mView = new JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new JMenuItem("标准型"));
mView.add(new JMenuItem("科学型"));
mView.addSeparator();
mView.add(new JMenuItem("查看分组"));
mHelp = new JMenu("帮助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new JMenuItem("帮助主题"));
mHelp.addSeparator();
mHelp.add(new JMenuItem("关于计算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new BorderLayout());
JPanel pTop = new JPanel();
tResult = new JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel pBottom = new JPanel();
pBottom.setLayout(new BorderLayout());
JPanel pLeft = new JPanel();
pLeft.setLayout(new GridLayout(5,1,3,3));
bM = new JButton(" ");
bM.setEnabled(false);
pLeft.add(bM);
/*************************Java计算器 功能键定义***************************/
bOther = new JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("M+");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
pBottom.add(pLeft,BorderLayout.WEST);
JPanel pRight = new JPanel();
pRight.setLayout(new BorderLayout());
JPanel pUp = new JPanel();
pUp.setLayout(new GridLayout(1,3,3,0));
bOther = new JButton("BackSpace");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,0,3,5));
pUp.add(bOther);
bOther = new JButton("CE");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
bOther = new JButton("C");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
/***************************Java计算器 数字键盘区定义**************************/
JPanel pDown = new JPanel();
pDown.setLayout(new GridLayout(4,5,3,2));
bNumber = new JButton("7");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("8");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("9");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("/");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bOperator = new JButton("sqrt");
bOperator.addActionListener(this);
bOperator.setForeground(Color.red);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bNumber = new JButton("4");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
bNumber.setHorizontalTextPosition(JButton.LEFT);
pDown.add(bNumber);
bNumber = new JButton("5");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("6");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("*");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("%");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bNumber = new JButton("1");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("2");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("3");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("-");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("1/x");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
pDown.add(bOperator);
bNumber = new JButton("0");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("+/-");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton(".");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("+");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("=");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
pRight.add(pUp,BorderLayout.NORTH);
pRight.add(pDown,BorderLayout.SOUTH);
pBottom.add(pRight,BorderLayout.EAST);
contentPane.add(pBottom,BorderLayout.SOUTH);
f.setSize(new Dimension(320,256));
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
/************************Java计算器 计算方法区***************************/
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("复制(C)"))
{
String temp = tResult.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mPaste.setEnabled(true);
}
else if(s.equals("粘贴(p)"))
{
tResult.setText(copyBoard.toString());
}
else if(s.equals("CE"))
{
//如果是CE则清除文本框
tResult.setText("0.");
}
else if(s.equals("BackSpace"))
{
if(!tResult.getText().trim().equals("0."))
{
//如果文本框中有内容
if(str.length()!=1 && str.length()!=0)
{
tResult.setText(str.delete(str.length()-1,str.length()).toString());
}
else
{
tResult.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(tResult.getText().trim());
}
else if(s.equals("C"))
{
//如果是C删除当前计算
tResult.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
}
else if(s.equals("MC"))
{
//如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
}
else if(s.equals("MR"))
{
//如果按键为MR则恢复缓冲区的数到文本框
tResult.setText(memory.toString());
}
else if(s.equals("MS"))
{
//如果按键为MS则将文本框的数存入缓冲区
String s1 = tResult.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
}
else if(s.equals("M+"))
{
//如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = tResult.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp += Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
}
else if(s.equals("1/x"))
{
//如果按键为1/x则将文本框中的数据为它的倒数
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText(""+1/dtemp);
}
else if(s.equals("sqrt"))
{
//如果按键为sqrt则将文本框中的内容求平方根
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText(""+Math.sqrt(dtemp));
}
else if(s.equals("+"))
{
str.setLength(0);
if(currentOp==0)
{
preOp = currentOp = 1;
op2 = 0;
tResult.setText(""+op1);
}
else
{
currentOp = preOp;
preOp = 1;
switch(currentOp){
case 1:
op1 += op2;
tResult.setText(""+op1);
break;
case 2:
op1 -= op2;
tResult.setText(""+op1);
break;
case 3:
op1 *= op2;
tResult.setText(""+op1);
break;
case 4:
op1 /= op2;
tResult.setText(""+op1);
break;
}
}
}
else if(s.equals("-")){
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=2;//op1=op2;op2=0;
tResult.setText(""+op1);
}
else
{
currentOp =preOp;
preOp =2;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("*"))//*
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=3;//op1=op2;op2=1;
tResult.setText(""+op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =3;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("/"))// /
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=4;//op2=1;
tResult.setText(""+op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =4;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("="))// =
{
if(currentOp==0)
{
str.setLength(0);
tResult.setText(""+op2);
}
else
{
str.setLength(0);
currentOp =preOp;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText("&qu
英民盖衡: 使用java编写android的app需要下载adt 这属于ecilpse的一种特殊插件,使用adt就可进行app的编程了.编写后生成apk放入手机中直接进行安装即可
沙湾县18734409373: 编写一个java Application程序,程序有个点Pointer类 - ?
英民盖衡: public class Pointer { private int x;//坐标 像素 所以是int private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }
沙湾县18734409373: java有点混乱 - ?
英民盖衡: 1、applet一般用于B/S页面上作为插件式的开发,而application主要是桌面应用程序的开 发,application是不能用Jsp加载的 2、Application和Java Applet的区别. Java语言是一种半编译半解释的语言.Java的用户程序分 为两类:Java ...
沙湾县18734409373: 如何用Visual Studio编译Java源代码 - ?
英民盖衡: 打开Visual Studio,建立任意工程. 把工程文件自动生成的Class1.cs等文件删掉. 向工程文件中增加一个文本文件,命名为Application.java 双击Application.java文件编辑java源代码: 在工程文件中增加一个文本文件,命名为:Compile.bat Tools -> External Tools... 增加一个entity如下,命名为 Javac 下面,将Compile.bat和Application.java这两个文件编写完整: 执行:Tools -> Javac
沙湾县18734409373: 编一个java程序,用i/o的方式把一个磁盘的文件复制到另一个磁盘 - ?
英民盖衡: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileTest { /** * @param args *...
沙湾县18734409373: 如何用eclipse创建一个Java工程 - ?
英民盖衡: 思路为:先新建一个java工程,然后在该工程下新建一个类,在该类中输入代码运行即可.方法如下:1. 在package explorer 右键,new - java project2. 右键src,new class,输入一个名字3. 在那个class里面的main方法里面输入一个System.out.print("zhidao");4. 右键那个java类,run as java application5. 控制台输出hello world就意味着你的这个java程序成功运行了
沙湾县18734409373: Java应用程序?
英民盖衡: 这个程序你填几个数都一样 每个数要以空格隔开public static void main(String[] args) { List list=new ArrayList(); int max; int min; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { StringTokenizer st=new ...
沙湾县18734409373: java工程师需要掌握哪些技能 - ?
英民盖衡: 1、语法:必须比较熟悉,在写代码的时候,IDE(Integrated Development Environment,集成开发环境)的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误,并且知道任何修正.2、命令:必须熟悉JDK(Java Development Kit...
沙湾县18734409373: 编写一个完整的Java application程序,程序功能为:在屏幕上输出“明年是2010年”的字符串信息?
英民盖衡: import javax.swing.*; import java.awt.Graphics; public class JavaAppStart { public static void main(String args[]) { new JavaAppFrame().setVisible(true); } } class JavaAppFrameextends JFrame { public JavaAppFrame() { super("新年快乐");...
沙湾县18734409373: 编写一个字符界面的java application程序,接受用户输入的10个整数的最大值 - ?
英民盖衡: 代码如下:import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.StringTokenizer; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing....