急求一个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() {
// 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);
}
}
}

}

温馨提示:答案为网友推荐,仅供参考