问题描述
我正在做Eclipse插件开发。我正在使用 MessageDialog
类。可以在。
I am doing Eclipse plugin development. I am using the class MessageDialog
. The API can be found here.
我想添加一个链接,就像我在消息中对此处所做的一样 MessageDialog
。
I want to add a link like I did about with "here" in the message of the MessageDialog
.
这是我在做什么:
String errorMessage = "You have received an error. Please visit " + URL_NEEDED_HERE
MessageDialog.openError(getShell(), "Get Existing Data Sources Error", errorMessage);
URL一直显示为字符串。
The URL keeps showing up as just a String. Is it possible for it to show as a link?
推荐答案
正如@ greg-449所说, MessageDialog
不支持链接。如果您不介意骇人听闻的方法,则可以节省一些工作并覆盖 createMessageArea
,例如:
As @greg-449 said, the MessageDialog
does not support links. If you don't mind the hackish approach, you can save some work and override createMessageArea
like so:
@Override
protected Control createMessageArea( Composite composite ) {
Image image = getImage();
if( image != null ) {
imageLabel = new Label( composite, SWT.NULL );
image.setBackground( imageLabel.getBackground() );
imageLabel.setImage( image );
GridDataFactory.fillDefaults().align( SWT.CENTER, SWT.BEGINNING ).applyTo( imageLabel );
}
if( message != null ) {
Link link = new Link( composite, getMessageLabelStyle() );
link.setText( "This is a longer nonsense message to show that the link widget wraps text if specified so. Please visit <a>this link</a>." );
GridDataFactory.fillDefaults()
.align( SWT.FILL, SWT.BEGINNING )
.grab( true, false )
.hint( convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ), SWT.DEFAULT )
.applyTo( link );
}
return composite;
}
};
代码从 IconAndMessageDialog
复制并只需用 Link
小部件替换 Label
。
The code is copied form the IconAndMessageDialog
and just replaces the Label
with a Link
widget.
另外,您也可以这样覆盖 createCustomArea
:
Alternatively you can override createCustomArea
like so:
@Override
protected Control createCustomArea( Composite parent ) {
Link link = new Link( parent, SWT.WRAP );
link.setText( "Please visit <a>this link</a>." );
return link;
}
这是将自定义控件添加到 MessageDialg ,但导致布局略有不同:
This is the designated way to add custom controls to a MessageDialg
but leads to a slightly different layout:
这篇关于将链接添加到MessageDialog消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!