java计算器程序
Ⅰ 用java程序编写一个计算器
给你一个参考,希望不要被网络吞了当晚餐
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//删除
private JButton C = new JButton("C ");
//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args) {
new Calculator();//new计算器实例
}
public Calculator(){
//添加组件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注册各个组件监听事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注册四则运算监听
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注册数字监听事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已经是0,结果还是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格键,删除一个直到没有字符删除
String inputText = input.getText();
if(inputText.length() > 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有运算数字和符号
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE-->将文本框置为0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等号
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){//执行加法运算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//减法运算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法运算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法运算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
//格式化最终结果,保留两位小数点
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
Ⅱ 用JAVA编写一个简单的计算器,要求如下:
以下是上图计算器的代码:
packageComputer;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Stack;
importjavax.swing.JApplet;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
{
/**
*
*/
=1L;
privateJTextFieldtextField=newJTextField("请输入");
Stringoperator="";//操作
Stringinput="";//输入的式子
booleanflag=true;
//booleanflag1=true;
//booleanflag2=true;
publicvoidinit()//覆写Applet里边的init方法
{
ContainerC=getContentPane();
JButtonb[]=newJButton[16];
JPanelpanel=newJPanel();
C.add(textField,BorderLayout.NORTH);
C.add(panel,BorderLayout.CENTER);
panel.setLayout(newGridLayout(4,4,5,5));
Stringname[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置按钮
for(inti=0;i<16;i++)//添加按钮
{
b[i]=newJButton(name[i]);
b[i].setBackground(newColor(192,192,192));
b[i].setForeground(Color.BLUE);//数字键设置为蓝颜色
if(i%4==3)
b[i].setForeground(Color.RED);
b[i].setFont(newFont("宋体",Font.PLAIN,16));//设置字体格式
panel.add(b[i]);
b[i].addActionListener(this);
}
b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色
b[13].setForeground(Color.RED);
}
publicvoidactionPerformed(ActionEvente)
{
intcnt=0;
StringactionCommand=e.getActionCommand();
if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*")||actionCommand.equals("/"))
input+=""+actionCommand+"";//设置输入,把输入的样式改成需要的样子
elseif(actionCommand.equals("C"))
input="";
elseif(actionCommand.equals("="))//当监听到等号时,则处理input
{
input+="="+compute(input);
textField.setText(input);
input="";
cnt=1;
}
else
input+=actionCommand;//数字为了避免多位数的输入不需要加空格
if(cnt==0)
textField.setText(input);
}
privateStringcompute(Stringinput)//即1237的样例
{
Stringstr[];
str=input.split("");
Stack<Double>s=newStack<Double>();
doublem=Double.parseDouble(str[0]);
s.push(m);
for(inti=1;i<str.length;i++)
{
if(i%2==1)
{
if(str[i].compareTo("+")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
s.push(help);
}
if(str[i].compareTo("-")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
s.push(-help);
}
if(str[i].compareTo("*")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
doubleans=s.peek();//取出栈顶元素
s.pop();//消栈
ans*=help;
s.push(ans);
}
if(str[i].compareTo("/")==0)
{
doublehelp=Double.parseDouble(str[i+1]);
doubleans=s.peek();
s.pop();
ans/=help;
s.push(ans);
}
}
}
doubleans=0d;
while(!s.isEmpty())
{
ans+=s.peek();
s.pop();
}
Stringresult=String.valueOf(ans);
returnresult;
}
publicstaticvoidmain(Stringargs[])
{
JFrameframe=newJFrame("Count");
Countapplet=newCount();
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();//applet的init方法
applet.start();//线程开始
frame.setSize(350,400);//设置窗口大小
frame.setVisible(true);//设置窗口可见
}
}
Ⅲ 求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算。 (
public
MyCalculator()
{
f
=
new
JFrame("计算器ByMdou");
Container
contentPane
=
f.getContentPane();
/****************菜单的创建开始**************************************/
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);
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);
Ⅳ 用java编写一个简单计算器
/*
* @(#)JCalculator.java 1.00 06/17/2015
*/
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
/**
* A simple calculator program.
*<p>I saw this program in a QQ group, and help a friend correct it.</p>
*
*@authorSingyuenYip
*@version1.00 12/29/2009
*@seeJFrame
*@seeActionListener
*/
{
/**
* Serial Version UID
*/
= -169068472193786457L;
/**
* This class help close the Window.
*@authorSingyuenYip
*
*/
{
publicvoidwindowClosing(WindowEvent we) {
System.exit(0);
}
}
inti;
// Strings for Digit & Operator buttons.
privatefinalString[]str= {"7","8","9","/","4","5","6","*","1",
"2","3","-",".","0","=","+"};
// Build buttons.
JButton[]buttons=newJButton[str.length];
// For cancel or reset.
JButtonreset=newJButton("CE");
// Build the text field to show the result.
JTextFielddisplay=newJTextField("0");
/**
* Constructor without parameters.
*/
publicJCalculator() {
super("Calculator");
// Add a panel.
JPanel panel1 =newJPanel(newGridLayout(4, 4));
// panel1.setLayout(new GridLayout(4,4));
for(i= 0;i<str.length;i++) {
buttons[i] =newJButton(str[i]);
panel1.add(buttons[i]);
}
JPanel panel2 =newJPanel(newBorderLayout());
// panel2.setLayout(new BorderLayout());
panel2.add("Center",display);
panel2.add("East",reset);
// JPanel panel3 = new Panel();
getContentPane().setLayout(newBorderLayout());
getContentPane().add("North", panel2);
getContentPane().add("Center", panel1);
// Add action listener for each digit & operator button.
for(i= 0;i<str.length;i++)
buttons[i].addActionListener(this);
// Add listener for "reset" button.
reset.addActionListener(this);
// Add listener for "display" button.
display.addActionListener(this);
// The "close" button "X".
addWindowListener(newWindowCloser());
// Initialize the window size.
setSize(800, 800);
// Show the window.
// show(); Using show() while JDK version is below 1.5.
setVisible(true);
// Fit the certain size.
pack();
}
publicvoidactionPerformed(ActionEvent e) {
Object target = e.getSource();
String label = e.getActionCommand();
if(target ==reset)
handleReset();
elseif("0123456789.".indexOf(label) > 0)
handleNumber(label);
else
handleOperator(label);
}
// Is the first digit pressed?
booleanisFirstDigit=true;
/**
* Number handling.
*@paramkey the key of the button.
*/
publicvoidhandleNumber(String key) {
if(isFirstDigit)
display.setText(key);
elseif((key.equals(".")) && (display.getText().indexOf(".") < 0))
display.setText(display.getText() +".");
elseif(!key.equals("."))
display.setText(display.getText() + key);
isFirstDigit=false;
}
/**
* Reset the calculator.
*/
publicvoidhandleReset() {
display.setText("0");
isFirstDigit=true;
operator="=";
}
doublenumber= 0.0;
Stringoperator="=";
/**
* Handling the operation.
*@paramkey pressed operator's key.
*/
publicvoidhandleOperator(String key) {
if(operator.equals("+"))
number+= Double.valueOf(display.getText());
elseif(operator.equals("-"))
number-= Double.valueOf(display.getText());
elseif(operator.equals("*"))
number*= Double.valueOf(display.getText());
elseif(operator.equals("/"))
number/= Double.valueOf(display.getText());
elseif(operator.equals("="))
number= Double.valueOf(display.getText());
display.setText(String.valueOf(number));
operator= key;
isFirstDigit=true;
}
publicstaticvoidmain(String[] args) {
newJCalculator();
}
}
运行界面:
Ⅳ 用JAVA编写一个计算器
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.SwingConstants;
{
/**
*
*/
=1L;
Resultresult=newResult();//定义text的面板
Number_Keynumber_key=newNumber_Key();//定义按钮面板
//当点击按钮+、-、*、/时,com=true
booleancom=false;
//当i=0时说明是我们第一次输入,字符串text不会累加
inti=0;
//存放text的内容
Stringtext="";
//存放点击按钮+、-、*、/之前的数值
doubledefbutton=0;
//+、-、*、/的代号分别为1,2,3,4
intsymbol=0;
//构造函数
Jisuanqi(){
super("WangJiao");//设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗体时退出程序
JPanelpane=newJPanel();//定义主面板
pane.setLayout(newBorderLayout());
setBounds(380,220,30,80);//前两个参数是在屏幕上显示的坐标,后两个是大小
//替换图标
ImageIconicon=newImageIcon("F:1.GIF");
//Jisuanqi.class.getResource("APPLE.GIF")
//);
setIconImage(icon.getImage());
pane.add(result,BorderLayout.NORTH);
pane.add(number_key,BorderLayout.CENTER);
pane.add(number_key.equal,BorderLayout.SOUTH);
number_key.one.addActionListener(this);//对1按钮添加监听事件
number_key.two.addActionListener(this);//对2按钮添加监听事件
number_key.three.addActionListener(this);//对3按钮添加监听事件
number_key.four.addActionListener(this);//对4按钮添加监听事件
number_key.five.addActionListener(this);//对5按钮添加监听事件
number_key.six.addActionListener(this);//对6按钮添加监听事件
number_key.seven.addActionListener(this);//对7按钮添加监听事件
number_key.eight.addActionListener(this);//对8按钮添加监听事件
number_key.nine.addActionListener(this);//对9按钮添加监听事件
number_key.zero.addActionListener(this);//对0按钮添加监听事件
number_key.ce.addActionListener(this);//对置零按钮添加监听事件
number_key.plus.addActionListener(this);//对+按钮添加监听事件
number_key.equal.addActionListener(this);//对=按钮添加监听事件
number_key.sub.addActionListener(this);//对-按钮添加监听事件
number_key.mul.addActionListener(this);//对*按钮添加监听事件
number_key.div.addActionListener(this);//对/按钮添加监听事件
number_key.point.addActionListener(this);//对.按钮添加监听事件
setContentPane(pane);
pack();//初始化窗体大小为正好盛放所有按钮
}
//各个按钮触发的事件
publicvoidactionPerformed(ActionEvente){
/*
*如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么com=true如果没有com=
*false;或者是否点击数字键,如果是i=1,如果没有i=0;
*/
if(e.getSource()==number_key.one){
if(com||i==0){
result.text.setText("1");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"1");
}
}elseif(e.getSource()==number_key.two){
if(com||i==0){
result.text.setText("2");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"2");
}
}elseif(e.getSource()==number_key.three){
if(com||i==0){
result.text.setText("3");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"3");
}
}elseif(e.getSource()==number_key.four){
if(com||i==0){
result.text.setText("4");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"4");
}
}elseif(e.getSource()==number_key.five){
if(com||i==0){
result.text.setText("5");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"5");
}
}elseif(e.getSource()==number_key.six){
if(com||i==0){
result.text.setText("6");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"6");
}
}elseif(e.getSource()==number_key.seven){
if(com||i==0){
result.text.setText("7");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"7");
}
}elseif(e.getSource()==number_key.eight){
if(com||i==0){
result.text.setText("8");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"8");
}
}elseif(e.getSource()==number_key.nine){
if(com||i==0){
result.text.setText("9");
com=false;
i=1;
}else{
text=result.text.getText();
result.text.setText(text+"9");
}
}
/*
*对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
*如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添加0;否则保持原来的数值不变
*/
elseif(e.getSource()==number_key.zero){//result.text.getText()是得到text里内容的意思
if(com||i==0){
result.text.setText("0");
com=false;
i=1;
}else{
text=result.text.getText();
if(Float.parseFloat(text)>0||Float.parseFloat(text)<0){//Float.parseFloat(text)就是类型转换了,下面都是一样
result.text.setText(text+"0");
}else{
if(text.trim().indexOf(".")==-1){
result.text.setText(text);
}else{
result.text.setText(text+"0");
}
}
}
}elseif(e.getSource()==number_key.ce){
result.text.setText("0");
i=0;
com=true;
//text="";
defbutton=0;
}
/*
*本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,不存在就添加.
*/
elseif(e.getSource()==number_key.point){
if(com||i==0){
result.text.setText("0.");
com=false;
i=1;
}else{
text=result.text.getText();
if(text.trim().indexOf(".")==-1){
result.text.setText(text+".");
}else{
result.text.setText(text);
}
}
}//获得点击+之前的数值
elseif(e.getSource()==number_key.plus){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=1;
}//获得点击-之前的数值
elseif(e.getSource()==number_key.sub){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=2;
}//获得点击*之前的数值
elseif(e.getSource()==number_key.mul){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol=3;
}//获得点击/之前的数值
elseif(e.getSource()==number_key.div){
com=true;
i=0;
defbutton=Double.parseDouble(result.text.getText());
symbol=4;
}elseif(e.getSource()==number_key.equal){
switch(symbol){
case1:{//计算加法
doublead=defbutton
+Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
case2:{//计算减法
doublead=defbutton
-Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i=0;
text="";
break;
}
case3:{//计算乘法
doublead=defbutton
*Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
case4:{//计算除法
doublead=defbutton
/Double.parseDouble(result.text.getText());
result.text.setText(ad+"");
i=0;
text="";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
publicstaticvoidmain(String[]args){
Jisuanqiloveyou=newJisuanqi();
loveyou.show();
}
}
//计算器数字按钮定义面板
classNumber_KeyextendsJPanel{
/**
*
*/
=1L;
JButtonzero=newJButton("0");//数字键0
JButtonone=newJButton("1");//数字键1
JButtontwo=newJButton("2");//数字键2
JButtonthree=newJButton("3");//数字键3
JButtonfour=newJButton("4");//数字键4
JButtonfive=newJButton("5");//数字键5
JButtonsix=newJButton("6");//数字键6
JButtonseven=newJButton("7");//数字键7
JButtoneight=newJButton("8");//数字键8
JButtonnine=newJButton("9");//数字键9
JButtonplus=newJButton("+");
JButtonsub=newJButton("-");
JButtonmul=newJButton("*");
JButtondiv=newJButton("/");
JButtonequal=newJButton("=");
JButtonce=newJButton("清零");//置零键
JButtonpoint=newJButton(".");
Number_Key(){
setLayout(newGridLayout(4,4,1,1));//定义布局管理器为网格布局
setBackground(Color.blue);//设置背景颜色
//添加按钮
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
//计算器显示结果的窗体
classResultextendsJPanel{
/**
*
*/
=1L;
//text先是输入和结果
JTextFieldtext=newJTextField("0");
@SuppressWarnings("deprecation")
Result(){//讲输入的数字或得到的结果在text的右边显示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false);//文本框不能编辑
setLayout(newBorderLayout());//设定布局管理器边框布局
add(text,BorderLayout.CENTER);//text放置在窗体的中间
}
}
直接复制保存成Jisuanqi.java可以直接运行了