2013-12-20 136 views
0

我正尝试在Java中创建JFrame登录屏幕。我搜索了互联网,只发现了Netbeans上我不想使用的东西,因为我想完全使用代码。JFrame中的登录屏幕

这将需要你只是通过查看代码才能看到用户名和密码。

我已经创建了一个JFrame:

JFrame launcher = new JFrame("Login"); 

和一些小工具:

JTextField User = new JTextField("Username"); 
JPasswordField Pass = new JPasswordField("Password"); 
JButton Login = new JButton("Login"); 

但我每次做的时候:

launcher.add(User) 
launcher.add(Pass) 
launcher.add(Login) 

登录按钮将coveer整个窗口。

因此,这里是我想要做的伪代码:

Login = create new button 
Pass = create new Password Field 
User = create new Text Field 
Check = 0 

add User at top of window 
add Pass at middle of window 
add Login at bottom of window 

if Pass input == any password from list then { 
    Check = Check + 1 
} 

if User input == any username from list then { 
    Check = Check + 1 
} 

if Check == 2 { 
    Change to new class 
}else{ 
    Show on screen "INVALID" then restart 
} 
+1

也许[_this answer_(http://stackoverflow.com/a/20286447/2587435)可能给你一些想法。它使用一个JDialog来登录到JFrame程序 –

回答

1

您没有设置布局管理器,因此JFrame的默认值是BorderLayout,它将所有内容添加到中心,这意味着当您添加通过时,它覆盖用户,然后添加登录按钮时,它覆盖了通行证,所以它看起来像你只有一个登录按钮。为了得到你想要的东西:

add User at top of window 
add Pass at middle of window 
add Login at bottom of window 

代码的一个例子是:

launcher.setLayout(new BorderLayout()); 
launcher.add(user, BorderLayout.NORTH); 
launcher.add(pass, BorderLayout.CENTER); 
launcher.add(login, BorderLayout.SOUTH); 

如果你想使它检查登录当按钮被点击添加ActionListener登录。

login.addActionListener(new ActionListener(){ 

    public void actionPerformed(ActionEvent e){ 
     //Check login stuff here... 

     //with maybe something like this? 
     String sLogin = login.getText(); 
     String sPass = pass.getText(); 
     //Then compare with some other string/data you already have saved somewhere... 

    } 
}); 

但是如果你想要更精确的布局,可以尝试搜索了GridBagLayout

如果你想读的一般了解版式去这里:LayoutManagers

+0

-1,这个答案已经提前一个小时了。没有必要混淆与重复的答案论坛。 – camickr

2

登录按钮将coveer整个窗口。

这是因为JFrame的默认布局管理器是BorderLayout,并且您将所有组件添加到“CENTER”(默认),并且只显示最后添加的组件。

所以你需要选择一个更合适的布局管理器。

阅读有关Layout Managers的Swing教程的部分以获取更多信息和示例。

0

您可以在NetBeans中使用的GridBagLayout。这种布局是复杂形式和面板的稳定方式。

tutorial for Netbeans

+0

Netbeans正在作弊。我想在日蚀中制作。 – genfy

0
Try this code for login screen 



class Login extends JFrame implements ActionListener{ 
    Connection con=null; 
    Statement st=null; 
    ResultSet rs=null; 
    JLabel l1,l2; 
    JButton b1,b2; 
    JTextField t1; 
    JPasswordField pf; 
    Login(){  
     Toolkit tk=Toolkit.getDefaultToolkit(); 
    Image img=tk.getImage("C:/Image.jpg"); 
    setIconImage(img); 
     Icon icon1=new ImageIcon("C:/Image.jpg"); 
     JLabel i=new JLabel(icon1); 
    JPanel p; 
    p=(JPanel)getContentPane(); 
    p.add(i); 
     this.getContentPane().setBackground(new Color(243,67,226)); 
     setVisible(true); 
     setLocation(225,50); 
     //setLocationRelativeTo(null); 
     setSize(900,600); 
     setTitle("Login"); 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     l1=new JLabel("UserName"); 
     l2=new JLabel("Password"); 
     t1=new JTextField(10); 
     AbstractAction action = new AbstractAction() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if(e.getSource() instanceof JButton){ 
      JButton button = (JButton) e.getSource(); 
      button.doClick();   
      } else if(e.getSource() instanceof JComponent){ 
       JComponent component = (JComponent) e.getSource(); 
       component.transferFocus(); 
      } 
     } 
     }; 

     t1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); 
     t1.getActionMap().put("TransferFocus", action); 

     pf=new JPasswordField(10); 
     pf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); 
     pf.getActionMap().put("TransferFocus", action); 
     ImageIcon ic=new ImageIcon("C:/key.gif"); 
     b1=new JButton("Login",ic); 
     b1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); 
     b1.getActionMap().put("TransferFocus", action); 
     ImageIcon ic1=new ImageIcon("C:/cancel.jpg"); 
     b2=new JButton("Exit",ic1);   
     b2.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); 
     b2.getActionMap().put("TransferFocus", action); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
     JPanel p1=new JPanel(); 
     JPanel p2=new JPanel();  
     JPanel p3=new JPanel(); 
     JPanel p4=new JPanel(); 
     p1.setBackground(new Color(243,67,226)); 
     p2.setBackground(new Color(243,67,226)); 
     p3.setBackground(new Color(243,67,226)); 
     p4.setBackground(new Color(243,67,226)); 
     p1.add(l1); 
     p1.add(t1); 
     p2.add(l2); 
     p2.add(pf); 
     p3.add(b1); 
     p3.add(b2); 
     p4.setLayout(new GridLayout(3,1)); 
     p4.add(p1);  
     p4.add(p2); 
     p4.add(p3); 
     add(p4); 
     t1.requestFocus(); 
     //b1.requestFocus(); 
     //b1.requestFocusInWindow(); 
     validate(); 

    } //eof cons. 
    public void actionPerformed(ActionEvent ae){ 
     if(ae.getSource()==b1){ 
      try { 
       try{ 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        String url=null,url1=null,userID=null,password=null; 
        String dbFileName=null; 
        String dbFileName1=null; 
        String sql=null; 

        dbFileName = "C:/db.accdb"; 

        url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+ 
          "DBQ="+dbFileName+";"; 
        con=DriverManager.getConnection(url);//,"system","manager" 
        st=con.createStatement(); 
       }catch(Exception e){ 
        System.out.println(e); 
        dispose(); 
       } 
       if(t1.getText().trim().length()==0){ 
        JOptionPane.showMessageDialog(this,"Enter User Name"); 
        return; 
       } 
       if(pf.getText().trim().length()==0){ 
        JOptionPane.showMessageDialog(this,"Enter Password"); 
        return; 
       } 



       ResultSet rs=st.executeQuery("select password from LOGIN1 where uname='"+t1.getText().trim()+"'"); 

       if(rs.next()){ 
        if(rs.getString(1).equals(pf.getText())){ 
         String smess="WEL-COME "; 
         String ss="Congratulations......"; 
         int res=JOptionPane.INFORMATION_MESSAGE; 
         JOptionPane.showMessageDialog((Component) null,smess,ss,res); 
         JFrame frm=new JFrame("Title"); 
         className b=new className(frm); 
         frm.setSize(800,500); 
         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         frm.setResizable(false); 
         frm.setLocationRelativeTo(null); 
         dispose(); 
         frm.show(); 
        }else{ 
         JOptionPane.showMessageDialog(this,"Invalid Password"); 
        } 

       }else{ 
        JOptionPane.showMessageDialog(this,"Invalid User name"); 
       } 

      } catch (SQLException ex) { 
       Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      }if(ae.getSource()==b2){ 
       dispose(); 
      } 
    } 
    public static void main(String args[]){ 
     new Login(); 
    } 

} 
+0

要么你使用NetBeans,要么你是一个非常混乱的程序员 – genfy

+0

我只在Netbeans中创建了这个登录框架。而你对我的第二个看法,所以我的名字本身就表明了这一点。谢谢! – Aarav