Java实战之亲戚关系计算器(swing版)(5)——回退及清空功能

实现点击称谓按钮功能

在map包目录下创建NameProperty.java类,其内容如下:

package map;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class NameProperty {
    Map<String, String> map = null;

    /**
     * 操作结果:根据传入的按钮获得其称呼
     *
     * @param button
     * @return
     */
    public String getNameByButton(JButton button) {
        map = new HashMap<String, String>();
        map.put("父", "爸爸");
        map.put("母", "妈妈");
        map.put("夫", "老公");
        map.put("妻", "老婆");
        map.put("子", "儿子");
        map.put("女", "女儿");
        map.put("兄", "哥哥");
        map.put("弟", "弟弟");
        map.put("姐", "姐姐");
        map.put("妹", "妹妹");

        String buttonName = button.getText();
        String returnName = map.get(buttonName);
        return returnName;
    }

    /**
     * 操作结果:将文本域获得的内容转换成Map的一种映射
     *
     * @param textArea
     * @return
     */
    public String convertTextAreaToString(JTextArea textArea) {
        String[] names = {"的", "爸爸", "妈妈", "老公", "老婆", "儿子", "女儿", "哥哥", "弟弟", "姐姐", "妹妹"};
        String[] simpleNames = {",", "f", "m", "h", "w", "s", "d", "bb", "sb", "bs", "ss"};
        String resultContent = textArea.getText();
        for (int i = 0; i < names.length; i++) {
            resultContent = resultContent.replace(names[i], simpleNames[i]);
        }
        return resultContent;
    }

}

在Frame.java中添加如下方法:

    /**
     * 将按钮名添加到文本域显示亲戚关系并根据判断其添加“的”前缀
     *
     * @param nameButton 按钮
     */
    private void setText(JButton nameButton) {
        String name;
        // 判断按钮的被点击次数是否为0
        if (count == 0) {
            // 如果是0,表示是第一次点击按钮,故不为其添加“的”前缀(即关系之间的分隔标志)
            name = new NameProperty().getNameByButton(nameButton);
        } else {
            // 如果不是0,则表示是第二次或者第N此点击按钮,因此需要为其添加“的”前缀作为亲戚关系之间的分隔
            name = "的" + new NameProperty().getNameByButton(nameButton);
        }
        // 按钮次数统计加一
        count++;
        // 将关系字符串显示到文本域中
        inputRelationTextArea.append(name);
    }

并在该类下定义变量count

private int count=0;

并为actionPerformed方法添加如下内容:

    @Override
    public void actionPerformed(ActionEvent e) {
        // 判断文本域是否为空,如果是则将累计的按钮点击次数清零,重新计数
        if (inputRelationTextArea.getText().trim().equals(null) || inputRelationTextArea.getText().equals("")) {
            count = 0;
        }
        // 判断事件源是哪一个按钮,并为其设置响应操作
        if (e.getSource() == fatherButton) {
            setText(fatherButton);
        } else if (e.getSource() == motherButton) {
            setText(motherButton);
        } else if (e.getSource() == husbandButton) {
            setText(husbandButton);
        } else if (e.getSource() == wifeButton) {
            setText(wifeButton);
        } else if (e.getSource() == sonButton) {
            setText(sonButton);
        } else if (e.getSource() == daughterButton) {
            setText(daughterButton);
        } else if (e.getSource() == bigBrotherButton) {
            setText(bigBrotherButton);
        } else if (e.getSource() == smallBrotherButton) {
            setText(smallBrotherButton);
        } else if (e.getSource() == bigSisterButton) {
            setText(bigSisterButton);
        } else if (e.getSource() == smallSisterButton) {
            setText(smallSisterButton);
        }
    }

运行程序,实现点击称谓按钮在上方的文本域显示关系链,效果图如下:

但并没有实现回退和清空的功能

 

回退按钮功能

在Frame.java的Frame构造器中添加如下内容(在类中定义变量undoManager):

        undoManager = new UndoManager();
        inputRelationTextArea.getDocument().addUndoableEditListener(new UndoableEditListener() {
            @Override
            public void undoableEditHappened(UndoableEditEvent e) {
                undoManager.addEdit(e.getEdit());
            }
        });

        // “回退”按钮事件
        undoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                undoManager.undo();
            }
        });

现在实现回退功能。

原:

点击:

回退:

 

清空按钮功能

在Frame构造器中为clearButton添加监听器内容如下:

        // ”清空“按钮事件
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                inputRelationTextArea.setText("");
                outputResultTextArea.setText("");
            }
        });

运行效果如下:

原:

清空:

 

 

可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。

注意:在公众号后台回复【20191114】可获取本节源码。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值