实现一个可以进行两个数加减乘除的计算器。有两个输入框用以输入数字,屏幕有四个按钮分别为+、-、×、÷,在界面中显示结果。以加法为例如果输入的数字为1和3,则在界面中的输出结果为:1+3=4。在输入时要进行输入检查和异常处理;如果用户未输入两个数,要提示请输入数据,可以用文本提示也可以弹出对话框提示。(注意所有组件采用Swing的组件,控件可以根据需求和设计的不同增加)
import java.awt.*;
public class ScreenUtils {
/*
获取当前电脑屏幕的宽度
*/
public static int getScreenWidth(){
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
/*
获取当前电脑屏幕的高度
*/
public static int getScreenHeight(){
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
}
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CalView {
JFrame jf = new JFrame("简易计算器");
/**
* 定义窗口的宽度
*/
final int WIDTH = 600;
/**
* 定义窗口的高度
*/
final int HEIGHT = 350;
/**
* 计算符号,计算值,计算结果
*/
String method;
double calValue1;
double calValue2;
double result;
JTextField valueField1;
JTextField valueField2;
JTextField resField;
/**
* 组装视图
*/
public void init() {
//设置关闭窗口为结束程序
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口相关的属性
jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
//设置窗口大小不可变
jf.setResizable(false);
/*
组装 value1 和 text1
*/
Box valueBox1 = Box.createHorizontalBox();
JLabel valueLabel1 = new JLabel("value1");
//设置为10个长度大小
valueField1 = new JTextField(10);
valueBox1.add(valueLabel1);
//设置水平间隔为20px
valueBox1.add(Box.createHorizontalStrut(20));
valueBox1.add(valueField1);
/*
组装 value2 和 text2
*/
Box valueBox2 = Box.createHorizontalBox();
JLabel valueLabel2 = new JLabel("value2");
//设置为10个长度大小
valueField2 = new JTextField(10);
valueBox2.add(valueLabel2);
//设置水平间隔为20px
valueBox2.add(Box.createHorizontalStrut(20));
valueBox2.add(valueField2);
/*
组装 valueBox1 和 valueBox2
*/
Box valueBox12 = Box.createHorizontalBox();
valueBox12.add(valueBox1);
//设置水平间隔为40px
valueBox12.add(Box.createHorizontalStrut(40));
valueBox12.add(valueBox2);
/*
组装计算方式
*/
Box methodBox = Box.createHorizontalBox();
JLabel methodLabel = new JLabel("计算方式");
JRadioButton addButton = new JRadioButton("+");
JRadioButton subButton = new JRadioButton("-");
JRadioButton mulButton = new JRadioButton("×");
JRadioButton divButton = new JRadioButton("÷");
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
//得到当前选择的计算方式
method = actionCommand;
}
};
addButton.addActionListener(actionListener);
subButton.addActionListener(actionListener);
mulButton.addActionListener(actionListener);
divButton.addActionListener(actionListener);
//添加到同一个按钮组,实现单选效果
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(addButton);
buttonGroup.add(subButton);
buttonGroup.add(mulButton);
buttonGroup.add(divButton);
methodBox.add(methodLabel);
methodBox.add(Box.createHorizontalStrut(30));
methodBox.add(addButton);
methodBox.add(Box.createHorizontalStrut(20));
methodBox.add(subButton);
methodBox.add(Box.createHorizontalStrut(20));
methodBox.add(mulButton);
methodBox.add(Box.createHorizontalStrut(20));
methodBox.add(divButton);
/*
* 组装运算组件
*/
JButton runButton = new JButton("开始运算");
runButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//清空结果文本域
resField.setText("");
if ("".equals(valueField1.getText()) || "".equals(valueField2.getText())) {
//说明有文本为空
JOptionPane.showMessageDialog(jf, "请输入数据", "提醒", JOptionPane.WARNING_MESSAGE);
return;
}
try {
calValue1 = Double.parseDouble(valueField1.getText().trim());
calValue2 = Double.parseDouble(valueField2.getText().trim());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(jf, "检测到非正常数据", "提醒", JOptionPane.ERROR_MESSAGE);
return;
}
if (method == null) {
JOptionPane.showMessageDialog(jf, "请选择运算方式", "提醒", JOptionPane.WARNING_MESSAGE);
return;
}
switch (method) {
case "+":
result = calValue1 + calValue2;
break;
case "-":
result = calValue1 - calValue2;
break;
case "×":
result = calValue1 * calValue2;
break;
case "÷":
if (calValue2 == 0) {
JOptionPane.showMessageDialog(jf, "检测到非法运算(0作为分母)", "提醒", JOptionPane.ERROR_MESSAGE);
return;
}
result = calValue1 / calValue2;
break;
}
//在文本域中显示运算结果
if(((result+"").matches(".*(\\.0)$"))){
resField.setText(valueField1.getText() + " " + method + " " + valueField2.getText() + " = " + (int)result);
}else{
resField.setText(valueField1.getText() + " " + method + " " + valueField2.getText() + " = " + result);
}
}
});
Box runBox = Box.createHorizontalBox();
runBox.add(runButton);
/*
组装计算结果
*/
Box resBox = Box.createHorizontalBox();
JLabel resLabel = new JLabel("计算结果");
resField = new JTextField(40);
resBox.add(resLabel);
resBox.add(Box.createHorizontalStrut(30));
resBox.add(resField);
/*
* 组装 valueBox12 methodBox resBox
*/
Box viewBox = Box.createVerticalBox();
viewBox.add(Box.createVerticalStrut(60));
viewBox.add(valueBox12);
viewBox.add(Box.createVerticalStrut(50));
viewBox.add(methodBox);
viewBox.add(Box.createVerticalStrut(50));
viewBox.add(runBox);
viewBox.add(Box.createVerticalStrut(50));
viewBox.add(resBox);
/*
* 组装 viewBox 到面板
*/
JPanel viewPanel = new JPanel();
//设置背景颜色为青蓝色
viewPanel.setBackground(new Color(153, 255, 255));
//为panel设置边框 创建凸起的斜边框,分别设置四条边的颜色
viewPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.GREEN, Color.BLUE, Color.GRAY));
viewPanel.add(viewBox);
jf.add(viewPanel);
//显示窗口可见
jf.setVisible(true);
}
}
/**
*主启动类
*/
public class CalApplication {
public static void main(String[] args) {
new CalView().init();
}
}