刚开始学JAVA比较菜,帮忙给加入异常处理功能
本程序是实现数据加密的,当输入为空,或者含有非数字字符进行异常处理
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class encrypt extends JFrame
{
private JLabel beforeencryptJLabel;
private JTextField beforeencryptJTextArea;
private JLabel afterencryptJLabel;
private JTextArea afterencryptJTextArea;
private JButton encryptJButton;
public encrypt()
{
createUserInterface();
}
private void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground( Color.YELLOW );
contentPane.setLayout(null);
beforeencryptJLabel = new JLabel();
beforeencryptJLabel.setBounds( 16, 16, 156, 24 );
beforeencryptJLabel.setText( "加密前数据:" );
beforeencryptJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) );
contentPane.add( beforeencryptJLabel );
beforeencryptJTextArea = new JTextField();
beforeencryptJTextArea.setBounds( 20,60,300,30 );
afterencryptJLabel = new JLabel();
afterencryptJLabel.setBounds( 16, 130, 156, 24 );
afterencryptJLabel.setText( "加密后数据:" );
afterencryptJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) );
contentPane.add( afterencryptJLabel );
String str="";
afterencryptJTextArea = new JTextArea( str);
afterencryptJTextArea.setBounds( 20, 160, 300, 100 );
contentPane.add( beforeencryptJTextArea );
contentPane.add( afterencryptJTextArea );
encryptJButton = new JButton();
encryptJButton.setBounds( 316, 16, 60, 24 );
encryptJButton.setText( "加密" );
contentPane.add( encryptJButton );
encryptJButton.addActionListener(
new ActionListener()// anonymous inner class
{
// event handler called when encryptbutton is clicked
public void actionPerformed( ActionEvent event )
{
encryptJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set properties of application's window
setTitle( "encrypt data" ); // set title bar text
setSize( 400, 320 ); // set window size
setVisible( true ); // display window
}
private void encryptJButtonActionPerformed( ActionEvent event )
{
afterencryptJTextArea.setText("");
String s;
int t,i;
long n;
n=Long.parseLong(beforeencryptJTextArea.getText());
s=String.valueOf(n);
t=s.length();
long[] a;
a=new long[t];
for(i=0;i<t;i++)
a[i]=Long.parseLong(String.valueOf(s.charAt(i)));
for(i=0;i<t;i++)
{ a[i]=(a[i]+7)%10;}
for(i=0;i<t;i++)
afterencryptJTextArea.append(String.valueOf(a[i]));
}
// main method
public static void main( String[] args )
{
encrypt application = new encrypt();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class encrypt