第1个回答 推荐于2018-03-22
发你了
/** SimpleCalculator.java *
** write by banxi1988 2010-05-*;
** modification by banxi1988 2010-06-05 22:00*/
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.JTextField;
public class SimpleCalculator extends JFrame implements ActionListener {
JFrame jfr = new JFrame("A Simple Calculator");
JLabel title = new JLabel("Simple Calculator");
JLabel[] jlaArry = { new JLabel("操作数一"), new JLabel("操作数二"),
new JLabel("结果") };
JTextField[] jtfArray = { new JTextField(), new JTextField(),
new JTextField() };
JButton[] jbtArray = { new JButton("相加"), new JButton("相减"),
new JButton("相乘"), new JButton("相除"), new JButton("清零") };
public SimpleCalculator() {
jfr.setLayout(null);
title.setBounds(30, 10, 160, 30);
jfr.add(title);
for (int i = 0; i < jlaArry.length; i++) {
jlaArry[i].setBounds(20, 50 + i * 40, 90, 30);
jfr.add(jlaArry[i]);
jtfArray[i].setBounds(140, 50 + i * 40, 90, 30);
jfr.add(jtfArray[i]);
}/* for */
jbtArray[0].setBounds(30, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[0].addActionListener(this);
jfr.add(jbtArray[0]);
jbtArray[1].setBounds(120, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[1].addActionListener(this);
jfr.add(jbtArray[1]);
jbtArray[2].setBounds(210, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[2].addActionListener(this);
jfr.add(jbtArray[2]);
jbtArray[3].setBounds(300, 40 + (jlaArry.length + 1) * 40, 80, 30);
jbtArray[3].addActionListener(this);
jfr.add(jbtArray[3]);
jbtArray[4].setBounds(70, 50 + (jlaArry.length + 2) * 40, 80, 30);
jbtArray[4].addActionListener(this);
jfr.add(jbtArray[4]);
jfr.setBounds(100, 100, 460, 330);
// jfr.setResizable(false);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}/* SimpleCalculator no-arg Constructor */
public void actionPerformed(ActionEvent e) {
float n1 = 0; /* local variable should be initialized */
float n2 = 0;
boolean correct = true;/* judge if input is correct */
String message = "您的输入有误.或者输入不完整.\n 请检查后重试!";
if (e.getSource() == jbtArray[4]) {
for (int i = 0; i < jtfArray.length; i++) {
jtfArray[i].setText("");
}/* for */
}/* if clear button is pressed */
else {
try {
n1 = Float.parseFloat(jtfArray[0].getText());
n2 = Float.parseFloat(jtfArray[1].getText());
} catch (NumberFormatException nfe) {
correct = false;
JOptionPane.showMessageDialog(null, message, "出错啦!",
JOptionPane.WARNING_MESSAGE);
}/* catch */
if (correct) {
if (e.getSource() == jbtArray[0]) {
float result = n1 + n2;
jtfArray[2].setText(Float.toString(result));
}/* if Add button is pressed */
else if (e.getSource() == jbtArray[1]) {
float result = n1 - n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
else if (e.getSource() == jbtArray[2]) {
float result = n1 * n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
else if (e.getSource() == jbtArray[3]) {
float result = n1 / n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
}/* if input is correct */
}/* outer else not clear button is pressed */
}/* actionPerformed */
/* main method */
public static void main(String[] args) {
SimpleCalculator cal = new SimpleCalculator();
}/* main() */
}/* SimpleCalculator class */本回答被提问者和网友采纳
第2个回答 2010-11-27
/** SimpleCalculator.java *
** write by banxi1988 2010-05-*;
** modification by banxi1988 2010-06-05 22:00*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleCalculator extends JFrame implements ActionListener {
JFrame jfr = new JFrame("A Simple Calculator");
JLabel title = new JLabel("Simple Calculator");
JLabel [] jlaArry ={ new JLabel("操作数一"), new JLabel("操作数二") ,new JLabel ("结果")};
JTextField [] jtfArray = { new JTextField(),new JTextField(),new JTextField()};
JButton [] jbtArray = { new JButton("相加"),new JButton("相减"),new JButton("清零")};
public SimpleCalculator() {
jfr.setLayout(null);
title.setBounds(30,10,160,30);
jfr.add(title);
for(int i = 0; i< jlaArry.length; i++) {
jlaArry[i].setBounds(20,50+i*40,90,30);
jfr.add(jlaArry[i]);
jtfArray[i].setBounds(140,50+i*40,90,30);
jfr.add(jtfArray[i]);
}/* for */
jbtArray[0].setBounds(30,40+(jlaArry.length+1)*40,80,30);
jbtArray[0].addActionListener(this);
jfr.add(jbtArray[0]);
jbtArray[1].setBounds(120,40+(jlaArry.length+1)*40,80,30);
jbtArray[1].addActionListener(this);
jfr.add(jbtArray[1]);
jbtArray[2].setBounds(70,50+(jlaArry.length+2)*40,80,30);
jbtArray[2].addActionListener(this);
jfr.add(jbtArray[2]);
jfr.setBounds(100,100,260,330);
//jfr.setResizable(false);
jfr.setVisible(true);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}/* SimpleCalculator no-arg Constructor*/
public void actionPerformed(ActionEvent e){
float n1 = 0; /* local variable should be initialized */
float n2 = 0;
boolean correct = true;/* judge if input is correct */
String message = "您的输入有误.或者输入不完整.\n 请检查后重试!";
if(e.getSource()==jbtArray[2]){
for(int i= 0; i< jtfArray.length; i++){
jtfArray[i].setText("");
}/* for */
}/* if clear button is pressed */
else{
try{
n1 = Float.parseFloat(jtfArray[0].getText());
n2 = Float.parseFloat(jtfArray[1].getText());
}catch(NumberFormatException nfe){
correct = false;
JOptionPane.showMessageDialog(null,
message,"出错啦!",JOptionPane.WARNING_MESSAGE);
}/* catch */
if(correct){
if(e.getSource()==jbtArray[0]){
float result = n1 +n2;
jtfArray[2].setText(Float.toString(result));
}/* if Add button is pressed */
else{
float result = n1 - n2;
jtfArray[2].setText(Float.toString(result));
}/* if sub button is pressed */
}/* if input is correct */
}/* outer else not clear button is pressed */
}/* actionPerformed */
/* main method */
public static void main(String [] args) {
SimpleCalculator cal = new SimpleCalculator();
}/* main() */
}/* SimpleCalculator class */