Eclipse WindowBuilder(SWT)插件安装及初次使用记录(萌新)

🏷️ nowgoal365live score 📅 2025-09-14 08:18:19 👤 admin 👀 6357 ⭐ 431
Eclipse WindowBuilder(SWT)插件安装及初次使用记录(萌新)

Eclipse WindowBuilder(SWT)插件安装及初次使用(萌新)

一、插件安装

(有VPN的挂VPN,服务器在外网更新下载比较慢)

1.首先更新到最新版本

点击Help,点击check for update,右下角显示查询进度,查询完毕会显示最新版内容。全部更新

2.还是在help内,点击eclipse marketplacea,搜索windowbuilder插件,安装。这个是图形化界面编程,内置swt和swing。我们用swt

二、创建project

在project explorer新建“other project”,快捷键ctrl+N,找到SWT/JFace java Project

点开,右键src,新建other

依次点击WindowsBuilder—SWT Designer—SWT—Application Window

空Project:

底部Source是源码,Design是图形化编程。Design里拖动组件,在源码中会同时显现代码。一般来说,在design中拖动组件(如按钮、文本框)等,然后添加监听(如点击等),运行的功能在source中给出代码

(更多用处还不清楚,今天第一天下载)

这边建议可以先看狂神的gui编程:

https://www.bilibili.com/video/BV1DJ411B75F?spm_id_from=333.337.search-card.all.click

虽然主要讲的是awt和swing但很多原理是共通的

二、自己摸索写了个gui计算a+b

很简单,三个label三个文本框,一个按钮。全都是可以拖动的

背景图片我没有找到在design中直接添加的办法,用代码添加。(随便挑了个图过来)

这个shell就是窗口数值代码

2.按钮监听

右键按钮组件,按上图添加监听。我随便选了个双击。

选完之后,返回source查看源代码,会发现多出一些代码:

中间就是要写当你双击时执行的代码

我写的是双击后执行A+B,同时不允许输入空格。

由于文本框输入都是String所以计算时多次强制转换。我认为应该还有更好的方式完成a+b这个简单的操作。哎

3.初次之外,还写了个文本框监听,让你无法输入英文和符号

下图为第一个文本框的监听,第二个相同,改一下文本框名字就行

4.自动换行

数字一长就会超出文本框显示,所以加了个自动换行的style

最终效果:

总结流程:design设计界面,并添加监听,执行代码得自己去source里写。

四、小结

有点gui基础会很轻松,不过后面老师应该也会交所以不急

网上关于swt下载安装教程太少了,(不过swt内实现一些功能的操作教学还是有不少的,可以ctrlcv了)文字版很多有问题。视频几乎没有。本文主要写一下下载方式和我第一天玩玩的基础操作。东西特别多,得慢慢学。

代码:

1 package testSWT;

2

3 import org.eclipse.swt.widgets.Display;

4 import org.eclipse.swt.widgets.Shell;

5 import org.eclipse.swt.widgets.Label;

6 import org.eclipse.jface.window.Window;

7 import org.eclipse.swt.SWT;

8 import org.eclipse.swt.widgets.Text;

9 import org.eclipse.swt.layout.GridLayout;

10 import org.eclipse.swt.custom.StackLayout;

11 import org.eclipse.swt.layout.FormLayout;

12 import org.eclipse.ui.forms.widgets.FormToolkit;

13 import org.eclipse.swt.widgets.Button;

14 import org.eclipse.swt.events.MouseAdapter;

15 import org.eclipse.swt.events.MouseEvent;

16 import org.eclipse.swt.widgets.Composite;

17 import org.eclipse.wb.swt.SWTResourceManager;

18 import org.eclipse.ui.forms.widgets.ImageHyperlink;

19 import org.eclipse.swt.widgets.Menu;

20 import org.eclipse.swt.widgets.MenuItem;

21 import org.eclipse.swt.events.MouseWheelListener;

22 import org.eclipse.swt.events.SelectionAdapter;

23 import org.eclipse.swt.events.SelectionEvent;

24 import org.eclipse.swt.events.VerifyListener;

25 import org.eclipse.swt.events.VerifyEvent;

26

27 public class testSWTAPP {

28

29 protected Shell shell;

30 private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());

31 private Text txtNewText;

32 private Text txtNewText_1;

33 private Text txtNewText_2;

34

35 /**

36 * Launch the application.

37 * @param args

38 */

39 public static void main(String[] args) {

40 try {

41 testSWTAPP window = new testSWTAPP();

42 window.open();

43 } catch (Exception e) {

44 e.printStackTrace();

45 }

46 }

47

48 /**

49 * Open the window.

50 */

51 public void open() {

52 Display display = Display.getDefault();

53 createContents();

54 shell.open();

55 shell.layout();

56 while (!shell.isDisposed()) {

57 if (!display.readAndDispatch()) {

58 display.sleep();

59 }

60 }

61 }

62

