运用各种组件,设计一个简易(简陋)的加减法计算器
实验内容:做一个简易的加减法计算器(设计标准的计算器方法也差不多,多些按键和监听,这里给个简单的)
上效果图:
额,确实简陋了点,不过是纯手敲,不是用拖拽做的(感觉这样对这部分内容理解好一点)。
实现结果:
使得程序运行之后,当输入两个小数作为运算数后,点击“相加”或者“相减”按钮,两数相加或相减的结果就会显示在“运算结果”框中。 如果点击“全部清零”按钮,那么两个运算数输入框和运算结果框中就会完全被清空。
上代码:
Test.java
public class Test {
public static void main(String[] args) {
new Calculator("简易计算器");
}
}
布局
Calculator.java
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class Calculator extends JFrame {
public Calculator(String title){
super(title);
Container container = this.getContentPane();
//4个标签
JLabel label1 = new JLabel("简易计算器");
JLabel label2 = new JLabel("运算数一");
JLabel label3 = new JLabel("运算数二");
JLabel label4 = new JLabel("运算结果");
//3个文本框
JTextField num1 = new JTextField(10);//字符数
JTextField num2 = new JTextField(10);//字符数
JTextField num3 = new JTextField(20);//字符数
//3个按钮
JButton button1 = new JButton("相加");
JButton button2 = new JButton("相减");
JButton button3 = new JButton("全部清零");
button3.setPreferredSize(new Dimension(100,30));
//对应监听
button1.addActionListener(new MyaddCalculatorListener(num1,num2,num3));
button2.addActionListener(new MysubCalculatorListener(num1,num2,num3));
button3.addActionListener(new MycleanCalculatorListener(num1,num2,num3));
//布局
container.setLayout(new FlowLayout());
//四个个面板
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(300, 40));//强制面板大小
p1.setBorder(new EmptyBorder(12,5,2,5));//改变面板边角,由于只能int,故p41直接控制大小
JPanel p2 = new JPanel(new GridLayout(3,2,10,10));
p2.setPreferredSize(new Dimension(180, 100));//太宽了,变小一点就可以减少距离
JPanel p3 = new JPanel(new GridLayout(1,2,50,0));
p3.setPreferredSize(new Dimension(180, 30));//强制面板大小
JPanel p4 = new JPanel();
p1.add(label1);
p2.add(label2);
p2.add(num1);
p2.add(label3);
p2.add(num2);
p2.add(label4);
p2.add(num3);
p3.add(button1);
p3.add(button2);
p4.add(button3);
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
setVisible(true);
setBounds(100,100,300,360);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
监听实现功能
MyCalculatorListener.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
//监听器类
//相加
class MyaddCalculatorListener implements ActionListener {
//获取变量
private JTextField num1,num2,num3;
public MyaddCalculatorListener(JTextField num1, JTextField num2, JTextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
if(num1.getText().equals(null) || num1.getText().equals("") || num2.getText().equals(null) || num2.getText().equals("")){
new MynullDialogDemo();//输入为空时有弹窗提示
return;
}
if (new Mymethod().special(num1,num2)){
return;
}
if(new Mydecimal().decimal(num1,num2)){
return;
}
//1.获得两个数
double n1 = Double.parseDouble(num1.getText());
double n2 = Double.parseDouble(num2.getText());
double sum1 = n1+n2;
BigDecimal bg = new BigDecimal(sum1);//保留6位小数
double f1 = bg.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
//2.运算后放到第三个框
num3.setText(""+f1);
}
}
//相减
class MysubCalculatorListener implements ActionListener{
//获取变量
private JTextField num1,num2,num3;
public MysubCalculatorListener(JTextField num1, JTextField num2, JTextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
if(num1.getText().equals(null) || num1.getText().equals("") || num2.getText().equals(null) || num2.getText().equals("")){
new MynullDialogDemo();
return;
}
if (new Mymethod().special(num1,num2)){
return;
}
if(new Mydecimal().decimal(num1,num2)){
return;
}
//1.获得两个数
double n1 = Double.parseDouble(num1.getText());
double n2 = Double.parseDouble(num2.getText());
double sum2 = n1-n2;
BigDecimal bg = new BigDecimal(sum2);
double f2 = bg.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
//2.运算后放到第三个框
num3.setText(""+f2);
}
}
//清除
class MycleanCalculatorListener implements ActionListener{
//获取变量
private JTextField num1,num2,num3;
public MycleanCalculatorListener(JTextField num1, JTextField num2, JTextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
num1.setText("");
num2.setText("");
num3.setText("");
}
}
//弹窗的窗口(输入为空时)
class MynullDialogDemo extends JDialog{
public MynullDialogDemo() {
this.setVisible(true);
this.setBounds(200,200,100,100);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JLabel label = new JLabel("运算数不能为空");
label.setBounds(10,10,100,40);
container.add(label);
}
}
//弹窗的窗口(有特殊字符)
class MyspecialDialogDemo extends JDialog{
public MyspecialDialogDemo() {
this.setVisible(true);
this.setBounds(200,200,200,100);
Container container = this.getContentPane();
container.setLayout(null);
JLabel label = new JLabel("运算数不能含有特殊字符");
label.setBounds(10,10,160,40);
container.add(label);
}
}
//弹窗的窗口(有特殊字符)
class MydecimalDialogDemo extends JDialog{
public MydecimalDialogDemo() {
this.setVisible(true);
this.setBounds(200,200,200,100);
Container container = this.getContentPane();
container.setLayout(null);
JLabel label = new JLabel("请核查运算数的小数点");
label.setBounds(10,10,160,40);
container.add(label);
}
}
//检查是否有特殊字符
class Mymethod{
public int qq = 0;
public boolean special(JTextField num1, JTextField num2) {
String str1 = num1.getText();
String str2 = num2.getText();
char[] a = str1.toCharArray();
char[] b = str2.toCharArray();
for(int i = 0; i<a.length; i++){
if((a[i]>='0' && a[i]<='9') || a[i]=='.'){
continue;
}else {
qq = 1;
new MyspecialDialogDemo();
}
}
for(int i = 0; i<b.length; i++){
if((b[i]>='0' && b[i]<='9') || b[i]=='.'){
continue;
}else {
qq = 1;
new MyspecialDialogDemo();
}
}
if(qq == 1){
return true;
}else {
return false;
}
}
}
//检查小数点
class Mydecimal{
public int flaga = 0;
public int flagb = 0;
public boolean decimal(JTextField num1, JTextField num2) {
String str1 = num1.getText();
String str2 = num2.getText();
char[] a = str1.toCharArray();
char[] b = str2.toCharArray();
for(int i = 0; i<a.length; i++){
if(a[0]=='.' || a[a.length-1]=='.'){
new MydecimalDialogDemo();
return true;
}
if(a[i]=='.'){
flaga++;
}
}
for(int i = 0; i<b.length; i++){
if(b[0]=='.' || b[b.length-1]=='.'){
new MydecimalDialogDemo();
return true;
}
if(b[i]=='.'){
flagb++;
}
}
if(flaga > 1 || flagb >1){
new MydecimalDialogDemo();
return true;
}else {
return false;
}
}
}
效果图:
感觉还可以,作为初学者码得有点痛苦,就不给各位白嫖了。