黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

swt/jface 自定義 Dialog

系統(tǒng) 2171 0

自定義Dialog很簡(jiǎn)單,下邊我們來(lái)一步步實(shí)現(xiàn)自定義Dialog
一、寫(xiě)一個(gè)類,繼承自Dialog

import ?org.eclipse.jface.dialogs.Dialog;
import ?org.eclipse.swt.widgets.Shell;

public ? class ?TestDialog? extends ?Dialog? {

????
public ?TestDialog(Shell?parentShell)? {
????????
super (parentShell);
????}

}

好了,寫(xiě)好了,如何運(yùn)行呢?
再寫(xiě)一個(gè)類:

import ?org.eclipse.swt.widgets.Display;
import ?org.eclipse.swt.widgets.Shell;

public ? class ?Test? {
????
public ? static ? void ?main(String[]?args)? {
????????Shell?shell?
= ? new ?Shell();
????????TestDialog?td?
= ? new ?TestDialog(shell);
????????td.setBlockOnOpen(
true );
????????td.open();
????????Display.getCurrent().dispose();
????}

}

好了運(yùn)行一下看到效果了吧,帶有兩個(gè)button.
二、看到上邊的代碼是否會(huì)想到別的呢?為什么要再寫(xiě)一個(gè)類來(lái)運(yùn)行Dialog,不能在內(nèi)部寫(xiě)個(gè)main方法嗎?
我們來(lái)試一下:
方法一:參考Jface hello world的的寫(xiě)法:

import ?org.eclipse.jface.dialogs.Dialog;
import ?org.eclipse.swt.widgets.Display;
import ?org.eclipse.swt.widgets.Shell;

public ? class ?TestDialog? extends ?Dialog? {

????
public ?TestDialog(Shell?parentShell)? {
????????
super (parentShell);
????}

????
public ? static ? void ?main(String[]?args)? {
????????TestDialog?td?
= ? new ?TestDialog( new ?Shell());
????????td.setBlockOnOpen(
true );
????????td.open();
????????Display.getCurrent().dispose();
????}

}


運(yùn)行一下看看什么效果,提示找不到main方法。且打印出如下Exception
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
?at org.eclipse.jface.resource.JFaceResources.getResources(JFaceResources.java:184)
?at org.eclipse.jface.resource.JFaceResources.getImageRegistry(JFaceResources.java:310)
?at org.eclipse.jface.dialogs.Dialog.<clinit>(Dialog.java:211)
Exception in thread "main"
為什么呢?我們是有main方法的,跟進(jìn)Exception去看看吧。
發(fā)現(xiàn)問(wèn)題出現(xiàn)在Dialog類的這段代碼上:

static ? {
????????ImageRegistry?reg?
= ?JFaceResources.getImageRegistry();
????????reg.put(DLG_IMG_MESSAGE_INFO,?ImageDescriptor.createFromFile(
????????????????Dialog.
class ,? " images/message_info.gif " ));? // $NON-NLS-1$
????????reg.put(DLG_IMG_MESSAGE_WARNING,?ImageDescriptor.createFromFile(
????????????????Dialog.
class ,? " images/message_warning.gif " ));? // $NON-NLS-1$
????????reg.put(DLG_IMG_MESSAGE_ERROR,?ImageDescriptor.createFromFile(
????????????????Dialog.
class ,? " images/message_error.gif " ));? // $NON-NLS-1$
????}

原來(lái)在靜態(tài)代碼塊上出現(xiàn)了Exception,造成在運(yùn)行main函數(shù)之前就退出了。所以才說(shuō)沒(méi)有main函數(shù)。
我們知道classload在加載一個(gè)類的時(shí)候,對(duì)于靜態(tài)代碼塊會(huì)逐行執(zhí)行,按照出現(xiàn)的先后順序。同時(shí)父類的靜態(tài)代碼塊一定比子類的先執(zhí)行。因?yàn)?
在load子類之前會(huì)先load父類。這就是為什么hello world中不會(huì)出現(xiàn)問(wèn)題,這里會(huì)出現(xiàn)問(wèn)題的原因。因?yàn)镈ialog比ApplicationWindow多了這段靜態(tài)代碼。

