// Enable user to enter security codes specifying access privileges.
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class SecurityGuard extends JFrame
{
private JLabel securityCodeJLabel;
private JPasswordField securityCodeJPasswordField;
public String str_password=new String();
private JButton oneJButton;
private JButton twoJButton;
private JButton threeJButton;
private JButton fourJButton;
private JButton fiveJButton;
private JButton sixJButton;
private JButton sevenJButton;
private JButton eightJButton;
private JButton nineJButton;
private JButton clearJButton;
private JButton zeroJButton;
private JButton enterJButton;
private JLabel accessLogJLabel;
private JTextArea accessLogJTextArea;
private JScrollPane accessLogJScrollPane;
public SecurityGuard()
{
createUserInterface();
}
private void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setLayout( null );
securityCodeJLabel = new JLabel();
securityCodeJLabel.setBounds( 16, 16, 90, 21 );
securityCodeJLabel.setText( "Security code:" );
contentPane.add( securityCodeJLabel );
//密码输入文本设置
securityCodeJPasswordField = new JPasswordField();
Font f=new Font("Time New Roman",Font.BOLD,36);
securityCodeJPasswordField.setFont(f);
securityCodeJPasswordField.setBounds(105,16,180,21);
securityCodeJPasswordField.setEditable(false);
contentPane.add( securityCodeJPasswordField );
oneJButton = new JButton();
oneJButton.setBounds( 80, 64, 50, 50 );
oneJButton.setText( "1" );
contentPane.add( oneJButton );
oneJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
oneJButtonActionPerformed( event );
}
} );
twoJButton = new JButton();
twoJButton.setBounds( 130, 64, 50, 50 );
twoJButton.setText( "2" );
contentPane.add( twoJButton );
twoJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
twoJButtonActionPerformed( event );
}
} );
threeJButton = new JButton();
threeJButton.setBounds( 180, 64, 50, 50 );
threeJButton.setText( "3" );
contentPane.add( threeJButton );
threeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
threeJButtonActionPerformed( event );
}
} );
fourJButton = new JButton();
fourJButton.setBounds( 80, 114, 50, 50 );
fourJButton.setText( "4" );
contentPane.add( fourJButton );
fourJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
fourJButtonActionPerformed( event );
}
} );
fiveJButton = new JButton();
fiveJButton.setBounds( 130, 114, 50, 50 );
fiveJButton.setText( "5" );
contentPane.add( fiveJButton );
fiveJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
fiveJButtonActionPerformed( event );
}
} ); // end call to addActionListener
sixJButton = new JButton();
sixJButton.setBounds( 180, 114, 50, 50 );
sixJButton.setText( "6" );
contentPane.add( sixJButton );
sixJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
sixJButtonActionPerformed( event );
}
} ); // end call to addActionListener
sevenJButton = new JButton();
sevenJButton.setBounds( 80, 164, 50, 50 );
sevenJButton.setText( "7" );
contentPane.add( sevenJButton );
sevenJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
sevenJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
eightJButton = new JButton();
eightJButton.setBounds( 130, 164, 50, 50 );
eightJButton.setText( "8" );
contentPane.add( eightJButton );
eightJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
eightJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
nineJButton = new JButton();
nineJButton.setBounds( 180, 164, 50, 50 );
nineJButton.setText( "9" );
contentPane.add( nineJButton );
nineJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
nineJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds( 80, 214, 50, 50 );
clearJButton.setText( "C" );
contentPane.add( clearJButton );
clearJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
zeroJButton = new JButton();
zeroJButton.setBounds( 130, 214, 50, 50 );
zeroJButton.setText( "0" );
contentPane.add( zeroJButton );
zeroJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
zeroJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
//**模仿其它按钮,建立enterJButton,显示为"#"
enterJButton = new JButton();
enterJButton.setBounds(180,214,50,50); //
enterJButton.setText("#"); //
contentPane.add( enterJButton ); //
enterJButton.addActionListener( //
new ActionListener() // anonymous inner class //
{
public void actionPerformed( ActionEvent event ) //
{
enterJButtonActionPerformed( event ); //
}
} // end anonymous inner class
); // end call to addActionListener
accessLogJLabel = new JLabel();
accessLogJLabel.setBounds( 16, 285, 100, 16 );
accessLogJLabel.setText( "Access log:" );
contentPane.add( accessLogJLabel );
accessLogJTextArea = new JTextArea();
accessLogJScrollPane = new JScrollPane( accessLogJTextArea );
accessLogJScrollPane.setBounds( 16, 309, 270, 95 );
contentPane.add( accessLogJScrollPane );
setTitle( "Security Guard" ); // set window's title
setSize( 310, 450 ); // set window's size
setVisible( true ); // display window
} // end method createUserInterface
private void oneJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"1";
securityCodeJPasswordField.setText(str_password); //
}
private void tw