简体   繁体   English

处理Java swing中的事件

[英]handling events in java swing

How can I assign two buttons to share the same class for handling events in Java/swing? 如何分配两个按钮以共享同一类来处理Java / Swing中的事件?

For example, I have this: 例如,我有这个:

private class BtnEvtHandler implements ActionListener {

        private int counter=10;

        public void actionPerformed(ActionEvent e) {
            gs.setX(counter);
            gs.repaint();
            counter=counter+10;
        }

        public void actionPerformed(ActionEvent e) {

                //action for move button
        }

    }

        JButton jumpBtn= new JButton("JUMP");
        BtnEvtHandler okButtonHandler= new BtnEvtHandler(); 
        (jumpBtn).addActionListener(okButtonHandler);
        menuPanel.add(jumpBtn);

Now I want to add another button as below which can have the same class as event handler but dispatches to different actionPerformed as mentioned in above code. 现在,我想添加另一个按钮,如下所示,该按钮可以与事件处理程序具有相同的类,但是可以分派到上述代码中提到的不同的actionPerformed上。

        JButton moveBtn= new JButton("MOVE");
        menuPanel.add(moveBtn);
        (moveBtn).addActionListener(okButtonHandler);

You can't reuse one ActionListener and expect it to call a different method depending on the button you attach it to. 您不能重用一个ActionListener并期望它根据附加到它的按钮来调用其他方法。 The contract of ActionListener has one method that gets called. ActionListener的约定有一种被调用的方法。 But you can check the source of the event and have flow control based on that. 但是您可以检查事件的来源并基于此进行流控制。 Here's an example: 这是一个例子:

package com.sandbox;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();

        JPanel pane = new JPanel();

        MyActionListener myActionListener = new MyActionListener();

        JButton button1 = new JButton("Button1");
        button1.addActionListener(myActionListener);
        pane.add(button1);
        JButton button2 = new JButton("Button2");
        button2.addActionListener(myActionListener);
        pane.add(button2);


        frame.add(pane);
    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


    private static class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
            if ("Button1".equals(source.getText())) {
                System.out.println("You clicked button 1");
            } else {
                System.out.println("You clicked button 2");
            }
        }
    }

}

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

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