Here is an example how to get CRC32 value:
- Code: Select all
import javax.swing.*;
import java.awt.event.*;
import java.util.zip.CRC32;
public class CRC32Test extends JFrame {
public static void main(String[] args) {
new CRC32Test();
}
private JButton buttonOK;
private JTextField textField;
public CRC32Test() {
this.setSize(325, 100);
this.setTitle("CRC32");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener b1 = new ButtonListener();
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Enter your text: "));
textField = new JTextField(15);
panel1.add(textField);
buttonOK = new JButton("OK");
buttonOK.addActionListener(b1);
panel1.add(buttonOK);
this.add(panel1);
this.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonOK) {
String text = textField.getText();
if (text.length() == 0) {
JOptionPane.showMessageDialog(CRC32Test.this,
"You didn't enter anything!",
"Result", JOptionPane.INFORMATION_MESSAGE);
} else {
CRC32 crc = new CRC32();
crc.reset();
crc.update(text.getBytes());
JOptionPane.showMessageDialog(CRC32Test.this,
"The CRC32 value is " + crc.getValue(),
"Result", JOptionPane.INFORMATION_MESSAGE);
}
textField.requestFocus();
}
}
}
}
