swt中的事件详解

这里主要介绍下swt中的MouseEvent鼠标事件和KeyEvent键盘事件。

下面是swt中键盘事件案例的运行效果:

GIF.gif

相关的java:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

package swt;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.KeyAdapter;

import org.eclipse.swt.events.KeyEvent;

import org.eclipse.swt.events.MouseEvent;

import org.eclipse.swt.events.MouseListener;

import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.junit.Test;

public class EventDemo_07 {

     

    /**

     * 指哪到哪

     */

    @Test

    public void testMouseEvent(){

            Display display=new Display();

            Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);

            topShell.setText("测试鼠标监听事件");

            topShell.setSize(800500);

             

            final Button button=new Button(topShell, SWT.NONE);

            button.setText("移动");

            button.setBounds(10010010030);

             

             

//          new MouseMoveListener() {

//             

//              @Override

//              public void mouseMove(MouseEvent e) {

//                 

//              }

//          };

            /*

            button.addMouseListener(new MouseListener() {

                 

                @Override

                public void mouseUp(MouseEvent e) {

                    //e.x和e.y为鼠标按键起来时相对于按钮左上角那个点的距离

                    int x = e.x;

                    int y = e.y;

                    System.out.println(x+"  "+y);

                    button.setBounds(x, y, 100, 30);

                }

                 

                @Override

                public void mouseDown(MouseEvent e) {

                     

                }

                 

                @Override

                public void mouseDoubleClick(MouseEvent e) {

                     

                }

            });

            */

            topShell.addMouseListener(new MouseListener() {

                 

                @Override

                public void mouseUp(MouseEvent e) {

                    System.out.println(e.x+"  "+e.y);

                    Rectangle rect = button.getBounds();

                     

                    Rectangle rectangle=new Rectangle(e.x, e.y, rect.width, rect.height);

                    button.setBounds(rectangle);

                }

                 

                @Override

                public void mouseDown(MouseEvent e) {

                     

                }

                 

                @Override

                public void mouseDoubleClick(MouseEvent e) {

                     

                }

            });

             

            topShell.open();

            while(!topShell.isDisposed()){

                if (!display.readAndDispatch()) {

                    display.sleep();

                }

            }

            display.dispose();

    }

    /**

     * 功能:按着ctrl+箭头可以移动按钮

     */

    @Test

    public void testKeyListener(){

        Display display=new Display();

        Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);

        topShell.setText("测试键盘监听事件");

        topShell.setSize(800500);

         

        Button button=new Button(topShell, SWT.NONE);

        button.setText("移动");

        button.setBounds(10010010030);

         

        button.addKeyListener(new KeyAdapter() {

            @Override

            public void keyPressed(KeyEvent e) {

                Button btn= (Button) e.widget;

                Rectangle bounds = btn.getBounds();

                //键盘事件特有的属性,表示辅助键:crtl或alt或shift

                int stateMask = e.stateMask;

                int sum = SWT.CTRL|SWT.ALT|SWT.SHIFT;

                int flag=stateMask∑

                if (flag==SWT.CTRL) {

                    if (e.keyCode==SWT.ARROW_DOWN) {

                        bounds.y++;

                    }else if (e.keyCode==SWT.ARROW_UP) {

                        bounds.y--;

                    }else if (e.keyCode==SWT.ARROW_LEFT) {

                        bounds.x--;

                    }else if (e.keyCode==SWT.ARROW_RIGHT) {

                        bounds.x++;

                    }else {

                        //键盘事件特有的属性,表示e.character表示可输入的字符

                        System.out.println("usage:ctrl+上|下|左|右"+e.character

                        +"   ---  "+e.keyCode);

                    }

                }else {

                    System.out.println("请使用ctrl辅助键");

                }

                System.out.println("输入的:"+e.character+"   ---  "+e.keyCode);

                btn.setBounds(bounds);

            }

             

        });

         

         

        topShell.open();

        while(!topShell.isDisposed()){

            if (!display.readAndDispatch()) {

                display.sleep();

            }

        }

        display.dispose();

    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值