代码之家  ›  专栏  ›  技术社区  ›  kostja

是否可以进行垂直SWT控制?

  •  0
  • kostja  · 技术社区  · 15 年前

    我想在应用程序中使用一些控件- Button S和 Label S-垂直方向-。但是我找不到这样做的可能性。即使是非公开的setOrientation方法也只处理从左到右的方向。 如果不实现自定义 纽扣 或衍生自 Canvas ?

    3 回复  |  直到 10 年前
        1
  •  2
  •   YoK    15 年前

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

        2
  •  3
  •   Andreas Dolk    15 年前

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

        3
  •  2
  •   App Work    10 年前

    是的,可能在 SWT 使用自定义小部件。 你需要自己做 Button / Label 。 在 PaintListener ,获取 Transform 对象并将文本旋转到所需角度。 在每次单击的示例中,将角度更改为此序列0,90180270。 这个 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