63 /**

64 * Create contents of the window.

65 */

66 protected void createContents() {

67 shell = new Shell(SWT.CLOSE|SWT.MIN);

68 shell.setImage(SWTResourceManager.getImage("C:\\Users\\wenwen\\Desktop\\CXTKZ026UV(LIF7]]YFG)CW.png"));

69 shell.setSize(466, 539);

70 shell.setText("Testing SWT...");

71 shell.setLayout(null);

72 shell.setBackgroundImage(SWTResourceManager.getImage("C:\\Users\\wenwen\\Desktop\\CXTKZ026UV(LIF7]]YFG)CW.png"));

73

74 Button button = formToolkit.createButton(shell, "\uFF08\u53CC\u51FB\uFF09\u6C42\u548C", SWT.NONE);

75

76

77 button.setForeground(SWTResourceManager.getColor(SWT.COLOR_LINK_FOREGROUND));

78 button.addMouseListener(new MouseAdapter() {

79 @Override

80 public void mouseDoubleClick(MouseEvent e) {

81 int flag = 0;

82 String A = txtNewText.getText();

83 if(A.contains(" "))flag=1;

84 String B= txtNewText_1.getText();

85 if(B.contains(" "))flag=1;

86

87 if(flag==1) {

88 txtNewText_2.setText("error,别tm输入空格啊小崽子");

89 }

90 else {

91 double a =Double.parseDouble(txtNewText.getText());

92 double b =Double.parseDouble(txtNewText_1.getText());

93 double c=a+b;String C=Double.toString(c);

94 txtNewText_2.setText(C);

95 }

96 }

97 });

98

99 button.setBounds(147, 372, 167, 34);

100

101 txtNewText = formToolkit.createText(shell,"", SWT.WRAP | SWT.MULTI);

102 txtNewText.addVerifyListener(new VerifyListener() {

103 public void verifyText(VerifyEvent e) {

104

105 try {

106 // 以Text中已经输入的内容创建StringBuffer对象

107 StringBuffer buffer = new StringBuffer(txtNewText.getText());

108 // 删除e.start, e.end指定范围的内容

109 // 并将要插入的内容e.text插入指定的位置,模拟输入e.text后Text对象中的内容

110 // 末尾添一个0,以保证buffer中只有一个字符为且为+-.时,不会触发NumberFormatException

111 buffer.delete(e.start, e.end).insert(e.start, e.text).append('0');

112 // 尝试将buffer中的内容转换成Float,如果不抛出异常说明输入内容有效

113 Double.valueOf(buffer.toString());

114 } catch (NumberFormatException ex) {

115 e.doit=false;

116 }

117 }

118 });

119 txtNewText.setBounds(259, 90, 151, 30);

120

121

122

123

124

125

126 txtNewText_1 = formToolkit.createText(shell, "", SWT.WRAP | SWT.MULTI);

127 txtNewText_1.addVerifyListener(new VerifyListener() {

128 public void verifyText(VerifyEvent e) {

129

130 try {

131 // 以Text中已经输入的内容创建StringBuffer对象

132 StringBuffer buffer = new StringBuffer(txtNewText_1.getText());

133 // 删除e.start, e.end指定范围的内容

134 // 并将要插入的内容e.text插入指定的位置,模拟输入e.text后Text对象中的内容

135 // 末尾添一个0,以保证buffer中只有一个字符为且为+-.时,不会触发NumberFormatException

136 buffer.delete(e.start, e.end).insert(e.start, e.text).append('0');

137 // 尝试将buffer中的内容转换成Float,如果不抛出异常说明输入内容有效

138 Double.valueOf(buffer.toString());

139 } catch (NumberFormatException ex) {

140 e.doit=false;

141 }

142 }

143 });

144 txtNewText_1.setBounds(259, 189, 151, 30);

145

146

147 txtNewText_2 = formToolkit.createText(shell, "", SWT.WRAP | SWT.MULTI);

148 txtNewText_2.setText("");

149 txtNewText_2.setBounds(259, 254, 151, 83);

150

151 Label lblA = formToolkit.createLabel(shell, "a:", SWT.CENTER);

152 lblA.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));

153 lblA.setBounds(107, 93, 90, 24);

154

155 Label lblB = formToolkit.createLabel(shell, "b:", SWT.CENTER);

156 lblB.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));

157 lblB.setBounds(107, 192, 90, 24);

158

159 Label lblA_1_1 = formToolkit.createLabel(shell, "a+b:", SWT.CENTER);

160 lblA_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));

161 lblA_1_1.setBounds(107, 290, 90, 24);

162

163 }

164 }

View Code

相关推荐 ✨

365bet苹果app KBOX(原新浪9158虚拟视频)

KBOX(原新浪9158虚拟视频)

📅 07-28 👀 6192
nowgoal365live score 电视市场中常用的7套“收视率”,谁最靠谱?
nowgoal365live score 慧与中国怎么样?有人面过吗?