java加法计算器代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java加法计算器代码相关的知识,希望对你有一定的参考价值。

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;



这是只有一个加法的例子!希望帮到你
参考技术A 只要加法?追问

恩我想知道加法 减法等对应的程序,和这些是怎么实现的、

追答

。。。
用两个变量( a , b )存下你输入的值。
结果=a+b
结果=a-b
你这个问题,让我感觉你一点基础都没有。

或者是我没有理解你想问什么。

本回答被提问者采纳

初学IOS开发-简单的加法计算器swift版本

先看图,要先把Main.storyboard里面的大致画好

这里用到4个Label,2个Text Field和1个Button具体的看下图

然后这是ViewController.swift(也就是代码区),看图

附上代码(需要的自己复制粘贴很贴心吧):
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var textNumber1: UITextField!

@IBOutlet weak var textNumber2: UITextField!

@IBOutlet weak var resultLabel: UILabel!

@IBAction func calculate(_ sender: UIButton) {
    let num1 = Int(textNumber1.text!)!
    let num2 = Int(textNumber2.text!)!
    let sum = num1+num2
    resultLabel.text = String(sum)
}

}
最后附上模拟器运行效果图

以上是关于java加法计算器代码的主要内容,如果未能解决你的问题,请参考以下文章

c++builder6.0设计一个简单的加法计算器,附上源代码,小弟不胜感激!

学习笔记--Maven+Eclipse Java EE实例:加法计算器

python之路-利用索引切片功能做一个简易的两个未知数的加法计算器,代码如下:

加法的Java 中的加法

初学IOS开发-简单的加法计算器swift版本

用java设计计算器