问题
I would like to have some of the controls in an app - Button
s and Label
s - vertically oriented - . However i could not find a possibilty to do this. Even the non-public setOrientation method tackles only the left-to-right orientation.
Is it possible without implementing a custom Button
or deriving from Canvas
?
回答1:
As far as I know vertical orientation for Button and Label is not possible. You will need to provide custom implementation for same. Check this link http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg30094.html
回答2:
SWT uses the standard widgets provided by the host operating system. So if the operating system does not provide support for vertically oriented controls, SWT can't provide it too.
回答3:
Yes its possible in SWT
using custom widget.
You need to make your own Button
/ Label
.
In the PaintListener
, get the Transform
object and rotate the text to your desired angle.
In the example for every click , change the angle to this sequence 0,90,180,270.
The button
(here actually Canvas
) aspect ratio is changed by setting the bounds
.
Feel free to play with the paint
method;
public class RotatingButton extends Canvas
{
private int mouse = 0;
private boolean hit = false;
private String text = "Button";
float rotatingAngle = 0f;
float[] angles = { 0, 90, 180, 270 };
int index = 0;
public RotatingButton(Composite parent, int style)
{
super(parent, style);
this.addListener(SWT.MouseUp, new Listener()
{
@Override
public void handleEvent(Event e)
{
index++;
index = index > 3 ? 0 : index;
Rectangle r = getBounds();
setBounds(r.x, r.y, r.height, r.width);
rotatingAngle = angles[index];
redraw();
}
});
this.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
paint(e);
}
});
this.addMouseMoveListener(new MouseMoveListener()
{
public void mouseMove(MouseEvent e)
{
if (!hit)
return;
mouse = 2;
if (e.x < 0 || e.y < 0 || e.x > getBounds().width
|| e.y > getBounds().height)
{
mouse = 0;
}
redraw();
}
});
this.addMouseTrackListener(new MouseTrackAdapter()
{
public void mouseEnter(MouseEvent e)
{
mouse = 1;
redraw();
}
public void mouseExit(MouseEvent e)
{
mouse = 0;
redraw();
}
});
this.addMouseListener(new MouseAdapter()
{
public void mouseDown(MouseEvent e)
{
hit = true;
mouse = 2;
redraw();
}
public void mouseUp(MouseEvent e)
{
hit = false;
mouse = 1;
if (e.x < 0 || e.y < 0 || e.x > getBounds().width
|| e.y > getBounds().height)
{
mouse = 0;
}
redraw();
if (mouse == 1)
notifyListeners(SWT.Selection, new Event());
}
});
this.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.keyCode == '\r' || e.character == ' ')
{
Event event = new Event();
notifyListeners(SWT.Selection, event);
}
}
});
}
public void setText(String string)
{
this.text = string;
redraw();
}
public void paint(PaintEvent e)
{
Transform tr = null;
tr = new Transform(e.display);
Rectangle r =getBounds();
text=e.gc.stringExtent(text)+"";
e.gc.setAntialias(SWT.ON);
Point p=e.gc.stringExtent(text);
int w = e.width;
int h = e.height;
tr.translate(w / 2, h / 2);
tr.rotate(rotatingAngle);
e.gc.setTransform(tr);
e.gc.drawString(text, r.x-(p.x/3)*2,r.y-p.y);
}
}
来源:https://stackoverflow.com/questions/3837602/is-it-possible-to-have-vertical-swt-controls