大二实训终于结束了,为了纪念一下这一个月来的努力,接下来几篇博客,就把实训的东西拿出来写一下吧。首先,为了让我们熟悉一下java的语法,就要弄一个简易的计算器。
题目
实现结果
最后实现起来也和题目的要求差不多,数字由用户自己输入,同时加多了一点儿用户非法输入的处理。实现结果图如下:
代码分析
对于熟悉了Java的伙伴来说,实现这个计算器真的不难,所以,代码分析这里,也只是针对刚学Java的伙伴进行讲解。至于数字的输入处理,比较简单,就不多说了。对于运算符号的处理,对每个运算符号button增加一个事件监听器,当点击该button的时候,运算符设置成相应的值(加、减、乘、除),并刷新表达式中运算符的显示。加号的处理如下:(其他运算符类似)
add = new JButton("+"); //第二排各Button的实例化,排版和事件监听处理
add.setBounds(10, 70, 50, 50);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
operator = '+';
operand.setText("+");
}
});
由于在JTextField中输入的数字,是字符串类型的,所以,在处理的时候,需要把字符串类型转换为数字,处理如下:
//字符串转化为数字的处理,异常输入的处理
private double numPaser(String str) {
if(str.equals("")) {
JOptionPane.showMessageDialog(null, "NO NUMBER", "ALERT", JOptionPane.WARNING_MESSAGE);
return 0;
}
double number = 0;
try {
number = Double.valueOf(str);
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "NOT NUMBER!", "ALERT", JOptionPane.WARNING_MESSAGE);
number = 0;
}
return number;
}
最后,附上完整代码。
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 EasyCalculator {
//主面板
private JFrame frame = null;
//数字输入面板
private JTextField firstNum = null;
private JTextField secondNum = null;
//操作数显示面板
private JLabel operand = null;
private JLabel equalSign = null;
private JLabel result = null;
//操作点击按钮
private JButton add = null;
private JButton sub = null;
private JButton mul = null;
private JButton div = null;
private JButton OK = null;
//数字和操作数存储
private double fNumber = 0;
private double sNumber = 0;
private double rNumber = 0;
private char operator = '+';
public EasyCalculator() {
frame = new JFrame("Easy Calculator"); //建立窗口
frame.setLayout(null);
firstNum = new JTextField(); //第一排各组件的实例化和排版
firstNum.setBounds(10, 10, 50, 50);
operand = new JLabel("+", JLabel.CENTER);
operand.setBounds(70, 10, 50, 50);
secondNum = new JTextField();
secondNum.setBounds(130, 10, 50, 50);
equalSign = new JLabel("=", JLabel.CENTER);
equalSign.setBounds(190, 10, 50, 50);
result = new JLabel("res");
result.setBounds(250, 10, 80, 50);
add = new JButton("+"); //第二排各Button的实例化,排版和事件监听处理
add.setBounds(10, 70, 50, 50);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
operator = '+';
operand.setText("+");
}
});
sub = new JButton("-");
sub.setBounds(70, 70, 50, 50);
sub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
operator = '-';
operand.setText("-");
}
});
mul = new JButton("*");
mul.setBounds(130, 70, 50, 50);
mul.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
operator = '*';
operand.setText("*");
}
});
div = new JButton("/");
div.setBounds(190, 70, 50, 50);
div.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
operator = '/';
operand.setText("/");
}
});
OK = new JButton("OK");
OK.setBounds(250, 70, 80, 50);
OK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fNumber = numPaser(firstNum.getText());//获取输入的两个操作数
sNumber = numPaser(secondNum.getText());
switch (operator) {
case '+':
rNumber = fNumber + sNumber;
break;
case '-':
rNumber = fNumber - sNumber;
break;
case '*':
rNumber = fNumber * sNumber;
break;
case '/':
rNumber = fNumber / sNumber;
break;
}
result.setText(String.valueOf(rNumber));//更改输出的结果
}
});
//将上述实例化的各个组件添加到窗口中
frame.add(firstNum);
frame.add(operand);
frame.add(secondNum);
frame.add(equalSign);
frame.add(result);
frame.add(add);
frame.add(sub);
frame.add(mul);
frame.add(div);
frame.add(OK);
//窗口属性的设置
frame.setSize(350, 160);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}
//字符串转化为数字的处理,异常输入的处理
private double numPaser(String str) {
if(str.equals("")) {
JOptionPane.showMessageDialog(null, "NO NUMBER", "ALERT", JOptionPane.WARNING_MESSAGE);
return 0;
}
double number = 0;
try {
number = Double.valueOf(str);
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "NOT NUMBER!", "ALERT", JOptionPane.WARNING_MESSAGE);
number = 0;
}
return number;
}
//主函数,实例化计算器
public static void main(String[] args) {
new EasyCalculator();
}
}