门户网站开发案例,阿里云建站中级版和高级版,禹城市建设局网站,浙江城乡和住房建设网实现控件的刷新
问题可以简化如下#xff0c;点击上方按钮#xff0c;使下方按钮移动#xff0c;但要求在监听事件里新建按钮对象#xff0c;而不是使用原来的按钮#xff08;原来的按钮被移除了#xff09;。 解决代码如下#xff1a;
public class TestUI {protecte…实现控件的刷新
问题可以简化如下点击上方按钮使下方按钮移动但要求在监听事件里新建按钮对象而不是使用原来的按钮原来的按钮被移除了。 解决代码如下
public class TestUI {protected Shell shell;Composite compositenull; int i0;public static void main(String[] args) {try {TestUI window new TestUI();window.open();} catch (Exception e) {e.printStackTrace();}}public void open() {Display display Display.getDefault();createContents();shell.open();shell.layout();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}}protected void createContents() {shell new Shell();shell.setMinimumSize(new Point(100, 20));shell.setMaximumSize(new Point(500, 400));shell.setSize(486,143);shell.setText(SWT Application);shell.setLayout(null);update();Button button new Button(shell, SWT.NONE);button.addSelectionListener(new SelectionAdapter() {Overridepublic void widgetSelected(SelectionEvent e) {update();i;}}); button.setBounds(10, 10, 80, 27);button.setText(\u79FB\u52A8);}void update() {if(composite!null)composite.dispose();composite new Composite(shell, SWT.BORDER);composite.setBounds(10, 43, 450, 52);Button btnNewButton new Button(composite, SWT.NONE);System.out.print(i);btnNewButton.setBounds(30*i, 10, 29, 27);}
}
扩展栏扩展项切换打开关闭
要实现打开一个扩展项时关闭其他扩展项需要给扩展栏添加扩展监听器。 关键代码如下
expandBar.addExpandListener(new ExpandListener() { Overridepublic void itemExpanded(ExpandEvent arg0) {// TODO Auto-generated method stubSystem.out.println(expanded);for(ExpandItem item:expandBar.getItems()) {item.setExpanded(false);//这里循环设置其他扩展项关闭}} Overridepublic void itemCollapsed(ExpandEvent arg0) {// TODO Auto-generated method stub 这个不用管}});
日期时间设置
DateTime对象的getMonth()方法返回的月份从0一月到1112月。
设置日期时用setDate(int year,int month,int day)方法比较方便合法的日期设置不会出现预料之外的结果month的取值范围在0-11之间。
而如果年月日分别用setYear(),setMonth(),setDay()方法年份无所谓但月和日的结果可能受二者的先后次序影响。看如下示例
DateTime dateTime1 new DateTime(shell, SWT.BORDER); dateTime1.setBounds(359, 10, 101, 24);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//今天是2023-11-26,getMonth()获得的月份从0开始,上面输出2023-10-26,控件显示2020/11/26dateTime1.setYear(2022); dateTime1.setMonth(11); dateTime1.setDay(31);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//所以上面输出2022-11-31,控件显示2022/12/31dateTime1.setYear(2022); dateTime1.setMonth(10); dateTime1.setDay(30);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//先设置11月上一次的31号保持不变。因为11月没有31号,所以月份设置失败保持12月不变再设置日期30号12月有30号,上面输出2022-11-30,控件显示2020/12/30dateTime1.setYear(2021); dateTime1.setMonth(8); dateTime1.setDay(31);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//实际上设置9月31号,Day非法,不改变,只改变年和月.上行输出2021-8-30,控件显示2020/9/30dateTime1.setYear(2020); dateTime1.setMonth(1); dateTime1.setDay(28);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//这里本想设置2月28号,先设置2月,但由于之前是30号,2月没有30号所以month不变day变输出2020-8-28,控件显示2020/9/28
//但如果先设置Day为28号再设置setMonth(1)则可以成功,下面输出2020-1-28控件显示2020-2-28
//dateTime1.setYear(2020); dateTime1.setDay(28); dateTime1.setMonth(1);
//System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());dateTime1.setYear(2019); dateTime1.setMonth(0); dateTime1.setDay(31);
System.out.println(dateTime1.getYear()-dateTime1.getMonth()-dateTime1.getDay());
//这里设置1月31号,输出2019-0-31,控件显示2019/1/31DateTime dateTime2 new DateTime(shell, SWT.BORDER);
dateTime2.setBounds(359, 40, 98, 24);
dateTime2.setDate(2022, 11, 30);
System.out.println(dateTime2.getYear()-dateTime2.getMonth()-dateTime2.getDay());
//上面输出2022-11-30,控件显示2022/12/30dateTime2.setDate(2022, 1, 28);
System.out.println(dateTime2.getYear()-dateTime2.getMonth()-dateTime2.getDay());
//上面输出2022-1-28,控件显示2022/2/28,直接用setDate方法则无顺序之分
可以看出当对月和日分别设置时就像在拨动一个拨轮日历同一时间只能调整月份或日期且一个不能带动另一个。如果从12月31号调整到11月30号如果先调整月份日期保持31号由于11月没有31号则月份调整失败保持12月不变再将日期调整为30号且月份不再调整。结果为12月30号而非11月30号。要想正确调整需先将日期从31号调整为30号再将月份从12月调整为11月。
而采用setDate()方法则一次性调整完成。
Composition类的扩展
Composition对象作为容器用于容纳其他控件可以将其扩展以做一些初始化操作
public class OperationComposite extends Composite {Statement statement;String userState;public OperationComposite(Composite parent, int style) {super(parent, style); }public OperationComposite(Shell shell, Composite parent, int style,String newAccount,String newRole,String tableName,String[] btnStrArray,String[] tableHeadersStrArray,ResultSet resultSet,Statement newStatement) {super(parent, style);statementnewStatement;Composite menuCompositenew Composite(parent, SWT.None); menuComposite.setBounds(10, 10, 722, 35);Group groupnew Group(parent, SWT.CENTER);//group.setText(tableName);group.setBounds(10, 51, 722, 243);}Overrideprotected void checkSubclass() {// Disable the check that prevents subclassing of SWT components}
}
Table类的扩展
Table对象用于将数据以表格形式展示可以将其扩展以完成一些初始化操作
public class ExtendedTable extends Table{String[] headerStrArray;String[] contextStrArray {};String contextStr;static Shell shell;protected void checkSubclass() { // TODO Auto-generated method stub }public ExtendedTable(Composite composite,int i) {super(composite, i);// TODO Auto-generated constructor stub}public ExtendedTable(Shell shell,Group group, int style,String newAccount,String newRole,String tableName,String[] headerStrArray,Statement statement,ResultSet resultSet) {// TODO Auto-generated constructor stubsuper(group, style);//super(shell, style); //Table tablenew Table(group, 0);setHeaderVisible(true); setLinesVisible(true);}
}