简体   繁体   English

如何扩展Java的SWT标签?

[英]How To Extend Java's SWT Label?

I wanted to extend the java SWT Label object, and found out it's not allowed. 我想扩展java SWT Label对象,但发现它是不允许的。
So I created a class 'MyLabel' that contains a Label object. 因此,我创建了一个包含Label对象的类'MyLabel'。
It looks something like this: 看起来像这样:

public class MyLabel {

    Label label;

    //more instance variables here...

    public MyLabel(Composite parent, int type) {
        label=new Label(parent,type);
        //more initialization here...
    }

    public void setText(String label_text) {
        label.setText(label_text);
    }

    public void setImage(Image label_image) {
        label.setImage(label_image);
    }

    public void addMouseListener(MouseListener board_listener) {
        label.addMouseListener(board_listener);
    }

    public void removeMouseListener(MouseListener board_listener) {
        label.removeMouseListener(board_listener);
    }

    //more staff here...
}

and it worked fine for a while, until I started listening to events, and realized this: 并在一段时间内运行良好,直到我开始收听事件并意识到这一点:

public class SWTTest {

    public static void main(String[] args) {

        Display display=new Display();
        Shell shell=new Shell(display);
        shell.setLayout(new RowLayout());


        MyLabel my_label=new MyLabel(shell, SWT.SHADOW_OUT);
        my_label.setText("this is my label");

        my_label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseUp(MouseEvent e) {
                e.getSource(); //returns the Label object instead of MyLabel object...
            }

        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) 
                display.sleep();
        }
    }
}

this may seems not too bad, but I have several MyLabel objects, and I need to know the source of the mouse click in terms of MyLabel. 这看起来似乎还不错,但是我有几个MyLabel对象,我需要根据MyLabel来了解鼠标单击的来源。
Is there some solution to this situation? 有什么解决办法吗?

**EDIT **编辑

I'm trying to create my own label. 我正在尝试创建自己的标签。 more specifically, I want a Label with an addition field X in it, and a getX() and setX(X x_var) in it. 更具体地说,我想要一个在其中带有加法字段X的Label,并在其中带有getX()和setX(X x_var)。

**EDIT2 **编辑2

OK, I'll be more specific: 好的,我会更具体:
I have a chess board with 64 labels on it, ordered in a 8x8 grid, each represents a position on the board. 我有一个国际象棋棋盘,上面有64个标签,按8x8网格排列,每个代表一个位置。
Now, what I'm trying to achieve is that whenever one of the 64 positions on the board was clicked - I'll know where it was clicked, in terms of x and y board-coordinates . 现在,我想要实现的是,每当主板上的64个位置中的一个被点击了-我就知道它被点击, 在x和y板坐标的条款
So, I want to extend Label, and add Position instance variable to it. 因此,我想扩展Label,并向其中添加Position实例变量。 (Position is a class that has x and y fields in it). (位置是其中具有x和y字段的类)。
Now, whenever one of the labels were clicked, I can invoke the "getPoint()" method from within the "mouseUp(MouseEvent e)" method, and to know which position on the board was clicked. 现在,每当单击其中一个标签时,我都可以从“ mouseUp(MouseEvent e)”方法中调用“ getPoint()”方法,并知道单击板上的哪个位置。

Hope it is more clear now, and sorry for the ambiguity. 希望现在更加清楚,对此表示抱歉。

You can use the setData method of Label to save a reference to your class. 您可以使用LabelsetData方法保存对您的类的引用。

So when you create your label use: 因此,当您创建标签时,请使用:

label.setData(myLabel);

and in the mouse event you can use 在鼠标事件中,您可以使用

MyLabel myLabel = (MyLabel)((Widget)e.getSource()).getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM