2010-10-01 96 views
0

我想在应用程序中使用一些控件 - 垂直方向 - 。但是我找不到这样做的可能性。即使是非公开的setOrientation方法也只能解决从左到右的方向。 有没有可能实施自定义Button或从Canvas派生?是否可以垂直SWT控制?

+1

现在你可以做到这一点。看看http://stackoverflow.com/q/8091920/796559 – 2011-12-03 17:38:58

+0

@Tonny谢谢,这是一个合理的解决方法 – kostja 2011-12-04 17:51:24

回答

2

据我所知,按钮和标签的垂直方向是不可能的。 您将需要为其提供自定义实现。 查看此链接http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg30094.html

+0

它的一个耻辱,但生病有做它自己:(也许e4将改善 – kostja 2010-10-01 11:29:39

+0

@ kostja - e4仍然会使用SWT和原生控件,只要这些原生控件不提供垂直定向或某种旋转的控件或组件,除了完全基于java的控件(比如Swing)之外别无他法, – 2010-10-04 06:38:33

3

SWT使用主机操作系统提供的标准小部件。所以如果操作系统不支持垂直导向控件,SWT也不能提供它。

2

是的,它可能在SWT使用自定义小部件。您需要自己制作Button/Label。 在PaintListener中,获取Transform对象并将文本旋转至所需的角度。 在每次点击的示例中,将角度更改为此序列0,90,180,270。 button(这里实际上是Canvas)长宽比通过设置bounds进行更改。 随意玩paint方法;

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); 
} 
} 

Screen SHOT