问题描述
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.*;classLoginWindowextendsJDialogimplementsActionListener{JPanelp1=newJPanel();//定义并建立面板JPanelp2=newJPanel();JPanelp3=newJPanel();JPanelp4=newJPanel();JPanelp5=newJPanel();inti=0;JTextFieldtxtUserName=newJTextField(15);//用户名文本框JPasswordFieldtxtPassWord=newJPasswordField(15);//密码框JButtonok=newJButton("确定");JButtoncancel=newJButton("取消");publicLoginWindow(){setModal(true);//设置模态setBackground(Color.LIGHT_GRAY);//设置背景颜色ContainercontentPane=this.getContentPane();//取出内容面板contentPane.setLayout(newGridLayout(5,1));//设置布局为5行1列//将组件添加到中间容器p2.add(newJLabel("用户名:"));p2.add(txtUserName);p3.add(newJLabel("密码:"));p3.add(txtPassWord);p4.add(ok);p4.add(cancel);ok.addActionListener(this);//注册事件接听者cancel.addActionListener(this);txtUserName.addActionListener(this);txtPassWord.addActionListener(this);contentPane.add(p1);//将面板添加到内容面板contentPane.add(p2);contentPane.add(p3);contentPane.add(p4);contentPane.add(p5);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置自动关闭窗口setSize(350,260);Dimensionscreen=Toolkit.getDefaultToolkit().getScreenSize();setLocation((screen.width-300)/2,(screen.height-220)/2);setTitle("登录窗口");setResizable(false);setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==ok||e.getSource()==txtPassWord){if(txtUserName.getText().trim().equals("shine")&&txtPassWord.getText().trim().equals("shine")){dispose();}else{if(++i>=3){dispose();System.exit(0);}JOptionPane.showMessageDialog(null,"用户名或密码错误!");txtUserName.requestFocus();txtUserName.setSelectionStart(0);txtUserName.setSelectionEnd(txtUserName.getText().length());}}elseif(e.getSource()==cancel){dispose();System.exit(0);}elseif(e.getSource()==txtUserName)txtPassWord.requestFocus();}publicstaticvoidmain(String[]args){JDialog.setDefaultLookAndFeelDecorated(true);Fontfont=newFont("JFrame",Font.PLAIN,14);Enumerationkeys=UIManager.getLookAndFeelDefaults().keys();while(keys.hasMoreElements()){Objectkey=keys.nextElements();if(UIManager.get(key)instanceofFont)UIManager.put(key,font);}newLoginWindow();}}//在编译的时候出现了错误,我真了半天找不到啊,希望前辈们指点哈。
解决方案
解决方案二:
Objectkey=keys.nextElements();例如,要输出向量v的所有元素,可使用以下方法:for(Enumeratione=v.elements();e.hasMoreElements();){System.out.println(e.nextElement());}nextElement()nextElements()