Topic: 模仿QQ标题栏的例子

  Print this page

1.模仿QQ标题栏的例子 Copy to clipboard
Posted by: elliott
Posted on: 2005-01-29 20:46

已经搞定,谢谢kite的指点,不过偶那图标全是用 Label 贴上去的,我试过用button, 但风格老是设置不好,我把Button风格改为 SWT.NONE 或 SWT.NULL, 生成的button都是 push 风格,突出的那种。。。

还有个问题就是,设置拖动 title 的方式为,当 title 上产生 mouseDown 的事件时,我调用
Display.getCurrent().getCursorLocation() 取当前鼠标位置,然后设置 shell 的位置。

当点击鼠示移动时,shell的(0, 0)会到鼠标当前位置来,看起来不是很舒服。

总结ing... ^__^

1. Button如果设置风格为像 Label 一样? 另在 Button 和 Label 中不能又添加字体又添加图标。
2. 就是拖动 title 时设置 shell 的位置的问题。

源代码:

CoolShell.jar (22.43k)

2.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: elliott
Posted on: 2005-01-29 20:50



3.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: kite
Posted on: 2005-01-30 12:36

我帮你改了一下,这两个问题都可以解决,具体的办法参见修改过的代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.*;

import com.swtdesigner.ResourceManager;

/*
* ´´½¨ÈÕÆÚ 2005-1-29
*
* TODO Òª¸ü¸Ä´ËÉú³ÉµÄÎļþµÄÄ£°å£¬ÇëתÖÁ
* ´°¿Ú £­ Ê×Ñ¡Ïî £­ Java £­ ´úÂëÑùʽ £­ ´úÂëÄ£°å
*/

/**
* @author elliott
*
* TODO Òª¸ü¸Ä´ËÉú³ÉµÄÀàÐÍ×¢Ê͵ÄÄ£°å£¬ÇëתÖÁ
* ´°¿Ú £­ Ê×Ñ¡Ïî £­ Java £­ ´úÂëÑùʽ £­ ´úÂëÄ£°å
*/
public class CoolShell {

protected Shell shell;
private boolean mouseDown = false;

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

public void open() {
final Display display = Display.getDefault();
createContents(display);
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

protected void createContents(final Display display) {
shell = new Shell(SWT.NO_TRIM);
shell.setSize(150, 375);

final Label lblClose = new Label(shell, SWT.NONE);
lblClose.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
shell.dispose();
}
});
lblClose.addMouseTrackListener(new MouseTrackAdapter() {
public void mouseEnter(MouseEvent e) {
lblClose.setImage(ResourceManager.getImage(CoolShell.class, "icons/close2.bmp"));
}
public void mouseExit(MouseEvent e) {
lblClose.setImage(ResourceManager.getImage(CoolShell.class, "icons/close1.bmp"));
}
});

lblClose.setImage(ResourceManager.getImage(CoolShell.class, "icons/close1.bmp"));
lblClose.setBounds(121, 0, 16, 25);

final Label lblMax = new Label(shell, SWT.NONE);
lblMax.setImage(ResourceManager.getImage(CoolShell.class, "icons/max1.bmp"));
lblMax.setBounds(105, 0, 16, 25);

final Label lblMin = new Label(shell, SWT.NONE);
lblMin.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
shell.setMinimized(true);
}
});
lblMin.addMouseTrackListener(new MouseTrackAdapter() {
public void mouseEnter(MouseEvent e) {
lblMin.setImage(ResourceManager.getImage(CoolShell.class, "/icons/min2.bmp"));
}
public void mouseExit(MouseEvent e) {
lblMin.setImage(ResourceManager.getImage(CoolShell.class, "icons/min1.bmp"));
}
});

lblMin.setImage(ResourceManager.getImage(CoolShell.class, "/icons/min1.bmp"));
lblMin.setBounds(89, 0, 16, 25);

final Label lblTitle = new Label(shell, SWT.WRAP);
  //Demo for handle Shell drag
  Listener l = new Listener() {
    Point origin;
    public void handleEvent(Event e) {
      switch (e.type) {
        case SWT.MouseDown:
          origin = new Point(e.x, e.y);
          break;
        case SWT.MouseUp:
          origin = null;
          break;
        case SWT.MouseMove:
          if (origin != null) {
            Point p = display.map(shell, null, e.x, e.y);
            shell.setLocation(p.x - origin.x, p.y - origin.y);
          }
          break;
      }
    }
  };
  lblTitle.addListener(SWT.MouseDown, l);
  lblTitle.addListener(SWT.MouseUp, l);
  lblTitle.addListener(SWT.MouseMove, l);
  //lblTitle.setBackground(display.getSystemColor(SWT.COLOR_RED));
/*
  lblTitle.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (mouseDown) {
Point point = Display.getCurrent().getCursorLocation();
shell.setLocation(point);
}
}
});
lblTitle.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
mouseDown = true;
}
public void mouseUp(MouseEvent e) {
mouseDown = false;
}
});*/
lblTitle.setImage(ResourceManager.getImage(CoolShell.class, "icons/title.bmp"));
lblTitle.setBounds(0, 0, 150, 25);
  
  //Demo for mixed text and image in a label
  CLabel mixedLabel = new CLabel(shell,SWT.LEFT);
  mixedLabel.setText("MixedLabel");
  mixedLabel.setImage(ResourceManager.getImage(CoolShell.class, "icons/close1.bmp"));
  mixedLabel.setBounds(10,40,150,28);
}
}

4.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: mimixia
Posted on: 2005-02-11 14:39

qiang

5.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: cnfree
Posted on: 2005-02-17 21:53

代码有点乱,没有仔细看过,根据我自己的经验提供一点建议,采用Lable做按钮呢,当窗口拉伸或收缩的时候,采用Label.setImage方法,由于JVM的缘故,图片会闪动的特别厉害,比较好地解决办法是用GC来draw一个Image,而不是setImage方法。再就是不如写成组件的形式,Label中间设置一个热点区域,只有在这个区域内才能触发各种事件

6.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: hrliuhai
Posted on: 2005-03-01 16:10

这个jar怎么用啊?

7.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: hrliuhai
Posted on: 2005-03-01 16:18

原来是没下载对,用了另存才下对了

8.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: qingxing2005
Posted on: 2005-03-12 08:10

为什么下载了之后,运行起来看不了,老是说“couldn't find main class. Program exit.”

另外,我用SWT开发时,总是在建立了JAVA project之后,就选用Visual class中的JAVA.lang.object生成一个定义好的JAVA文件,大家都是么?这么做,有时候要用的display,但是老师有问题。该怎么办?这么做不会不好吧?

9.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: qingxing2005
Posted on: 2005-03-12 08:17

还有看到cnfree版主关于GC的用法,我也试过,可是老是预见错误。我很倾向于给button加图相,能不能提供一个简单的例子,GC+button的。

谢谢了

10.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: yangnian
Posted on: 2005-03-15 18:16

强 学习ing

11.Re:模仿QQ标题栏的例子 [Re: elliott] Copy to clipboard
Posted by: edwin1213
Posted on: 2005-04-13 16:31

顶下~多谢你的贴~正在学习中


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923