Change just the font size in SWT

两盒软妹~` 提交于 2019-12-18 03:50:09

问题


I need to use a larger font for one of the labels.

label.setFont( new Font(display,"Arial", 14, SWT.BOLD ) );

but obviously Arial is not always the default font. I want to change just the size and keep everything else at default values.

Can I do something like

label.setFontSize( 14 );

to avoid setting the other parameters? Or can I at least find out the name of the font that is actually being used as default?


回答1:


I believe you could do something like

FontData[] fD = label.getFont().getFontData();
fD[0].setHeight(16);
label.setFont( new Font(display,fD[0]));

As long as no more than one font is returned, that should work.




回答2:


You can do the following:

FontData[] fontData = label.getFont().getFontData();
for(int i = 0; i < fontData.length; ++i)
    fontData[i].setHeight(14);

final Font newFont = new Font(display, fontData);
label.setFont(newFont);

// Since you created the font, you must dispose it
label.addDisposeListener(new DisposeListener() {
    public void widgetDisposed(DisposeEvent e) {
        newFont.dispose();
    }
});


来源:https://stackoverflow.com/questions/1449968/change-just-the-font-size-in-swt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!