JAVA程序计算器除法不为零_急求一个Java程序的计算器,带图形界面的,只完成整数的加减乘除,除数为零弹出对话框提示,好心人!!!...

展开全部

package com.calc;

import java.awt.FlowLayout;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

@SuppressWarnings("serial")

public class CalcMain extends JFrame implements ActionListener {

private JPanel jpnl1;

private JPanel jpnl2;

private JTextField jtf1;

private JTextField jtf2;

private JTextField jtfResult;

private JLabel jlblCalc;

private JLabel jlblResult;

private JButton jbnPlus;

private JButton jbnMinus;

private JButton jbnTimes;

private JButton jbnDivide;

public CalcMain() {

62616964757a686964616fe4b893e5b19e31333332643930// TODO Auto-generated constructor stub

jtf1 = new JTextField();

jtf2 = new JTextField();

jtfResult = new JTextField();

jlblCalc = new JLabel("+");

jlblResult = new JLabel("=");

jpnl1 = new JPanel();

GridBagLayout gr = new GridBagLayout();

jpnl1.setLayout(gr);

GridBagConstraints gc = new GridBagConstraints();

jpnl1.add(jtf1);

jpnl1.add(jlblCalc);

jpnl1.add(jtf2);

jpnl1.add(jlblResult);

jpnl1.add(jtfResult);

gc.fill = GridBagConstraints.BOTH;

gc.weightx = 4;

gc.weighty = 1;

gr.setConstraints(jtf1, gc);

gr.setConstraints(jtf2, gc);

gr.setConstraints(jtfResult, gc);

jbnPlus = new JButton("Plus");

jbnPlus.addActionListener(this);

jbnMinus = new JButton("Minus");

jbnMinus.addActionListener(this);

jbnTimes = new JButton("Times");

jbnTimes.addActionListener(this);

jbnDivide = new JButton("Divide");

jbnDivide.addActionListener(this);

jpnl2 = new JPanel();

jpnl2.setLayout(new FlowLayout());

jpnl2.add(jbnPlus);

jpnl2.add(jbnMinus);

jpnl2.add(jbnTimes);

jpnl2.add(jbnDivide);

this.setLayout(new GridLayout(2, 1));

this.add(jpnl1);

this.add(jpnl2);

this.setTitle("计算器");

this.setSize(500, 100);

this.setLocation(500, 300);

this.setVisible(true);

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

new CalcMain();

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if (e.getSource().equals(jbnPlus)) {

jlblCalc.setText("+");

try {

int calc1 = Integer.parseInt(jtf1.getText().trim());

int calc2 = Integer.parseInt(jtf2.getText().trim());

jtfResult.setText(calc1 + calc2 + "");

} catch (NumberFormatException e1) {

e1.printStackTrace();

JOptionPane.showMessageDialog(this, "请输入两个整数", "消息",

JOptionPane.INFORMATION_MESSAGE);

}

} else if (e.getSource().equals(jbnMinus)) {

jlblCalc.setText("-");

try {

int calc1 = Integer.parseInt(jtf1.getText().trim());

int calc2 = Integer.parseInt(jtf2.getText().trim());

jtfResult.setText(calc1 - calc2 + "");

} catch (NumberFormatException e1) {

e1.printStackTrace();

JOptionPane.showMessageDialog(this, "请输入两个整数", "消息",

JOptionPane.INFORMATION_MESSAGE);

}

} else if (e.getSource().equals(jbnTimes)) {

jlblCalc.setText("×");

try {

int calc1 = Integer.parseInt(jtf1.getText().trim());

int calc2 = Integer.parseInt(jtf2.getText().trim());

jtfResult.setText(calc1 * calc2 + "");

} catch (NumberFormatException e1) {

e1.printStackTrace();

JOptionPane.showMessageDialog(this, "请输入两个整数!", "消息",

JOptionPane.INFORMATION_MESSAGE);

}

} else if (e.getSource().equals(jbnDivide)) {

jlblCalc.setText("÷");

try {

int calc1 = Integer.parseInt(jtf1.getText().trim());

int calc2 = Integer.parseInt(jtf2.getText().trim());

if (calc2 == 0) {

JOptionPane.showMessageDialog(this, "除数不能为零!", "消息",

JOptionPane.INFORMATION_MESSAGE);

return;

}

jtfResult.setText(calc1 / calc2 + "");

} catch (NumberFormatException e1) {

e1.printStackTrace();

JOptionPane.showMessageDialog(this, "请输入两个整数", "消息",

JOptionPane.INFORMATION_MESSAGE);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值