如上图,实现一个计算器的+,-,*,/与重置的功能,具有异常处理功能
话不多说上代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;//保留小数
public class SimpleCalculation extends JFrame{
Frame frame = new JFrame();
Frame MistakeWindons = new JFrame();
private JButton SumBtn = new JButton("SUM");
private JButton SubBtn = new JButton("SUB");
private JButton MulBtn = new JButton("MUL");
private JButton DivBtn = new JButton("DIV");
private JButton Reset = new JButton("Reset");
private JTextField FristCount = new JTextField(5);
private JTextField SecondCount = new JTextField(5);
private JTextField Result = new JTextField(10);
private Label Design = new Label("Design by 蒋永超");
private Font Ft = new Font("宋体", Font.BOLD, 18);//设置字体
public SimpleCalculation() {
frame.setResizable(false);//设置为不可最大化
frame.setTitle("简单计算器");
frame.setLocation(600, 400);//设置窗口位置
frame.setSize(300, 180);//设置窗口大小
frame.setLayout(null);
frame.setVisible(true);
frame.add(FristCount);
FristCount.setBounds(10, 10, 60, 20);
frame.add(SecondCount);
SecondCount.setBounds(90, 10, 60, 20);
frame.add(new Label("=")).setBounds(160, 10, 20, 20);
frame.add(Result);
Result.setBounds(180, 10, 100, 20);
frame.add(Design);
Design.setBounds(60, 40, 200, 30);
frame.add(SumBtn);
SumBtn.setBounds(5, 80, 70, 30);
frame.add(SubBtn);
SubBtn.setBounds(75, 80, 70, 30);
frame.add(MulBtn);
MulBtn.setBounds(145, 80, 70, 30);
frame.add(DivBtn);
DivBtn.setBounds(215, 80, 70, 30);
frame.add(Reset);
Reset.setBounds(215,110,70,30);
Design.setFont(Ft);
MyActionListen();
}
private void MyActionListen(){//事件处理函数
SumBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {//异常处理,如果输入的不是数字,则提示输入错误
String text0 = FristCount.getText();//将JTextFiled转换String
String text1 = SecondCount.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
Result.setText(a + b + "");
}
catch (Exception e1){
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200,100);
MistakeWindons.setLocation(550,550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50,20,100,20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
SubBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = FristCount.getText();//将JTextFiled转换String
String text1 = SecondCount.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
Result.setText(a - b + "");
}
catch (Exception e1){
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200,100);
MistakeWindons.setLocation(550,550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50,20,100,20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
MulBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = FristCount.getText();//将JTextFiled转换String
String text1 = SecondCount.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
Result.setText(a * b + "");
}
catch (Exception e1){
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200,100);
MistakeWindons.setLocation(550,550);
JLabel Mistake = new JLabel("Wrong Input!!!");
Mistake.setBounds(50,20,100,20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
DivBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String text0 = FristCount.getText();//将JTextFiled转换String
String text1 = SecondCount.getText();
double a = Double.valueOf(text0);//将String类型转换为double
double b = Double.valueOf(text1);
Result.setText(a / b + "");
double c = a/b;
NumberFormat Nf = NumberFormat.getNumberInstance();//保留小数
Nf.setMaximumFractionDigits(6);
Result.setText(Nf.format(c));
}
catch (Exception e1){
MistakeWindons.setResizable(false);
MistakeWindons.setLayout(null);
MistakeWindons.setTitle("Wrong Warning!");
MistakeWindons.setSize(200,100);
MistakeWindons.setLocation(550,550);
JLabel Mistake = new JLabel("Wrong Input!!!");
//Mistake.setFont(Ft);
Mistake.setBounds(50,20,100,20);
MistakeWindons.add(Mistake);
MistakeWindons.setVisible(true);
}
}
});
Reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FristCount.setText("");
SecondCount.setText("");
Result.setText("");
}
});
}
}
class Text{
public static void main(String[] args){
SimpleCalculation simpleCalculation = new SimpleCalculation();
}
}
有个问题没有考虑到,0不能做除数,
大家可以在DIV按钮下修改异常处理,把0不能做除数加进去就好。可以尝试自己去实现。