繼續(xù)追下去為什么這段代碼會(huì)出現(xiàn)空指針異常呢,原來(lái)這段代碼依賴于new Shell()必須先運(yùn)行。而我們的new Shell()寫(xiě)在main方法里邊,肯定是在加載類完成后才能運(yùn)行的。所以在類內(nèi)部直接寫(xiě)個(gè)main方法是不行的。只能單獨(dú)寫(xiě)個(gè)類來(lái)調(diào)用。
方法二:
單獨(dú)寫(xiě)個(gè)類如下:

import ?org.eclipse.swt.widgets.Display;
import ?org.eclipse.swt.widgets.Shell;

public ? class ?Test? {
????
public ? static ? void ?main(String[]?args)? {
????????TestDialog?td?
= ? new ?TestDialog( new ?Shell());
????????td.setBlockOnOpen(
true );
????????td.open();
????????Display.getCurrent().dispose();
????}

}

依然是不行的,報(bào)同樣的錯(cuò)誤,為什么?仔細(xì)看一下,我們把new Shell()寫(xiě)在構(gòu)造函數(shù)的參數(shù)里,其實(shí)范了和剛才同樣的錯(cuò)誤。所以單獨(dú)提出new Shell(),寫(xiě)在構(gòu)造函數(shù)之前。就得到了文章開(kāi)始的Test類。平時(shí)我們使用的時(shí)候?yàn)槭裁床怀鲞@個(gè)問(wèn)題呢?因?yàn)槲覀兤綍r(shí)使用的時(shí)候Dialog從里不是單獨(dú)存在的,在之前shell早被構(gòu)造過(guò)了。反而是demo更容易出這個(gè)問(wèn)題。
好了,上邊只是個(gè)小插曲,繼續(xù)我們的自定義Dialog.
三、去掉兩個(gè)按鈕
雖然Dialog天生帶的兩個(gè)按鈕不錯(cuò),但我們有的時(shí)候并不想要這兩個(gè)按鈕,怎么辦,如何去掉它?
簡(jiǎn)單,只要我們覆蓋父類的createButtonsForButtonBar這個(gè)方法就可以了,覆寫(xiě)這個(gè)方法,里邊什么也不寫(xiě)

protected ? void ?createButtonsForButtonBar(Composite?parent)? {
????}


看一下按鈕消失了。
四、加入右上角的最大化和關(guān)閉
覆寫(xiě)父類的這個(gè)方法:

protected ? int ?getShellStyle() {
????????
return ? super .getShellStyle() | SWT.RESIZE | SWT.MAX;
????}


五、改變Dialog的大小
覆寫(xiě)這個(gè)方法:

protected ?Point?getInitialSize() {
????????
return ? new ?Point( 300 , 400 ); // 300是寬400是高
????}

六、加入自己的控件
覆寫(xiě)createDialogArea方法

protected ?Control?createDialogArea(Composite?parent)? {
????????Composite?container?
= ?(Composite)? super .createDialogArea(parent);
????????container.setLayout(
new ?RowLayout());
????????text?
= ? new ?Text(container,?SWT.BORDER);
????????text.setLayoutData(
new ?RowData( 100 , - 1 ));
????????
return ?container;
????}


這里使用了RowLayout
七、加入自己的按鈕
覆寫(xiě)initializeBounds

protected ? void ?initializeBounds() {
????????Composite?comp?
= ?(Composite)getButtonBar();
????????
super .createButton(comp,?IDialogConstants.OK_ID,? " 完成 " ,? true );
????}

好了這里自定義Dialog完成了,然后根據(jù)你的需要再Dialog中添加更負(fù)載的控件,更多的按鈕。最后目的當(dāng)然是從Dialog取道數(shù)值。
八、帶提示框的Dialog
使用方法和前邊相同,不同的是不是繼承自Dialog而是繼承自TitleAreaDialog,然后在createDialogArea中加入兩行

setTitle( " 標(biāo)題 " );
setMessage(
" 提示信息 " )
// setMessage可以加上圖片,加入的辦法是setMessage("提示信息",IMessageProvider.WARNING);如果想加入其他的圖片,調(diào)用相應(yīng)的常量。

swt/jface 自定義 Dialog


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論