`
lw223
  • 浏览: 97831 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SWT/Jface学习笔记3(登录框)

阅读更多

下面是我做的一个登录框的小程序,当输入用户名(123)和密码(123)的时候会跳转到一个界面,用户名和密码错误的话会弹出一个提示对话框。

开发环境:eclipse3.1 插件SWT-Designer。

程序还有很多不完善的地方,请大家多多指正。

本来想把截图一起放在上面,可是不知道为什么图片贴不上来,谁能告诉我“编辑插入图像具体“怎么用啊,为什么我上传的图都是红XX

本来想把截图一起放在上面,可是不知道为什么图片贴不上来,谁能告诉我“编辑插入图像具体“怎么用啊,为什么我上传的图都是红XX,在这里先谢谢拉o(∩_∩)o...。

登陆框LoginSwt.java的代码:

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
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 LoginSwt ...{

    
public static void main(String[] args) ...{
        Display display 
= Display.getDefault();
        
final Shell shell = new Shell(display,SWT.CLOSE|SWT.SYSTEM_MODAL);
        
//shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
        shell.setSize(357218);
        shell.setText(
"SWT Application");
    
        shell.open();

        
final Label label = new Label(shell, SWT.NONE);
        
//label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
        
//label.setAlignment(SWT.CENTER);
        label.setText("登陆名:");
        label.setBounds(
68475119);

        
final Label label_1 = new Label(shell, SWT.NONE);
        
//label_1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
        
//label_1.setAlignment(SWT.CENTER);
        label_1.setText("密     码:");
        label_1.setBounds(
68725116);

        
final Text name = new Text(shell, SWT.BORDER);
        name.setBounds(
1364413120);

        
final Text password = new Text(shell, SWT.PASSWORD | SWT.BORDER);
        password.setBounds(
1366913119);

        Button button 
= new Button(shell, SWT.NONE);
        button.setText(
"登陆");
        button.setBounds(
1051264025);
        button.addSelectionListener(
new SelectionAdapter() ...{
            
public void widgetSelected(SelectionEvent arg0) ...{
                
if(name.getText().equals("123")&&password.getText().equals("123"))...{
                    
try...{
                        ChildShell window 
= new ChildShell();
                        window.open();
                        }
catch (Exception e) ...{
                            e.printStackTrace();
                        }

                }
else...{
                    MessageDialog.openWarning(shell,
null,  "用户名或密码错误");
                }

                    
            }

        }
);
        
//button.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
        button.addMouseListener(new MouseAdapter() ...{
            
public void mouseDoubleClick(MouseEvent arg0) ...{
                name.setText(
"鼠标双击事件");
            }

        }
);
        

        Button button_1 
= new Button(shell, SWT.NONE);
        button_1.setText(
"取消");
        button_1.setBounds(
2021274423);
        
//button_1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
        button_1.addMouseListener(new MouseAdapter() ...{
            
public void mouseDown(MouseEvent arg0) ...{
            MessageDialog.openWarning(shell,
null,  "没有此项功能");
            }

        }
);
        

        shell.layout();
        
while (!shell.isDisposed()) ...{
            
if (!display.readAndDispatch())
                display.sleep();
        }

    }

}

恢复被注掉的代码可以显示界面和控件的颜色。

 

        button.addSelectionListener(new SelectionAdapter() ...{
            
public void widgetSelected(SelectionEvent arg0) ...{
                
if(name.getText().equals("123")&&password.getText().equals("123"))...{
                    
try...{
                        ChildShell window 
= new ChildShell();
                        window.open();
                        }
catch (Exception e) ...{
                            e.printStackTrace();
                        }

                }
else...{
                    MessageDialog.openWarning(shell,
null,  "用户名或密码错误");
                }

                    
            }

        }
);

这段代码是给“登录”按钮添加一个事件,if(name.getText().equals("123")&&password.getText().equals("123"))

是建立用户名和密码是123,并判断,如果输入的用户名和密码都是123,则跳转到try里面内容所指向的界面及ChildShell.java,如果错误则弹出一个Dialog,显示"用户名或密码错误"。

ChildShell.java的代码:

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ChildShell ...{

    
public static void main(String[] args) ...{
        
try ...{
            ChildShell window 
= new ChildShell();
            window.open();
        }
 catch (Exception e) ...{
            e.printStackTrace();
        }

    }


    
/** *//**
     * Open the window
     
*/

    
public void open() ...{
        
final Display display = Display.getDefault();
        
final Shell shell = new Shell();
        shell.setSize(
500375);
        shell.setText(
"SWT Application");
        

        shell.open();
分享到:
评论
1 楼 ktzhxm 2012-05-07  
多谢分享,对初学者的我有帮助。登陆后弹出新的窗口,怎么把原来的登陆界面给关闭了?

相关推荐

Global site tag (gtag.js) - Google Analytics