if(names.get(i).equals(tfName.getText())){
if(pwd.get(i).equals(tfPassword.getText())){
JOptionPane.showMessageDialog(null,"登陆成功");
setVisible(false);//隐藏原界面
newGuessNum();//进入主界面
}
else{
JOptionPane.showMessageDialog(null,"用户名或密码有误");
}
}
}
}
});
p3.add(ok);
p3.add(cancel);
pm.add(p1);
pm.add(p2);
pm.add(p3);
this.add(pm);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newLogin();
}
}
packageJAVA;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.util.Random;
importjavax.swing.JButton;
importjavax.swing.JLabel;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjavax.swing.WindowConstants;
importjava.awt.FlowLayout;
publicclassGuessNumextendsjavax.swing.JFrame{
privatestaticfinallongserialVersionUID=1L;
privateJLabeljLabel1;
privateJTextFieldjTextField1;
privateJButtonjButton1;
privateJButtonjButton2;
privateJButtonjButton3;
privateintnumber=0;
privateintcounter=0;
longstartTime=System.currentTimeMillis();
longendTime;//计算时间
publicGuessNum(){
super("猜数字小游戏");
initChuangkou();
Randomrandom=newRandom();
number=random.nextInt(100);//产生一个1——100的随机数
}
privatevoidinitChuangkou(){
try{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//窗口关闭时销毁窗口
getContentPane().setLayout(null);//设置窗口布局为绝对布局
JFrameframe=newJFrame("TestButtons");
frame.setLayout(newFlowLayout());
jLabel1=newJLabel();
getContentPane().add(jLabel1);
jLabel1.setText("★欢迎进入有趣的猜数字游戏★请输入1~100中的任意一个数:
");
jLabel1.setBounds(75,20,200,50);
jTextField1=newJTextField();
getContentPane().add(jTextField1);
jTextField1.setBounds(110,100,112,28);
jTextField1.addKeyListener(newKeyAdapter(){
publicvoidkeyPressed(KeyEventevt){
jTextField1KeyPressed(evt);
}
});
jButton1=newJButton();
getContentPane().add(jButton1);
jButton1.setText("确定");
jButton1.setBounds(120,160,100,25);
jButton1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevt){
jButton1ActionPerformed(evt);//按钮被点击时执行该方法
}
});
jButton2=newJButton();
getContentPane().add(jButton2);
jButton2.setText("显示答案");
jButton2.setBounds(120,200,100,25);
jButton2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevt2){
jButton2ActionPerformed(evt2);//按钮被点击时执行该方法
}
});
jButton3=newJButton();
getContentPane().add(jButton3);
jButton3.setText("再来一次");
jButton3.setBounds(120,240,100,25);
jButton3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevt3){
jButton3ActionPerformed(evt3);//按钮被点击时执行该方法
}
});
pack();
this.setSize(340,400);//窗口大小
setLocationRelativeTo(null);//设置窗口在显示器居中显示
}
catch(Exceptione){
e.printStackTrace();
}
setVisible(true);
}
privatevoidjButton2ActionPerformed(ActionEventevt2){
JOptionPane.showMessageDialog(null,"答案是"+number);//直接显示答案
setVisible(false);//隐藏原界面
return;
}
privatevoidjButton3ActionPerformed(ActionEventevt3){
setVisible(false);
newGuessNum();
}
privatevoidjButton1ActionPerformed(ActionEventevt){
intguess=0;
counter++;//计数器加一
try{
g