java中 简单的登录代码!(SWT 可视化界面)
package Denglv;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Login {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell();
shell.setBounds(30, 75, 300, 400);
shell.setText("登陆界面");
// ------------------新插入的界面核心代码------------------------
//创建触发按钮以及按钮的显示文字和大小位置
Button okButton = new Button(shell,SWT.NONE);
Button registerButton = new Button(shell,SWT.NONE);
okButton.setText("确定");
registerButton.setText("注册");
registerButton.setBounds(160, 235, 70, 25);
okButton.setBounds(60, 235, 70, 25);
final Text nameText = new Text(shell,SWT.NONE);
final Text passNumber = new Text(shell,SWT.PASSWORD);
nameText.setBounds(120, 100, 110, 20);
passNumber.setBounds(120, 160, 110, 20);
Label nameLabel = new Label(shell,SWT.NONE);
nameLabel.setText("用户名:");
nameLabel.setBounds(65, 100, 40,20);
Label passLabel = new Label(shell,SWT.NONE);
passLabel.setText("密 码:");
passLabel.setBounds(65, 165, 40, 20);
//输入后单击确定后的操作
okButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
//创建一个adm数组
Users[] user = new Users[3];
user[0] = new Users("123", "123");
user[1] = new Users("123456", "123456");
user[2] = new Users("123456789", "123456789");
int j=-1;
//判断账号密码是否正确
for(int i=0;i<user.length;i++){
if(user[i].getName().equals(nameText.getText())&&user[i].getPastNumber().equals(passNumber.getText())){
j = 0;
break;
}
}
if(j==0){
MessageDialog.openWarning(shell, "恭喜", "欢迎"+nameText.getText()+"登录");
}
else{
MessageDialog.openError(shell, "错误","请输入正确的用户名或密码!");
}
}
}
);
registerButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){{
//用一个错误对话框来提醒该功能还没有实现
MessageDialog.openError(shell, "错误","该功能还没有实现!");
}}});
//-----------------------END-----------------------------------
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
class Users{
private String username;
private String pastNumber;
Users(String name, String pastNumber){
this.username = name;
this.pastNumber = pastNumber;
}
public String getName() {
return username;
}
public void setName(String name) {
this.username = name;
}
public String getPastNumber() {
return pastNumber;
}
public void setPastNumber(String pastNumber) {
this.pastNumber = pastNumber;
}
}
运行结果:
情况如下
1、
2、
3、
感谢阅读!!!!!希望提供宝贵意见!!!!QQ:237333696
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Login {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell();
shell.setBounds(30, 75, 300, 400);
shell.setText("登陆界面");
// ------------------新插入的界面核心代码------------------------
//创建触发按钮以及按钮的显示文字和大小位置
Button okButton = new Button(shell,SWT.NONE);
Button registerButton = new Button(shell,SWT.NONE);
okButton.setText("确定");
registerButton.setText("注册");
registerButton.setBounds(160, 235, 70, 25);
okButton.setBounds(60, 235, 70, 25);
final Text nameText = new Text(shell,SWT.NONE);
final Text passNumber = new Text(shell,SWT.PASSWORD);
nameText.setBounds(120, 100, 110, 20);
passNumber.setBounds(120, 160, 110, 20);
Label nameLabel = new Label(shell,SWT.NONE);
nameLabel.setText("用户名:");
nameLabel.setBounds(65, 100, 40,20);
Label passLabel = new Label(shell,SWT.NONE);
passLabel.setText("密 码:");
passLabel.setBounds(65, 165, 40, 20);
//输入后单击确定后的操作
okButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
//创建一个adm数组
Users[] user = new Users[3];
user[0] = new Users("123", "123");
user[1] = new Users("123456", "123456");
user[2] = new Users("123456789", "123456789");
int j=-1;
//判断账号密码是否正确
for(int i=0;i<user.length;i++){
if(user[i].getName().equals(nameText.getText())&&user[i].getPastNumber().equals(passNumber.getText())){
j = 0;
break;
}
}
if(j==0){
MessageDialog.openWarning(shell, "恭喜", "欢迎"+nameText.getText()+"登录");
}
else{
MessageDialog.openError(shell, "错误","请输入正确的用户名或密码!");
}
}
}
);
registerButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){{
//用一个错误对话框来提醒该功能还没有实现
MessageDialog.openError(shell, "错误","该功能还没有实现!");
}}});
//-----------------------END-----------------------------------
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
class Users{
private String username;
private String pastNumber;
Users(String name, String pastNumber){
this.username = name;
this.pastNumber = pastNumber;
}
public String getName() {
return username;
}
public void setName(String name) {
this.username = name;
}
public String getPastNumber() {
return pastNumber;
}
public void setPastNumber(String pastNumber) {
this.pastNumber = pastNumber;
}
}
运行结果:
情况如下
1、
2、
3、
感谢阅读!!!!!希望提供宝贵意见!!!!QQ:237333696