2010-10-01 131 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