重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
不仅可以加减还可以乘除,代码如下:
成都创新互联公司服务项目包括新区网站建设、新区网站制作、新区网页制作以及新区网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,新区网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到新区省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
package 计算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
boolean flag = false;
JTextField jtf = new JTextField();
public static void main(String[] args) {
new Calculator();
}
public Calculator() {
setSize(300, 300);
setTitle("计算器");
Container c = getContentPane();
jtf.setHorizontalAlignment(JTextField.RIGHT);
JButton jb = new JButton("=");
jb.addActionListener(this);
JPanel jp = new JPanel();
c.add(jtf, BorderLayout.NORTH);
c.add(jp, BorderLayout.CENTER);
c.add(jb, BorderLayout.SOUTH);
jp.setLayout(new GridLayout(4, 4));
addButton(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addButton(JPanel jp) {
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp.add(b);
b = new JButton("2");
b.addActionListener(this);
jp.add(b);
b = new JButton("3");
b.addActionListener(this);
jp.add(b);
b = new JButton("+");
b.addActionListener(this);
jp.add(b);
b = new JButton("4");
b.addActionListener(this);
jp.add(b);
b = new JButton("5");
b.addActionListener(this);
jp.add(b);
b = new JButton("6");
b.addActionListener(this);
jp.add(b);
b = new JButton("-");
b.addActionListener(this);
jp.add(b);
b = new JButton("7");
b.addActionListener(this);
jp.add(b);
b = new JButton("8");
b.addActionListener(this);
jp.add(b);
b = new JButton("9");
b.addActionListener(this);
jp.add(b);
b = new JButton("*");
b.addActionListener(this);
jp.add(b);
b = new JButton("0");
b.addActionListener(this);
jp.add(b);
b = new JButton(".");
b.addActionListener(this);
jp.add(b);
b = new JButton("C");
b.addActionListener(this);
jp.add(b);
b = new JButton("/");
b.addActionListener(this);
jp.add(b);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "=") {
if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {
String s = jtf.getText();
Pattern p = Pattern.compile("\\d+\\.?\\d*");
Matcher m = p.matcher(s);
m.find();
double firstNum = Double.parseDouble(m.group());
m.find();
double secondNum = Double.parseDouble(m.group());
if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum + secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum - secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum * secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum / secondNum));
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确表达式!");
}
flag = true;
} else if (e.getActionCommand() == "C") {
jtf.setText("");
} else {
if (flag) {
if (e.getActionCommand() != "+" e.getActionCommand() != "-"
e.getActionCommand() != "*"
e.getActionCommand() != "/") {
//System.out.println(e.getActionCommand() != "+");
jtf.setText("");
}
flag = false;
}
jtf.setText(jtf.getText() + e.getActionCommand());
}
}
}
主要涉及的知识点: 类的写法, 以及方法的调用 .建议多做练习. 如果有看不懂的地方. 可以继续追问,一起讨论.
参考代码如下
//Number类
class Number {
private int n1;//私有的整型数据成员n1
private int n2;//私有的整型数据成员n2
// 通过构造函数给n1和n2赋值
public Number(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
// 加法
public int addition() {
return n1 + n2;
}
// 减法
public int subtration() {
return n1 - n2;
}
// 乘法
public int multiplication() {
return n1 * n2;
}
// 除法 (可能除不尽,所以使用double作为返回类型)
public double division() {
return n1 * 1.0 / n2; // 通过n1*1.0 把计算结果转换成double类型.
}
}
//Exam4 类
public class Exam4{
public static void main(String[] args) {
Number number=new Number(15, 6);//创建Number类的对象
//下面的是调用方法得到返回值进行输出显示
System.out.println("加法"+number.addition());
System.out.println("减法"+number.subtration());
System.out.println("乘法"+number.multiplication());
System.out.println("除法"+number.division());
}
}
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;
}
这是只有一个加法的例子!希望帮到你