Java:如何检查按下了哪个按钮?

时间:2017-04-19 20:29:56

标签: java jframe jbutton

让我说我有这样的事情:

public class MyFrame implements ActionListener{
    private String title;

    public MyFrame(String title){
        this.title = title;
    }
    public void createFrame(){
        JFrame options = new JFrame("Options");

        options.getContentPane().setLayout(new GridLayout(6,1));

        options.add(createButton("Add"));
        options.add(createButton("Delete"));
        options.add(createButton("Search"));

        options.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        options.setVisible(true);
        options.pack();
    }

    private JButton createButton(String buttonName){
        JButton jb = new JButton(buttonName);
        jb.addActionListener(this);
        return jb;
    }

    public void actionPerformed(ActionEvent e) {

        //what to write here?
    }
}

我的问题是,如何查看按下了哪个按钮?我在想这样的事情,但我真的不知道如何实现我的想法:

int result = //return a value after button pressed
if(result == something){
    //do something
}

我知道这个问题已被多次询问,但我没有找到适合我案例的答案。任何帮助将不胜感激!

0 个答案:

没有答案