解决SWT Button设置背景颜色不可用的问题
2010-07-06 16:46
495 查看
在做Eclipse 的一个项目时遇到org.eclipse.swt.widgets.Button设置背景色,不起作用。
遇到问题后在网上找了好久,终于找到一段修正代码,不可直接使用,于是将其更改为如下方法。
本想重写swt的Button类,结果任务在身,遇到问题就放弃了,有能力的可以试试。
或许该方法还可以继续优化,大家继续啊。
使用方法:正常使用SWT Button,只是在设置背景设以后,记得调用下面的方法,即可,测试通过。
/***
* Just to fix button's backgroundColor,may be it is on test.
*
* @param button
*/
private void fixSetBackground(Button button) {
Color foreground = button.getForeground();
Color background = button.getBackground();
int x = 0;
int y = 0;
Rectangle rect = button.getBounds();
int width = rect.width;
int height = rect.height;
String text = button.getText();
if (width == 0)
width = 1;
if (height == 0)
height = 1;
button.setImage(new Image(button.getParent().getDisplay(), width,height));
Image original = button.getImage();
GC gc = new GC(original);
gc.setForeground(foreground);
gc.setBackground(background);
gc.drawRectangle(x, y, width, height);
gc.fillRectangle(x, y, width, height);
Font font = button.getFont();
FontData fontData = font.getFontData()[0];
int fontSize = fontData.getHeight();
gc.setFont(button.getFont());
int ximg = (x + width) / 2 - fontSize * text.length() / 3;
int yimg = (y + height) / 2 - fontSize * 3 / 4;
gc.drawText(text, ximg < 4 ? ximg : 4, yimg < 4 ? yimg : 4,
SWT.DRAW_TRANSPARENT | SWT.DRAW_MNEMONIC);
gc.dispose();
}
原文:https://bugs.eclipse.org/bugs/show_bug.cgi?id=23837
相关文章推荐
- 解决SWT Button设置背景颜色不可用的问题
- 解决设置了body宽高,但设置背景颜色仍然会铺满问题
- 解决设置了body宽高,但设置背景颜色仍然会铺满问题
- 解决listview设置背景图片以后,拖动出现黑色的问题。
- css解决select不能被遮盖和不能设置边框背景补白等样式的问题
- 通过设置子窗体透明度,解决C# winform 子窗体设置背景图卡的问题
- UINavigation设置背景颜色后 界面适配的问题
- jQuery中aninamte不能改变背景/颜色的解决问题
- Android自定义布局的背景在多分辨率的情况下设置fill_parent时背景不能够横向全屏的问题解决
- qss样式:按钮在designer中设置背景颜色的问题
- 解决 设置导航栏背景色 和影响状态栏的颜色 问题
- iOS之解决UITableView与背景颜色不一致问题
- div+css div不设置高度背景颜色或外边框不能显示的解决方法
- 解决web标准中IE无法设置滚动条颜色的问题
- VS2010解决对话框设置背景图片和文字重叠问题
- ipad 设置视图的背景颜色问题
- 想把图片当背景,但是其他控件背景设为透明色后,显示的是背景颜色的问题解决
- android在根布局组件设置背景不显示的问题解决
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- 转【备忘】android 文本颜色和背景的设置问题