简体   繁体   English

SetText function in netbeans

[英]SetText function in netbeans

I am trying to use setText function in netbeans.我正在尝试在 netbeans 中使用 setText function。 So there is a login page if the user login to the page with the correct credentials, (in this case that is only admin) in the next page (which is the main page) there should be a message with Welcome User I am trying to change the label to Welcome user in the main page因此,如果用户使用正确的凭据登录到该页面,则会有一个登录页面,(在这种情况下,这只是管理员)在下一页(这是主页)中应该有一条带有欢迎用户的消息,我正在尝试在主页中将 label 更改为 Welcome user

Backend of the login button登录按钮的后端

private void loginMouseClicked(java.awt.event.MouseEvent evt) {                                   
       if(loginUsername.getText().toString().trim().length() == 0 && loginPassword.getText().toString().trim().length() == 0){
            int opt = JOptionPane.showConfirmDialog(null, "All fields are required", "Alert", JOptionPane.CLOSED_OPTION);
        }
       else if((loginUsername.getText().toString().equals("admin") && loginPassword.getText().toString().equals("admin"))){
           new MainPage().setVisible(true);
            this.setVisible(false);
       }
       else{
           int opt = JOptionPane.showConfirmDialog(null, "Username or Password Incorrect", "Alert", JOptionPane.CLOSED_OPTION);
       }
    }  

Code that i tried in the Main Page我在主页中尝试过的代码

SignUp signUp = new SignUp();
        MainPage mainPage = new MainPage();
        mainPage.setVisible(true);
       mainPage.jLabel2.setText("Welcome" + signUp.loginUsername.getText().toString());
  1. Don't use a MouseListener.不要使用 MouseListener。 You should add an ActionListner to the button on your login page.您应该将ActionListner添加到登录页面上的按钮。 The ActionListener will be invoked when you click on your Login button.当您单击登录按钮时,将调用ActionListener

  2. The second block of code you posted should be in the ActionListener code of your login button.您发布的第二个代码块应该在登录按钮的ActionListener代码中。

So the code should be something like:所以代码应该是这样的:

else if((loginUsername.getText().equals("admin") && loginPassword.getText().equals("admin")))
{
    MainPage mainPage = new MainPage();
    mainPage.jLabel2.setText("Welcome" + loginUsername.getText());
    mainPage.setVisible(true);
    this.setVisible(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM