简体   繁体   English

JAVA GUI - 单击时我的提交按钮没有响应

[英]JAVA GUI - My submit button does not respond when clicked

I am attempting to create a login GUI and when I press the submit button nothing happens.我正在尝试创建一个登录 GUI,当我按下提交按钮时没有任何反应。 The other two buttons work as intended.其他两个按钮按预期工作。 I have attempted to add in a JOptionPane, but that seems to do nothing as well.我试图添加一个 JOptionPane,但这似乎也无济于事。 Does anyone have any tips?有没有人有任何提示?

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
    shell.setSize(450, 300);
    shell.setText("Homework 1 GUI");
    
    CLabel lblHomework1Login = new CLabel(shell, SWT.NONE);
    lblHomework1Login.setBounds(141, 10, 149, 26);
    lblHomework1Login.setText("Homework 1 Login");
    
    CLabel lblUsername = new CLabel(shell, SWT.NONE);
    lblUsername.setBounds(10, 74, 76, 26);
    lblUsername.setText("Username");
    
    CLabel lblPassword = new CLabel(shell, SWT.NONE);
    lblPassword.setBounds(10, 129, 76, 26);
    lblPassword.setText("Password");
    
    txtUsername = new Text(shell, SWT.BORDER);
    txtUsername.setBounds(117, 74, 255, 26);
    
    txtPassword = new Text(shell, SWT.BORDER);
    txtPassword.setBounds(116, 129, 256, 26);
    
    Button btnSubmit = new Button(shell, SWT.BORDER);
    btnSubmit.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
    btnSubmit.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            String password = txtPassword.getText();
            String username = txtUsername.getText();
            
            // Implementing an action event listener class with conditional statement
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String username = txtUsername.getText();
                String password = txtPassword.getText();

                if (username.equals("student1") && password.equals("987123"))
                    JOptionPane.showMessageDialog(null, "Login Successful");
                else
                    JOptionPane.showMessageDialog(null, "Username or Password mismatch ");
            }
            
            
        }
    });

Newest Edit and I am presented with the following error:Syntax error on token "MessageDialog", delete this token.最新编辑,我收到以下错误:令牌“MessageDialog”上的语法错误,删除此令牌。 Also the submit button still does not work.此外,提交按钮仍然不起作用。 Thanks to all who have helped.感谢所有帮助过的人。

// Implementing an selection event listener class with conditional statement
            @Override
            public void handle(SelectionEvent onClick) {
                String Username = txtUsername.getText();
                String Password = txtPassword.getText();

                if (onClick.getSource() == btnSubmit)(Username.equals("student1") && Password.equals("987123"))
                    MessageDialog.openInformation(shell, "Login", "Login Successful");
                else
                    MessageDialog.openError(shell, "Login", "Username or Password mismatch");
            }
            
        }
    );
    
    

The code you show doesn't compile, you can't have a actionPerformed method nested in a widgetSelected method like that.您显示的代码无法编译,您不能将actionPerformed方法嵌套在这样的widgetSelected方法中。

JOptionPane is a Java Swing class, do not try to mix Swing with SWT, they are different GUIs and are hard to make work together. JOptionPane是一个 Java Swing 类,不要尝试将 Swing 与 SWT 混合,它们是不同的 GUI,很难一起工作。

This code works for me:这段代码对我有用:

shell.setSize(450, 300);
shell.setText("Homework 1 GUI");

shell.setLayout(new GridLayout(2, false));

Label lblHomework1Login = new Label(shell, SWT.NONE);
lblHomework1Login.setText("Homework 1 Login");
lblHomework1Login.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 2, 1));

Label lblUsername = new Label(shell, SWT.NONE);
lblUsername.setText("Username");

Text txtUsername = new Text(shell, SWT.BORDER);
txtUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

Label lblPassword = new Label(shell, SWT.NONE);
lblPassword.setText("Password");

Text txtPassword = new Text(shell, SWT.BORDER);
txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

Button btnSubmit = new Button(shell, SWT.BORDER);
btnSubmit.setText("Submit");
btnSubmit.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(final SelectionEvent e) {
        String password = txtPassword.getText();
        String username = txtUsername.getText();

        if (username.equals("student1") && password.equals("987123"))
          MessageDialog.openInformation(shell, "Login", "Login Successful");
        else
          MessageDialog.openError(shell, "Login", "Username or Password mismatch");
    }
});

Your btnSubmit had no text, I added some.您的btnSubmit没有文本,我添加了一些。

I have use the JFace MessageDialog for the messages, if you don't want to use JFace you could use the SWT MessageBox instead.我已经为消息使用了 JFace MessageDialog ,如果您不想使用 JFace,您可以改用 SWT MessageBox

I have use Layouts rather than setBounds.我使用的是 Layouts 而不是 setBounds。 setBounds should be avoided as it will not work well with different fonts.应避免使用 setBounds,因为它不适用于不同的字体。

To use MessageBox use:要使用MessageBox ,请使用:

 btnSubmit.addSelectionListener(new SelectionAdapter() {
     @Override
     public void widgetSelected(final SelectionEvent e) {
         String password = txtPassword.getText();
         String username = txtUsername.getText();

         MessageBox msgBox = new MessageBox(shell);
         if (username.equals("student1") && password.equals("987123"))
           msgBox.setMessage("Login Successful");
         else
           msgBox.setMessage("Username or Password mismatch");
         msgBox.open();
    }
 });

Whenever I used the Button class in my Java GUIs, I would always set up the buttons as so:每当我在 Java GUI 中使用Button类时,我总是会这样设置按钮:

Button submitButton = new Button("Submit Button")
submitButton.setOnAction(this);

Then you would need the code to "hear" when the button is pressed.然后你需要代码在按下按钮时“听到”。 The following method "hears" that the button is pressed, then runs the following code:以下方法“听到”按钮被按下,然后运行以下代码:

@Override
    public void handle(ActionEvent onClick) {
        if (onClick.getSource() == submitButton)
        {
             // Do whatever you need to do
        }
    }

Perks of doing it this way:这样做的好处:

  • There is a lot less code to deal with需要处理的代码要少得多
  • All you need to do is change what the getSource() method is set equal to您需要做的就是更改getSource()方法的设置等于
  • It's easier to read更容易阅读

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

相关问题 JAVA GUI:有人可以帮我检查为什么我的按钮没有响应被点击吗? - JAVA GUI: Can someone help me check why my button doesn't respond to it being clicked? 单击提交按钮时不执行任何操作 - Submit button does nothing when clicked 为什么我的列表视图仅在单击文本时才响应? - Why does my listview only respond when it's text is clicked? 在Java中单击按钮时如何返回上一个GUI - How to go back to the previous GUI when a button is clicked in Java GUI提交按钮不起作用-Java - GUI submit button not working- Java 我正在从数据库中填充我的下拉列表,但是选择后,当单击提交按钮时,在我的Java代码中存在的方法中我得到了空值 - I am populating my dropdown from DB but after selecting I am getting null value in my method present in my java code when submit button is clicked 在GUI的actionListener中按下按钮时,图像无法打开? - image does not open when button is pressed in actionListener in my GUI? 在Swing Java GUI应用程序中单击时如何使按钮保持按下状态 - How to make a button stay depressed when clicked in a Swing Java GUI app 为什么单击按钮后我的应用程序崩溃? - Why does my App crash, when a Button is clicked? 单击按钮时无法在JLabel中显示图像-Java - Having trouble displaying an image in my JLabel when button is clicked - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM