项目资源下载链接Java实现人民币大写+GUI界面
1.提要
(1)基本功能:将人民币转换为大写,实现了仟亿级别以内的大写转换,规则和运行结果可参考网站人民币小写在线转换成大写
(2)实现了简单的GUI界面,对于输入的异常情况有所提示和处理,有测试用例
(3)最终有两个窗口类,一个实现转换的类以及一个测试类
2.算法
(1)因为是自己写的,一边敲,思路就慢慢出来了,所以逻辑性不是那么的严谨,就勉强写出来了吧,最多只可实现仟亿以内的转换
(2)基本算法(实现功能的在Cconvert类中):
1)设置输入的数值为符号串,首先通过int ConvertRMB.Convert.inputflag(String money)
函数将金额字符串分为三种情况,当返回值为0时,输入的金额字符串包含不规则字符,即数字和小数点以外的符号。
2)根据int ConvertRMB.Convert.inputflag(String money)
,的返回值,在String ConvertRMB.Convert.Convertmoney(String money)
函数中通过字符串分割函数将金额划分为小数部分和整数部分,分别执行整数部分转换函数String ConvertRMB.Convert.convertzheng(String money1)
和小数部分转换函数String ConvertRMB.Convert.convertsmall(String money2)
,最后加上“人民币”字符串,再将两者拼接起来即可
3. 代码
(1)界面:windowbuilder插件的使用
(1)先创建项目,再在该项目下,按鼠标右键,选择 New ->other -> application window,新建一个Main的窗口类
(2) 点击 工作区下的Design ,先点击相对布局(也可选择其他布局)放在面板上,再随意添加组件。先点击组件,再将鼠标移到面板,点击即可,不是通过拖动。在Properties区域可以设置组件的响应信息,字体font,内容text等。在Components视图区,可以指定选择已经在在窗口中的组件,以及查看组件的包含关系。
(3)给组件添加事件。鼠标选中组件之后,按右键 -> add event handler,再选择要添加的事件。在sorce代码区,可以添加事件的相应响应效果。
(4)再根据需要修改、添加其中一些代码即可。
(5)最终Main.java类的代码,包括了事件部分
package ConvertRMB;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
public class Main {
private JFrame frame;
private JTextField inputmoney;
private JTextField outputmoney;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("\u4EBA\u6C11\u5E01\u5927\u5199\u8F6C\u6362");
frame.setBounds(100, 100, 617, 495);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null); // 设置窗口居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* 该类的这行代码frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* 与Help类的frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
* 这两行代码是设置关闭Help窗口,Main窗口不关闭的
* */
inputmoney = new JTextField();
inputmoney.setFont(new Font("宋体", Font.PLAIN, 17));
inputmoney.setBounds(190, 92, 311, 42);
frame.getContentPane().add(inputmoney);
inputmoney.setColumns(10);
outputmoney = new JTextField();
outputmoney.setFont(new Font("宋体", Font.PLAIN, 17));
outputmoney.setBounds(190, 183, 311, 42);
frame.getContentPane().add(outputmoney);
outputmoney.setColumns(10);
JLabel lblNewLabel = new JLabel("\u5C0F\u5199\u91D1\u989D");
lblNewLabel.setBounds(89, 95, 72, 39);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u5927\u5199\u91D1\u989D");
lblNewLabel_1.setBounds(89, 186, 72, 39);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u4EBA\u6C11\u5E01\u5927\u5199\u8F6C\u6362");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_2.setBounds(217, 55, 159, 24);
frame.getContentPane().add(lblNewLabel_2);
JButton help = new JButton("\u67E5\u770B\u5E2E\u52A9");
help.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {