JFrame更改的鼠标单击事件

时间:2019-09-13 07:59:03

标签: java swing jframe

我想使用JFrameMouseEvent窗口更改为另一个窗口。代码在下面。

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
String pass;
String user;
user = txtUser.getText();
pass = txtPass.getText();

if(pass.equals("********") && user.equals("**********") )
{
  ??????????
}
else{
    lblDisplay.setText("Please try again.");

1 个答案:

答案 0 :(得分:-1)

如果JFrameusername匹配,则需要转到另一个password,则可以执行以下操作。

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    String pass;
    String user;
    user = txtUser.getText();
    pass = txtPass.getText();

    if(pass.equals("********") && user.equals("**********"))
    {
        // Here you can dispose the current window
        this.dispose(); // Or you can use this.setVisible(false);

        // Then call your next window to appear
        new YourNextWindow().setVisible(true);

        // And of course you can create an object for that window,

        YourNextWindow yourNextWindow = new YourNextWindow();
        yourNextWindow.setVisible(true);
    } else {
        lblDisplay.setText("Please try again.");
    }
}

最好使用this.dispose();而不是使JFrame不可见setVisible(false),因为它会继续运行而无需任何使用。

更新

正如camickr所说,最好使用ActionListener而不是MouseListener

public void jButton1ActionPerformed(ActionEvent e) {
     // Your code here
}