Java SWT 文本框 垂直 居中
来源:互联网 发布:支持ubuntu的手机 编辑:程序博客网 时间:2024/05/22 01:52
项目需要一个垂直居中的text,翻了很多帖子,没发现有实现。
自己写一个。
具体用法和SWT Text的用法一样,当然没有把所有的text的功能加入。如果有发现还有常用的功能未曾实现,可以@我
代码如下:
package test;
/**
* NewText:支持垂直居中的Text;
* 说明:不支持换行;
* @author Ray
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
public class NewText extends Composite implements ControlListener, MouseTrackListener, MouseListener {
private Text text;
private int style;
private Color textBackground;
private Color background;
private int rightPadding = 0;
private int leftPadding = 0;
public NewText(Composite parent, int style) {
super(parent, style);
this.style = style;
this.setCursor(new Cursor(Display.getDefault(), SWT.CURSOR_IBEAM));
createText(style);
textBackground = text.getBackground();
background =new Color(getDisplay(), 255, 255, 255);
super.setBackground(background);
addControlListener(this);
addMouseTrackListener(this);
addMouseListener(this);
textAddListener();
}
private void createText(int style) {
if (hasBorder())
text = new Text(this, setBorderBitTo0(style));
else
text = new Text(this, style);
}
private void textAddListener() {
text.addMouseTrackListener(this);
text.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
setLostBackground();
}
@Override
public void focusGained(FocusEvent e) {
setGainedBackground();
}
});
}
/**
* 去掉style中的SWT.BORDER
*
* @return
*/
private int setBorderBitTo0(int style) {
return style & ~SWT.BORDER;
}
/**
* 判断传入的style是否包含边框
*
* @return
*/
private boolean hasBorder() {
return (style & SWT.BORDER) != 0;
}
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
Point pt = text.computeSize(wHint, hHint);
return new Point(pt.x + 4, pt.y + 4);
}
@Override
public void controlMoved(ControlEvent e) {
}
@Override
public void controlResized(ControlEvent e) {
setTextBounds();
}
private int getY() {
return (getSize().y - text.getSize().y) / 2;
}
private void setTextBounds() {
text.pack();
if (hasBorder()) {
text.setLocation(2 + leftPadding, getY() - 2);
text.setSize(getBounds().width - 8 + (rightPadding + leftPadding), text.getSize().y);
} else {
text.setLocation(0 + leftPadding, getY());
text.setSize(getBounds().width - 2 + (rightPadding + leftPadding), text.getSize().y);
}
}
@Override
public void pack() {
text.pack();
if (hasBorder()) {
setSize(text.getSize().x + 4, text.getSize().y + 8);
} else {
setSize(text.getSize().x, text.getSize().y);
}
}
public void setText(String string) {
text.setText(string);
}
public void setFont(Font font) {
text.setFont(font);
setTextBounds();
}
public String getText() {
return text.getText();
}
public void selectAll() {
text.selectAll();
}
public boolean forceFocus() {
return text.forceFocus();
}
public boolean setFocus() {
return text.setFocus();
}
@Override
public void setBackground(Color color) {
textBackground = color;
background = color;
super.setBackground(color);
text.setBackground(color);
}
@Override
public void setForeground(Color color) {
super.setForeground(color);
text.setForeground(color);
}
@Override
public void addFocusListener(FocusListener listener) {
text.addFocusListener(listener);
}
public void addSelectionListener(SelectionListener listener) {
text.addSelectionListener(listener);
}
public void addModifyListener(ModifyListener listener) {
text.addModifyListener(listener);
}
public void addVerifyListener(VerifyListener listener) {
text.addVerifyListener(listener);
}
private void setGainedBackground() {
super.setBackground(textBackground);
}
private void setLostBackground() {
super.setBackground(background);
}
@Override
public void mouseDown(MouseEvent e) {
relocate(e);
}
public void relocate(MouseEvent me) {
if (me.widget != this)
return;
final Point pt = getDisplay().map(this, null, me.x, getSize().y / 2);
Event event = new Event();
event.widget = text;
event.type = SWT.MouseMove;
event.x = pt.x;
event.y = pt.y;
getDisplay().post(event);
event.type = SWT.MouseDown;
event.button = 1;
getDisplay().post(event);
event.type = SWT.MouseUp;
getDisplay().post(event);
}
@Override
public void mouseUp(MouseEvent e) {
}
@Override
public void mouseEnter(MouseEvent e) {
}
@Override
public void mouseExit(MouseEvent e) {
}
@Override
public void mouseHover(MouseEvent e) {
}
@Override
public void mouseDoubleClick(MouseEvent e) {
}
public int getRightPadding() {
return rightPadding;
}
public void setRightPadding(int rightPadding) {
this.rightPadding = rightPadding;
}
public int getLeftPadding() {
return leftPadding;
}
public void setLeftPadding(int leftPadding) {
this.leftPadding = leftPadding;
}
}
0 0
- Java SWT 文本框 垂直 居中
- 垂直居中文本框
- input文本框文字垂直居中
- input文本框文字垂直居中
- SWT标签Label垂直居中的方法
- 文本框里的内容垂直居中
- HTML中的文本框中的文字垂直居中
- 设置图片和文本框垂直居中
- 文本框输入字符垂直居中的解决方法
- 【css】input文本框光标垂直不居中
- ie8下文本框内文字垂直居中
- Java SWT 窗口居中对齐
- input文本框等垂直居中的最简单办法!!
- input 文本框 文字垂直居中对齐 ie firefox
- 怎么让文本框中的文字垂直方向居中
- 图片和文本框在TD中垂直居中
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- input文本框、文字、按钮、图片 垂直居中对齐的解决办法
- 使用Linux top命令进行性能分析
- new(std::nothrow) 和 new
- 设计模式总结之Composite Pattern(组合模式)
- 自定义 Android 对话框 (AlertDialog) 的样式
- XML在Android-iOS上的应用
- Java SWT 文本框 垂直 居中
- 导入*.sql脚本
- REDIS学习(4)spring boot redisTemplate 对REDIS的简单封装,以及对引用包的说明,以及对序列化的详细说明
- Android 自动跳转到系统界面总结
- 函数指针!!
- 【网络】高级I/O多路复用之select、poll和epoll
- 微信公众号测试帐号
- Hats’Worlds(字典树)
- HDOJ 5795 A Simple Nim