如何正确格式化ActionEvent以便JButtons可以工作

时间:2019-04-01 14:18:50

标签: java jbutton actionevent

我已经设置了一些ActionListeners,但是只有“ Takeoff”有效。单击其他按钮后不起作用。单击它们后,什么也不会发生。

我试图创建一个新的ButtonHandler,但这没用。

var indexPoint = 0;
$('#MarkPoint').on('click', function(){
    map.removeLayer(vector);
    removeInteractions();
    source = new ol.source.Vector();
    indexPoint = 0;

    vector = new ol.layer.Vector({
        source: source,
        style: function(){ 
            return new ol.style.Style({
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 0.2)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#ffcc33',
                    width: 2
                }),
                image: new ol.style.Circle({
                    radius: 7,
                    fill: new ol.style.Fill({
                        color: '#ffcc33'
                    })
                }),               
                text:  textPoint().text              
            })              
        }
    });

    draw = new ol.interaction.Draw({
        source: source,
        type: 'Point'
    });
    map.addInteraction(draw);
    map.addLayer(vector);     

})

var textPoint = function() {
    indexPoint += 1
    var zoom = map.getView().getZoom();
    //var resolution = map.getView().getResolution();
    var font = ( zoom + 1 )
    return {
        text: new ol.style.Text({
            font: '12 px Arial',
            fill: new ol.style.Fill({
                color: '#ffd300'
            }),
            stroke: new ol.style.Stroke({
                color: '#000',
                width: 3
            }),
            offsetX: 20,
            offsetY: -12,
            overflow: true,
            text: 'Ponto ' + indexPoint.toString()

        })   
    }
}

我想做的是沿正确的方向移动机器人无人机,而不是让它静止不动。

我不确定这部分是否有帮助,但是我有ButtonHandler实现ActionListener的地方。

ButtonListener l = new ButtonListener();

JButton takeoff = new JButton("Takeoff");
takeoff.addActionListener(new ButtonHandler());
takeoff.addActionListener();
grid[0][2].add(takeoff);

JButton land = new JButton("Land");
land.addActionListener(new ButtonHandler());
grid[1][2].add(land);

JButton forward = new JButton("Forward");
forward.addMouseListener(new MouseHandler(l));
forward.addActionListener();
grid[2][1].add(forward);

JButton left = new JButton("Left");
left.addMouseListener(new MouseHandler());
left.addActionListener(new ButtonHandler());
left.addActionListener();
grid[3][0].add(left);


takeoff.addActionListener(l);
land.addActionListener(l);
forward.addActionListener(l);
backward.addActionListener();
left.addActionListener(l);
right.addActionListener(l);
turnLeft.addActionListener(l);
turnRight.addActionListener(l);
up.addActionListener(l);
down.addActionListener(l);
stop.addActionListener(l);

2 个答案:

答案 0 :(得分:1)

您可以使用actionCommand通过反射来调用方法,例如

private void init() {
    JButton left = new JButton("Go left");
    // This
    left.setActionCommand("left");
    left.addActionListener(new MethodCallActionHandler());
    // OR that
    left.addActionListener(new MethodCallActionHandler("left"));
}

private void left() {
    // go left...
}

private class MethodCallActionHandler implements ActionListener {

    private String methodName;

    private MethodCallActionHandler() {
        super();
    } 

    private MethodCallActionHandler(String methodName) {
        this.methodName = methodName;
    } 

    public void actionPerformed(ActionEvent e)
    {
        String button = methodName != null ? methodName : e.getActionCommand();
        SurroundingClass.this.getClass().getMethod(button, new Class[] {}).invoke(SurroundingClass.this);
    }
}

您还可以将操作命令作为String传递给MethodCallActionHandler。

答案 1 :(得分:0)

您可以将Action Listener类继承到当前类中,然后添加所需的方法。然后,您可以执行takeoff.add(this)...等。

您也无处设置操作命令,该操作命令是在按钮设置中完成的。

设置时

String button = e.getActionCommand();

那不是你要做的事情

JButton button = new JButton("Takeoff"); <-- This is the text associated with the button


button.setActionCommand("Takeoff");

然后它应该工作。