打造仿淘寶注冊的Text(二)
上次貼了幾張圖片出來顯擺,這次徹底公布代碼~大家看看原理就好,有興趣的朋友可以和我聯系,把SWT里面的控件都封裝一下,做一套驗證框架出來~
大家可以看到整個類中沒有布局的出現,其實真的是“無布局才是最好的布局!”,沒有布局反倒更加的靈活了,方便談不上,但是靈活性大大提高;
Demo下載
客戶虐我千百遍,我待客戶如初戀!
1
package com.glnpu.dmp.controls;
2
3
import org.eclipse.swt.SWT;
4
import org.eclipse.swt.events.ControlEvent;
5
import org.eclipse.swt.events.ControlListener;
6
import org.eclipse.swt.events.ModifyEvent;
7
import org.eclipse.swt.events.ModifyListener;
8
import org.eclipse.swt.events.PaintEvent;
9
import org.eclipse.swt.events.PaintListener;
10
import org.eclipse.swt.graphics.GC;
11
import org.eclipse.swt.graphics.Image;
12
import org.eclipse.swt.widgets.Composite;
13
import org.eclipse.swt.widgets.Display;
14
import org.eclipse.swt.widgets.Text;
15
16
public class SuperText extends Composite implements PaintListener, ControlListener{
17
18
private Text text;
19
20
private Image tempCorner;
21
22
private Image tempMsg;
23
24
private Image warning_corner;
25
26
private Image ok_corner;
27
28
private Image error_corner;
29
30
private Image msg_warning;
31
32
private Image msg_ok;
33
34
private Image msg_error;
35
36
/**
37
* Create the composite
38
* @param parent
39
* @param style
40
*/
41
public SuperText(Composite parent, int style) {
42
super(parent, SWT.NONE);
43
initResource();
44
initControls(style);
45
this.addPaintListener(this);
46
this.addControlListener(this);
47
}
48
49
private void initResource() {
50
warning_corner = new Image(Display.getDefault(), "icons/input_warning_corner.gif");
51
ok_corner = new Image(Display.getDefault(), "icons/input_ok_corner.gif");
52
error_corner = new Image(Display.getDefault(), "icons/input_error_corner.gif");
53
54
msg_warning = new Image(Display.getDefault(), "icons/standard_msg_warning.gif");
55
msg_ok = new Image(Display.getDefault(), "icons/standard_msg_ok.gif");
56
msg_error = new Image(Display.getDefault(), "icons/standard_msg_error.gif");
57
58
tempCorner = warning_corner;
59
tempMsg = msg_warning;
60
}
61
62
private void initControls(int style) {
63
text = new Text(this, SWT.FLAT|style);
64
text.addModifyListener(new ModifyListener(){
65
public void modifyText(ModifyEvent e) {
66
if(SuperText.this.text.getText()!=null && !SuperText.this.text.getText().equals("")) {
67
if(SuperText.this.text.getText().length()>10) {
68
tempMsg = msg_ok;
69
tempCorner = ok_corner;
70
SuperText.this.redraw();
71
}else {
72
tempCorner = error_corner;
73
tempMsg = msg_error;
74
SuperText.this.redraw();
75
}
76
77
}else {
78
System.out.println("do here");
79
tempCorner = warning_corner;
80
tempMsg = msg_warning;
81
SuperText.this.redraw();
82
}
83
}
84
});
85
}
86
87
@Override
88
public void dispose() {
89
super.dispose();
90
}
91
92
@Override
93
protected void checkSubclass() {}
94
95
public void paintControl(PaintEvent e) {
96
System.out.println("paintControl");
97
GC gc = e.gc;
98
gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
99
gc.fillGradientRectangle(1, 1, this.getSize().x-11-tempMsg.getBounds().width, this.getSize().y-2, false);
100
gc.drawRectangle(1, 1, this.getSize().x-11-tempMsg.getBounds().width, this.getSize().y-2);
101
102
//set warning corner
103
gc.drawImage(tempCorner, this.getSize().x-11-tempMsg.getBounds().width-tempCorner.getBounds().width, this.getSize().y-2-tempCorner.getBounds().height);
104
105
//set msg warning
106
gc.drawImage(tempMsg, this.getSize().x-tempMsg.getBounds().width-5, 1);
107
gc.dispose();
108
}
109
110
public void controlMoved(ControlEvent e) {}
111
112
public void controlResized(ControlEvent e) {
113
text.setBounds(2, 2, this.getSize().x-12-tempMsg.getBounds().width-tempCorner.getBounds().width, this.getSize().y-3);
114
}
115
116
}
117
實現原理其實就是繪制!所謂自定義控件其實就是兩種,一種就是用C寫,然后JAVA調用,用SWT封裝;第二種就是繪制;第一種的做法其實并不好,會讓SWT的控件失去跨平臺性~一般的做法都是第二種。
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

大家可以看到整個類中沒有布局的出現,其實真的是“無布局才是最好的布局!”,沒有布局反倒更加的靈活了,方便談不上,但是靈活性大大提高;
Demo下載
客戶虐我千百遍,我待客戶如初戀!
posted on 2008-01-08 20:20 阿南 閱讀(1847) 評論(3) 編輯 收藏 所屬分類: Eclipse-RCP 、Eclipse-SWT 、個人原創