[code=Java]
// 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
// 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.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 twoJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"2";
securityCodeJPasswordField.setText(str_password); // //
}
private void threeJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"3";
securityCodeJPasswordField.setText(str_password); //
}
private void fourJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"4";
securityCodeJPasswordField.setText(str_password); //
}
private void fiveJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"5";
securityCodeJPasswordField.setText(str_password); //
}
private void sixJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"6";
securityCodeJPasswordField.setText(str_password); //
}
private void sevenJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"7";
securityCodeJPasswordField.setText(str_password); //
}
private void eightJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"8";
securityCodeJPasswordField.setText(str_password); //
}
private void nineJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"9";
securityCodeJPasswordField.setText(str_password); //
}
private void zeroJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"0";
securityCodeJPasswordField.setText(str_password); //
}
private void clearJButtonActionPerformed( ActionEvent event )
{
str_password="";
securityCodeJPasswordField.setText(str_password);
accessLogJTextArea.setText("");
}
//**当用户按下#时,屏幕中显示相应信息
private void enterJButtonActionPerformed( ActionEvent event )
{ Date time=new Date();
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
int int_password=Integer.parseInt(str_password);
if(int_password==1645)
accessLogJTextArea.append(time1.format(time)+"技术员\n");
else if(int_password==8345)
accessLogJTextArea.append(time1.format(time)+"管理员\n");
else if(int_password==9998||(int_password>1005&&int_password<1009))
accessLogJTextArea.append(time1.format(time)+"科学家\n");
else if(int_password==7||int_password==8||int_password==0)
accessLogJTextArea.append(time1.format(time)+"Restricted Access\n");
else
accessLogJTextArea.append(time1.format(time)+"Access Denied\n");
str_password="";
securityCodeJPasswordField.setText(str_password);
} // end method enterJButtonActionPerformed
// main method
public static void main( String[] args )
{
SecurityGuard application = new SecurityGuard();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main} // end class SecurityGuard
[/code]
请大家帮忙测试一下~~~~有问题的话帮忙改改~~~
希望对此程序有所改进的意见 比如加入键盘输入的部分,增加一部分的按键等等,高分回报~!~!
在执行#号建的时候,首先应该进行str_password是否为空的判断!
这个gui的设计构思太长了,特别是增加1..9的安扭部份和响应可以换为更简单点的 按扭用 for语句完成,响应用一个
方法内集合处理,主要用这条命令判读
if (e.getActionCommand().equals("1")){ ...}
if (e.getActionCommand().equals("2")){ ...}
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 twoJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"2";
securityCodeJPasswordField.setText(str_password); // //
}
private void threeJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"3";
securityCodeJPasswordField.setText(str_password); //
}
private void fourJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"4";
securityCodeJPasswordField.setText(str_password); //
}
private void fiveJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"5";
securityCodeJPasswordField.setText(str_password); //
}
private void sixJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"6";
securityCodeJPasswordField.setText(str_password); //
}
private void sevenJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"7";
securityCodeJPasswordField.setText(str_password); //
}
private void eightJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"8";
securityCodeJPasswordField.setText(str_password); //
}
private void nineJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"9";
securityCodeJPasswordField.setText(str_password); //
}
private void zeroJButtonActionPerformed( ActionEvent event )
{
str_password=str_password+"0";
securityCodeJPasswordField.setText(str_password); //
}
private void clearJButtonActionPerformed( ActionEvent event )
{
str_password="";
securityCodeJPasswordField.setText(str_password);
accessLogJTextArea.setText("");
}
//**当用户按下#时,屏幕中显示相应信息
private void enterJButtonActionPerformed( ActionEvent event )
{ Date time=new Date();
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
int int_password=Integer.parseInt(str_password);
if(int_password==1645)
accessLogJTextArea.append(time1.format(time)+"技术员\n");
else if(int_password==8345)
accessLogJTextArea.append(time1.format(time)+"管理员\n");
else if(int_password==9998||(int_password>1005&&int_password <1009))
accessLogJTextArea.append(time1.format(time)+"科学家\n");
else if(int_password==7||int_password==8||int_password==0)
accessLogJTextArea.append(time1.format(time)+"Restricted Access\n");
else
accessLogJTextArea.append(time1.format(time)+"Access Denied\n");
str_password="";
securityCodeJPasswordField.setText(str_password);
} // end method enterJButtonActionPerformed
// main method
public static void main( String[] args )
{
SecurityGuard application = new SecurityGuard();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class SecurityGuard