⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crcmainswing.java

📁 CRC程序的演示程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CRCMainSwing {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		SwingFrame frame = new SwingFrame();
	//	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	//	int width = 800;
	//	int height = 200;
	//	frame.setBounds((d.width - width) / 2, (d.height - height) / 2, width,
	//			height);
//
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}

@SuppressWarnings("serial")
class SwingFrame extends JFrame {

	JButton genCode;
	JButton checkCode;


	JLabel hintSrcFrame;
	JLabel hintGenerator;

	JTextField txtSrcFrame;
	JTextField txtGen;

	CRC_Check cc = new CRC_Check();

	static String s1 = new String();
	static String s2 = new String();
	static String s3 = new String();

	SwingFrame() {

		setTitle("CRC校验(CopyRight By Chenx @ csu2009)");

		JPanel panel = new JPanel();
        panel.setLayout(new  GridBagLayout());
        GridBagConstraints gbCons = new GridBagConstraints();
        
        gbCons.gridx = 0;
		gbCons.gridy = 0;
		hintSrcFrame = new JLabel("数据或校验帧S(x):");
		panel.add(hintSrcFrame,gbCons);
		
		gbCons.gridx = 1;
		gbCons.gridy = 0;
	    txtSrcFrame = new JTextField(64);
	    panel.add(txtSrcFrame,gbCons);
	    txtSrcFrame.addActionListener(new ButtonOperation());
	    
	    gbCons.gridx = 0;
		gbCons.gridy = 1;
		hintGenerator = new JLabel("生成的多项式G(x):");
        panel.add(hintGenerator,gbCons);
        txtGen.addActionListener(new ButtonOperation());
          
	    gbCons.gridx = 1;
		gbCons.gridy = 1;
        txtGen = new JTextField(64);
        panel.add(txtGen,gbCons);
        
        
        JPanel btnPanel = new JPanel();
        
		genCode = new JButton("生成校验码");
		genCode.addActionListener(new ButtonOperation());
		btnpanel.add(genCode);
		
		checkCode = new JButton("检查校验码");
		checkCode.addActionListener(new ButtonOperation());
		btnpanel.add(checkCode);

     	cc.setBitSet(32);
		
		gbCons.gridx = 1;
		gbCons.gridy = 2;
		gbCons.anchor = GridBagConstraints.EAST;
	    panel.add(btnPanel, gbCons);
	    
		getContentPane().add(panel);
		setSize(3000, 1500);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private class ButtonOperation implements ActionListener {
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent event) {

			if (event.getSource().equals(txtSrcFrame)) {

				s1 = txtSrcFrame.getText();

			}

			if (event.getSource().equals(txtGen)) {
				s2 = txtGen.getText();

			}

			if (event.getSource().equals(genCode)) {
				if (s1.equals("") || s2.equals("")) {
					JOptionPane.showMessageDialog(genCode, "请先输入G(x)或是S(x)!"
							+ s3, "Error Input", JOptionPane.ERROR_MESSAGE);
					return;

				}
				cc.sf = s1;
				cc.formatSourceFraToBitSet();
				cc.gt = s2;
				cc.formatGerneratorToBitSet();

				s3 = cc.getCheckSumFrame();
				JOptionPane.showMessageDialog(genCode, "校验和帧为:" + s3, "校验和帧",
						JOptionPane.INFORMATION_MESSAGE);
			}
			if (event.getSource().equals(checkCode)) {
				if (s1.equals("") || s2.equals("")) {
					JOptionPane.showMessageDialog(genCode, "请先输入G(x)或是S(x)!"
							+ s3, "Error Input", JOptionPane.ERROR_MESSAGE);
					return;

				}
				String sTrim = new String();
				sTrim = s1.substring(0, s1.length() - s2.length() + 1);
				cc.sf = sTrim;
				cc.formatSourceFraToBitSet();
				cc.gt = s2;
				cc.formatGerneratorToBitSet();
				s3 = cc.getCheckSumFrame();
				if (s3.equals(s1)) {
					JOptionPane.showMessageDialog(checkCode, "校验正确!", "校验结果",
							JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(checkCode, "校验错误!", "校验结果",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}

		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -