java计算器源代码

编写程序完成简单计算器的功能:加、减、乘、除,程序界面如下:

Number1、Number2、Result 是三个JLabel 控件,它们后面的是三个 JTextField控件,用来输入操作数并显示运算结果;窗体下边是四个JButton控件Add、Subtract、Multiply、Divide,分别代表加、减、乘、除运算,点击是进行相应的运算并显示结果;窗体上边的菜单完成同样的功能。

第1个回答  2009-11-17
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
initComponents();
}

private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
result = new javax.swing.JTextField();
addBtn = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Num1:");

jLabel2.setText("Num2:");

jLabel3.setText("Num3:");

addBtn.setText("Add");
addBtn.addActionListener(new jisuanAC());

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addBtn)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addComponent(num1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(result)
.addComponent(num2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGap(168, 168, 168))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(num2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(61, 61, 61)
.addComponent(addBtn)
.addContainerGap(81, Short.MAX_VALUE))
);

jMenu1.setText("Operation");

jMenu3.setText("Add");
jMenu1.add(jMenu3);

jMenuBar1.add(jMenu1);

jMenu2.setText("Exit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private class jisuanAC implements ActionListener
{

public void actionPerformed(ActionEvent e) {
if(e.getSource()== addBtn)
{
int number1 = Integer.parseInt(num1.getText());
int number2 = Integer.parseInt(num2.getText());
int rs = number1 + number2;
result.setText(String.valueOf(rs));

}
}

}

// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField result;

}

这是只有一个加法的例子!希望帮到你本回答被提问者采纳

用JAVA编写的科学计算器源代码
以下是一个简单的用Java编写的科学计算器的源代码示例:java import java.util.Scanner;public class ScientificCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Welcome to the Scientific Calculator!");System.out.println("Ente...

用java编得计算器程序软件和源代码
import java.awt.*; import javax.swing.*; import java.awt.event.*; class Counter extends WindowAdapter { static JFrame f=new JFrame("计算器"); static JTextField text1=new JTextField("0."); static String source=""; static String cal=""; static String object=""; static boolean flag=fa...

java 写的计算器源代码只实现加减乘除四则运算即可
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Vector;public class calculator { String str1="0"; \/\/运算数1 初值一定为0 为了程序的安全 String str2="0"; \/\/运算数2 String fh="+"; \/\/运算符 String jg="";\/\/结果 \/\/状态开关 重要 int k1=...

求基于Java开发的多功能计算器源码,要有注释的,谢谢!
import java.awt.*;import java.awt.event.*;public class Calculator extends WindowAdapter implements ActionListener { private double result=0,data1=0,radixPointDepth=1;private boolean radixPointIndicate=false,resultIndicate=false;private char prec='+';private Frame f;private TextField tf;...

java计算器源代码
} private void initComponents() { jPanel1 = new javax.swing.JPanel();jLabel1 = new javax.swing.JLabel();jLabel2 = new javax.swing.JLabel();jLabel3 = new javax.swing.JLabel();num1 = new javax.swing.JTextField();num2 = new javax.swing.JTextField();result = new javax...

求JAVA计算器源代码!!!
f=new JFrame("计算器");p=new Panel();\/\/运算界面p p.setLayout(new GridLayout(4,4)); \/\/ 设置p的布局为GridLayout,四行四列 tf=new TextField(30);\/\/实例化按钮 b1=new Button("7");b2=new Button("8");b3=new Button("9");b4=new Button("+");b5=new Button("4");b6...

用java写的电脑附件中计算器的源代码
number = getNumberOnDisplay(); \/\/将新输入的数由显示器上的转存入number if(isUnaryOp(op)) handleUnaryOp(op); else handleBinaryOp(op); }void handleDecPoint(){ opPressed = false; \/\/事实上小数点不算运算符,在这里我把它作为一种特殊的符号处理if( strVal.indexOf(".")<0 ){ strVal += "....

用JAVA编写的科学计算器源代码
import java.awt.*; import java.awt.event.*; \/\/计算器显示结果的窗体 class Result extends JPanel { JTextField text = new JTextField("0"); \/\/text先是输入和结果 Result() { text.setHorizontalAlignment(SwingConstants.RIGHT); \/\/讲输入的数字或得到的结果在text的右边显示 text.setEnabled(false...

编写java application程序实现一个简易计算器
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 b...

急:求一个简单的JAVA计算器代码,简单点,有图形界面的
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;class Stack_Float { float nums[];int top;Stack_Float(){ nums = new float[50];top = -1;} boolean IsEmpty(){ if (top == ...

相似回答