酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java SWT:可直接运行的简单登录界面实现代码

下面是使用 SWT 实现的一个简单的登录界面示例,你可以直接运行。需要注意的是,你需要先下载并导入 SWT 库才能编译和运行该示例。 ```java import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.events.*; public class LoginWindow { private Text usernameText; private Text passwordText; private Shell shell; public LoginWindow(Display display) { shell = new Shell(display, SWT.CLOSE); shell.setText("登录"); createComponents(); shell.pack(); shell.open(); } private void createComponents() { Label usernameLabel = new Label(shell, SWT.NONE); usernameLabel.setText("用户名:"); usernameLabel.setBounds(10, 20, 60, 25); usernameText = new Text(shell, SWT.BORDER); usernameText.setBounds(80, 20, 150, 25); Label passwordLabel = new Label(shell, SWT.NONE); passwordLabel.setText("密码:"); passwordLabel.setBounds(10, 60, 60, 25); passwordText = new Text(shell, SWT.BORDER | SWT.PASSWORD); passwordText.setBounds(80, 60, 150, 25); Button loginButton = new Button(shell, SWT.PUSH); loginButton.setText("登录"); loginButton.setBounds(80, 100, 60, 25); loginButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String username = usernameText.getText(); String password = passwordText.getText(); if (username.equals("admin") && password.equals("123456")) { MessageBox messagebox = new MessageBox(shell); messagebox.setMessage("登录成功"); messagebox.open(); } else { MessageBox messagebox = new MessageBox(shell, SWT.ICON_ERROR); messagebox.setMessage("登录失败"); messagebox.open(); } } }); } public static void main(String[] args) { Display display = new Display(); new LoginWindow(display); while (!display.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ``` 这段代码将创建一个简单的登录界面,其中包含用户名输入框、密码输入框和登录按钮。当点击登录按钮时,程序会检查用户名和密码是否正确,如果正确则显示“登录成功”对话框,否则显示“登录失败”对话框。 ######[AI写代码神器 | 1680点数解答 | 2024-05-27 18:08:41]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]