自己美化的swt文本域组件,支持滚动条,鼠标滚轮,支持显示行号,支持拖拽大小。win10系统下效果图如下:
附代码:
测试类:
package ZFang.Widget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextXSline_Test extends Shell {
public static void main(String args[]) {
try {
Display display = Display.getDefault();
TextXSline_Test shell = new TextXSline_Test(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* @param display
*/
public TextXSline_Test(Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FillLayout(SWT.HORIZONTAL));
TextXSline composite = new TextXSline(this, SWT.NONE);
composite.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
createContents();
composite.getTextObject().setText("");
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("文本域组件测试 - 鹞之神乐");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
组件类:
package ZFang.Widget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
public class TextXSline extends Composite {
private Text text;
Composite Track,Slider;
/**
* Create the composite.
* @param parent
* @param style
*/
public TextXSline(Composite parent, int style) {
super(parent, style);
setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
Composite composite = new Composite(this, SWT.NONE);
composite.setLocation(1, 1);
System.out.println("0000000000000000000000000000000=="+getSize());
composite.setSize(getSize().x-2, getSize().y-2);
addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
Rectangle c = getBounds();
c.x=c.y=1;
c.width-=2;
c.height-=2;
composite.setBounds(c);
Point p = composite.getSize();
Rectangle pr = composite.getBounds();
pr.width = 25;
lineNo.setBounds(pr);
pr.width = composite.getBounds().width;
// p.x-=20;
pr.x+=25;
pr.width=pr.width-25;
text.setBounds(pr);
Rectangle rect = Track.getBounds();
rect.x=p.x-20;
rect.height=p.y;
Track.setBounds(rect);
}
});
Track = new Composite(composite, SWT.NONE);
Track.setBackground(SWTResourceManager.getColor(255, 228, 225));
Track.setBounds(414, 0, 20, 261);
Track.setVisible(false);
Slider = new Composite(Track, SWT.NONE);
Slider.addListener(SWT.MouseMove, listener);
Slider.addListener(SWT.MouseDown, listener);
Slider.setBackground(SWTResourceManager.getColor(255, 69, 0));
Slider.setBounds(0, 0, Track.getSize().x, 200);
text = new Text(composite, SWT.MULTI);
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == 97 && e.stateMask == SWT.CTRL) {
text.selectAll();
}
}
});
text.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
text.addListener(SWT.MouseWheel, listener2);
text.addListener(SWT.Modify, listener2);
text.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
System.out.println(text.getLineCount()*text.getLineHeight()>text.getSize().y?"�?要滚动条":"不需要滚动条");
try {
text.setText(text.getText());
} catch (Exception e1) {
}
// System.out.println("TEXT RESIZE");
// System.out.println("(显示大小)TEXT.SIZE�?"+text.getSize());
// System.out.println("(真实大小)总行数x行高�?"+text.getLineCount()*text.getLineHeight());
}
});
text.setBounds(0, 0, 434, 261);
lineNo = new Text(composite, SWT.READ_ONLY | SWT.MULTI);
lineNo.setEnabled(false);
lineNo.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
lineNo.setBackground(SWTResourceManager.getColor(255, 228, 225));
lineNo.setText("12");
lineNo.setBounds(0, 0, 73, 23);
lineNo.setFont(text.getFont());
// createContents();
}
Listener listener = new Listener() {
int startX, startY;
public void handleEvent(Event e) {
if (e.type == SWT.MouseDown && e.button == 1) {
startX = e.x;
startY = e.y;
}
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
Composite c = (Composite)e.widget;
Rectangle rect = c.getBounds();
// rect.x = rect.x+e.x-startX;//X轴运�?
rect.y = rect.y+e.y-startY;//Y轴运�?
if (rect.y<=0) {
rect.y=0;
}
if (rect.y>=c.getParent().getSize().y-rect.height) {
rect.y=c.getParent().getSize().y-rect.height;
}
c.setBounds(rect);
double b = (double)c.getBounds().y/(c.getParent().getSize().y-c.getSize().y);
int LineCount = text.getLineCount();
int LineHeight = text.getLineHeight();
int TopPixel = text.getTopPixel();
int x = (int) ((LineCount*LineHeight-text.getSize().y)*b)/LineHeight;
text.setTopIndex(x);
lineNo.setTopIndex(x);
System.out.println("缩放比列�?"+(double)c.getBounds().y/(c.getParent().getSize().y-c.getSize().y));
}
}
};
Listener listener2 = new Listener() {
public void handleEvent(Event e) {
if (e.type==SWT.MouseWheel) {
text.setTopIndex(text.getTopIndex()-e.count);
}
System.out.println(text.getLineCount()*text.getLineHeight()>text.getSize().y?"�?要滚动条============":"不需要滚动条");
Track.setVisible(text.getLineCount()*text.getLineHeight()>text.getSize().y?true:false);
System.out.println("TEXT MODIFY");
System.out.println("(显示大小)TEXT.SIZE�?"+text.getSize());
System.out.println("(真实大小)总行数x行高�?"+text.getLineCount()*text.getLineHeight());
System.out.println("比例:"+(double)text.getLineCount()*text.getLineHeight()/text.getSize().y);
double d = (double)text.getSize().y/(text.getLineCount()*text.getLineHeight());
System.out.println(d);
Rectangle rect = Slider.getBounds();
rect.height = (int) (Track.getSize().y*d);
d = (double)text.getTopPixel()/(text.getLineCount()*text.getLineHeight());
rect.y = (int) ((Track.getSize().y)*d);
// rect.y = (text.getTopPixel()/(text.getLineCount()*text.getLineHeight()))*(Track.getSize().y-rect.height);
Slider.setBounds(rect);
//行号
int LineCount = text.getLineCount();
System.out.println(LineCount);
String lines = "";
for (int i = 1; i < LineCount+1; i++) {
lines+=i+"\n";
}
lineNo.setText(lines);
lineNo.setTopIndex(text.getTopIndex());
}
};
private Text lineNo;
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public Text getTextObject() {
return text;
}
}