2013-04-10 212 views
-2

我做了一個包含用戶名和密碼的表單...並提交按鈕 因此,點擊提交...我必須在單獨的程序中進行驗證部分,其中獲取結果爲真將允許用戶導航到內部主頁...添加禁用登錄page.And如果登錄信息是錯誤的,它會顯示消息dialog..showing錯誤的用戶名和密碼..登錄和註銷揮杆

public void actionPerformed(ActionEvent ae) 
{ 
    if (ae.getSource() == submit) 
    { 
     //to do... 
    } 
} 
+1

這很好,但是你的問題是什麼? – MadProgrammer 2013-04-10 06:52:21

+0

什麼是編碼..我需要完成提交按鈕 – Hitesh 2013-04-10 06:54:49

+0

這取決於。你如何驗證用戶?我可以建議你看看[SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html),它將允許在後臺線程中執行該過程,儘管 – MadProgrammer 2013-04-10 06:56:09

回答

2

不知道用戶的詳細信息是如何驗證,可以肯定地說,你不會想在美國東部時間的範圍內稱它。

相反,我用SwingWorker爲我完成這項工作(你必須填寫)。它只是返回一個truefalse值回EDT。

如果登錄失敗,我們只顯示一個JOptionPane顯示「失敗的登錄」消息。否則,我們可以簡單地處理登錄對話框...

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.Frame; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.concurrent.ExecutionException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.SwingWorker; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestLogin { 

    public static void main(String[] args) { 
     new TestLogin(); 
    } 

    public TestLogin() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JDialog frame = new JDialog((Frame) null, "Login", true); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

       System.exit(0); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.insets = new Insets(4, 4, 4, 4); 
      gbc.anchor = GridBagConstraints.WEST; 
      add(new JLabel("User Name:"), gbc); 
      gbc.gridy++; 
      add(new JLabel("Password:"), gbc); 
      gbc.gridx++; 
      gbc.gridy = 0; 
      add(new JTextField(10), gbc); 
      gbc.gridy++; 
      add(new JPasswordField(10), gbc); 

      JButton okay = new JButton("Okay"); 
      JButton cancel = new JButton("Cancel"); 

      gbc.gridx = 0; 
      gbc.gridy++; 
      gbc.anchor = GridBagConstraints.CENTER; 
      add(okay, gbc); 
      gbc.gridx++; 
      add(cancel, gbc); 

      okay.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        setFormEnabled(false); 
        new LoginWorker().execute(); 
       } 
      }); 

      cancel.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        SwingUtilities.windowForComponent((Component) e.getSource()).dispose(); 
       } 
      }); 
     } 

     protected void setFormEnabled(boolean enabled) { 
      for (Component comp : getComponents()) { 
       comp.setEnabled(enabled); 
      } 
     } 

     protected void loginSuccessful() { 
      SwingUtilities.windowForComponent(this).dispose(); 
     } 

     protected void loginFailed() { 
      JOptionPane.showMessageDialog(this, "Login failed", "Fail", JOptionPane.ERROR_MESSAGE); 
      setFormEnabled(true); 
     } 

     public class LoginWorker extends SwingWorker<Boolean, Void> { 

      @Override 
      protected Boolean doInBackground() throws Exception { 
       boolean login = false; 
       Thread.sleep(5000); 
       login = (int) Math.round(Math.random() * 1) == 0 ? false : true; 
       return login; 
      } 

      @Override 
      protected void done() { 
       try { 
        Boolean login = get(); 
        if (login) { 
         loginSuccessful(); 
        } else { 
         loginFailed(); 
        } 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
        loginFailed(); 
       } catch (ExecutionException ex) { 
        ex.printStackTrace(); 
        loginFailed(); 
       } 
      } 
     } 
    } 
}