[英]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:这样做的好处:
getSource()
method is set equal togetSource()
方法的设置等于
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.