查看: 2357|回复: 0

JAVA编的简单QQ界面

[复制链接]

该用户从未签到

发表于 2008-7-7 22:49:01 | 显示全部楼层 |阅读模式
分享到:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class QQ {
    /**
     * 为了自定义监听器类MyMouseLister中的代码能访问到以下对象,故将这些对象定义成 类的实例变量
     */
    private StackLayout stackLayout = new StackLayout();
    private Composite yourDataComp;
    private Composite otherComp;
    private List selectList;
    private Composite rightComp;
    /**
     * 由于自定义方法较多程序较长,这次的主程序框架选择了Eclipse“新建”向导的第二种: “public open() mothd”
     */
    public static void main(String[] args) {
        try {
            QQ window = new QQ();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void open() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(550, 350);
        shell.setText("个人设置");
        shell.setLayout(new GridLayout());
        {
            //分割窗口
            SashForm sashForm = new SashForm(shell, SWT.BORDER);
            sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
            {
                //分割窗左边的列表框
                selectList = new List(sashForm, SWT.BORDER);
                //作为演示只加了两项
                selectList.setItems(new String[] { "个人资料", "联系方式" });
                //加一个鼠标监听器
                selectList.addMouseListener(new MyMouseLister());//
            }
            {
                //右边的堆栈式面板
                rightComp = new Composite(sashForm, SWT.NONE);
                rightComp.setLayout(stackLayout);
                //共两页。将生成此面板的代码提出成一个方法,保证代码结构的清晰。
                yourDataComp = createYourDataComp(rightComp);//个人资料的面板
                otherComp = createOtherComp(rightComp);//联系方式的面板
                //在堆栈面板上先显示“个人资料”界面
                stackLayout.topControl = yourDataComp;
            }
            //分割窗口的左右空间比例
            sashForm.setWeights(new int[] { 1, 4 });
        }
        {
            //界面的按钮的面板
            Composite buttonComp = new Composite(shell, SWT.BORDER);
            //使用GridData设置buttonComp在它父容器Shell中的布局方式
            GridData gridData = new GridData();
            gridData.horizontalAlignment = GridData.END;//让buttonComp向右靠
            buttonComp.setLayoutData(gridData);
            //设置buttonComp的布局为RowLayout,用来设定buttonComp内组件的布局方式
            RowLayout rowLayout = new RowLayout();
            rowLayout.spacing = 15;//按钮之间间隔15个像素
            buttonComp.setLayout(rowLayout);
            //在buttonComp下建立三个按钮
            new Button(buttonComp, SWT.NONE).setText("    确定    ");
            new Button(buttonComp, SWT.NONE).setText("    取消    ");
            new Button(buttonComp, SWT.NONE).setText("    应用    ");
        }
        shell.layout();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
    /** 个人资料面板的生成 */
    private Composite createYourDataComp(Composite rightComp) {
        Composite composite = new Composite(rightComp, SWT.NONE);
        composite.setLayout(new GridLayout(6, false));//个人资料面板分成6列
        //---------用户号码的标签及其文本框
        new Label(composite, SWT.NONE).setText("用户号码:");
        //只读型的文本框
        Text text = new Text(composite, SWT.READ_ONLY | SWT.BORDER);
        //水平抢占式充满,并占用三列的空间. createGridData是自定义方法
        text.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 3));
        //---------图片部份.我们再用一个面板嵌套来管理
        Composite photoComp = new Composite(composite, SWT.BORDER);
        //水平垂直对齐式充满,横占两列,竖占4行.
        photoComp.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL, 2, 4));
        photoComp.setLayout(new GridLayout(2, false));//分2列
        {
            //图片. Image类的使用暂时还没有讲到,我们先用一个Composite来代替图片.
            Composite tempComp = new Composite(photoComp, SWT.BORDER);
            tempComp.setLayoutData(new GridData(50, 50));//设定大小:宽50,高50
            //选择图片的箭头型按钮,并设置它向下靠
            Button selPhotoButton = new Button(photoComp, SWT.ARROW | SWT.DOWN);
            selPhotoButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
            //升级成为会员的按钮.横占photoComp的两列,并横向对齐充满
            Button updateButton = new Button(photoComp, SWT.NONE);
            updateButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 2));
            updateButton.setText("升级成为会员");
        }
        new Label(composite, SWT.NONE).setText("用户呢称:");
        Text nicknameText = new Text(composite, SWT.BORDER);
        nicknameText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
        new Label(composite, SWT.NONE).setText("个性签名:");
        Text attachNameText = new Text(composite, SWT.BORDER);
        attachNameText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
        new Label(composite, SWT.NONE).setText("等    级:");
        {
            //Image类的使用暂时还没有讲到,我们先用一个Composite来代替图片
            Composite tempComp = new Composite(composite, SWT.BORDER);
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
            gridData.horizontalSpan = 3;
            //Composite默认的高度太高,故手工设定高度为20像素
            gridData.heightHint = 20;
            tempComp.setLayoutData(gridData);
        }
        new Label(composite, SWT.NONE).setText("性    别:");
        Combo sexCombo = new Combo(composite, SWT.NONE);
        new Label(composite, SWT.NONE).setText("姓名:");
        Text nameText = new Text(composite, SWT.BORDER);
        nameText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        new Label(composite, SWT.NONE).setText("年龄:");
        Text oldText = new Text(composite, SWT.BORDER);
        oldText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        new Label(composite, SWT.NONE).setText("毕业院校:");
        Text schoolText = new Text(composite, SWT.BORDER);
        schoolText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
        new Label(composite, SWT.NONE).setText("生肖:");
        Combo animalCombo = new Combo(composite, SWT.NONE);
        animalCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        new Label(composite, SWT.NONE).setText("职    业:");
        Text jobText = new Text(composite, SWT.BORDER);
        jobText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
        new Label(composite, SWT.NONE).setText("星座:");
        Combo constellationCombo = new Combo(composite, SWT.NONE);
        constellationCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
        Label introLabel = new Label(composite, SWT.NONE);
        //默认是居中,改为顶端对齐
        introLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        introLabel.setText("个人说明:");
        Text introText = new Text(composite, SWT.BORDER | SWT.WRAP);//WRAP自动换行
        introText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL, 5));
        //返回个人资料面板composite
        return composite;
    }
    /** 生成一个简单的联系方式的面板 */
    private Composite createOtherComp(Composite rightComp) {
        Composite composite = new Composite(rightComp, SWT.NONE);
        composite.setLayout(new FillLayout());
        new Label(composite, SWT.NONE).setText("联系方式面板");
        return composite;
    }
    /**
     * 生成GridData对象的重复代码太多,写成一个方法可以减少程序的行数,用起来也方便
     * 而且还要注意一个GridData只能被一个组件使用,不能两个组件使用一个GriData, 则 有一个组件将不会显示出来
     */
    private GridData createGridData(int style, int horizontalSpan) {
        GridData gridData = new GridData(style);
        gridData.horizontalSpan = horizontalSpan;
        return gridData;
    }
    private GridData createGridData(int style, int horizontalSpan, int verticalSpan) {
        GridData gridData = new GridData(style);
        gridData.horizontalSpan = horizontalSpan;
        gridData.verticalSpan = verticalSpan;
        return gridData;
    }
    /** 鼠标监听器,采用事件的命名内部类的写法 */
    private class MyMouseLister extends MouseAdapter {
        public void mouseDown(MouseEvent e) {
            //得到列表的当前选择项的序号(即单击的那项)
            int selectionIndex = selectList.getSelectionIndex();
            //如果单击第一项"个人资料",则让相对应的面板移至最上面
            if (selectionIndex == 0)
                stackLayout.topControl = yourDataComp;
            else
                stackLayout.topControl = otherComp;
            rightComp.layout();//刷新堆栈式布局的顶容器
        }
    };
}
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-12-28 09:16 , Processed in 0.121596 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.