2016-06-13 76 views
0

我目前正在Netbeans中工作一個簡單的項目,習慣JFrame表單。我創建了幾個JFrames,我想通過按下按鈕跳過它們,但每當按下按鈕時都不會發生任何事情。JFrame中的JButton不起作用

這是我的代碼

public class MainLogIn extends javax.swing.JFrame { 

/** 
* Creates new form MainLogIn 
*/ 

//testdata 
private String[] dummy_data = {"user1:parola1", "user2:parola2"}; 

public MainLogIn() { 
    initComponents(); 

    LogIn = new JButton(); 
    signUp = new JButton(); 
    username = new JTextField(); 
    password = new JPasswordField(); 

    wrong = new JLabel(); 
    wrong.setVisible(false); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 
    username = new javax.swing.JTextField(); 
    password = new javax.swing.JPasswordField(); 
    LogIn = new javax.swing.JButton(); 
    signUp = new javax.swing.JButton(); 
    wrong = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("Username:"); 

    jLabel2.setText("Password:"); 

    LogIn.setText("LogIn"); 
    LogIn.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      LogInActionPerformed(evt); 
     } 
    }); 

    signUp.setText("SignUp"); 
    signUp.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      signUpActionPerformed(evt); 
     } 
    }); 

    wrong.setText("Wrong Username And Password"); 
    wrong.setVisible(false); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(16, 16, 16) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
        .addGap(31, 31, 31) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) 
         .addComponent(password))) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(28, 28, 28) 
        .addComponent(LogIn) 
        .addGap(95, 95, 95) 
        .addComponent(signUp)) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(60, 60, 60) 
        .addComponent(wrong))) 
      .addContainerGap(146, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(51, 51, 51) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
       .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(18, 18, 18) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel2) 
       .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(52, 52, 52) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(LogIn) 
       .addComponent(signUp)) 
      .addGap(26, 26, 26) 
      .addComponent(wrong) 
      .addContainerGap(48, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

private boolean isPasswordValid(String password) 
{ 
    if(password.length()>8) 
     return true; 
    else 
     return false; 
} 

private boolean loginAttempt(String user, String pass){ 

    String usertext, passtext; 

    //get username and password 
    usertext = user; 
    passtext = pass; 

    //check username 
    if(usertext == null) 
    { 
     username.setText("Please add username"); 
    } 
    if(passtext == null) 
    { 
     password.setText("Please add password"); 
    } 
    else 
     if(isPasswordValid(passtext) == false) 
      password.setText("Password too short"); 

    //check for good data among dummy_data 
    for(String credentials:dummy_data) 
    { 
     String[] user_pass = credentials.split(":"); 
     if(user_pass[0].equals(usertext) && 
      user_pass[1].equals(passtext)) 
      { 
       return true; 
      } 
    } 

    return false; 
} 


private void LogInActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    String usertext, passtext; 
    usertext = username.getText().toString(); 
    passtext = password.getText().toString(); 
    if(loginAttempt(usertext,passtext) == true) 
    { 
     LogIn.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       WelcomeToTheServer view = new WelcomeToTheServer(); 
       view.setVisible(true); 
       throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
      } 

     }); 
    } 
    else 
    { 
     wrong.setVisible(true); 
    } 

}          

private void signUpActionPerformed(java.awt.event.ActionEvent evt) {          
    // start NewUser JFrame 
    signUp.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       NewUser new_userjframe = new NewUser(); 
       new_userjframe.setVisible(true); 
       throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
      } 

     }); 
}          

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(MainLogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(MainLogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(MainLogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(MainLogIn.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new MainLogIn().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton LogIn; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JPasswordField password; 
private javax.swing.JButton signUp; 
private javax.swing.JTextField username; 
private javax.swing.JLabel wrong; 
// End of variables declaration     

}

當我嘗試

LogIn.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       WelcomeToTheServer view = new WelcomeToTheServer(); 
       view.setVisible(true); 
       throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
      } 

     }); 

理智最多發生。我寫錯了ActionListener嗎?

謝謝

+0

*「我創建了幾個JFrame」* 1)使用['CardLayout'](http://download.oracle.com/javase/8/docs /api/java/awt/CardLayout.html),如[本答案](http://stackoverflow.com/a/5786005/418556)所示。 2)請參見[多個JFrames的使用,良好/錯誤的實踐?](http://stackoverflow.com/q/9554636/418556)3)請參見[檢測/修復代碼塊的懸掛閉合括號](http ://meta.stackexchange.com/q/251795/155831)對於一個問題,我不能再糾結於此。 –

回答

0

你初始化LogIn(和其它組件)的兩倍,使得第二覆蓋了你已經設置了

首先在initialiseComponents()其次,只要你回到


通過更新MainLogIn()構造函數:

public MainLogIn() { 
    initComponents(); 
} 

您可以刪除重複項和問題