how to define action listener for buttons in java

后端 未结 4 589
悲哀的现实
悲哀的现实 2021-01-26 08:31

I have a jframe that includes JButton.I have six buttons in this frame, but I don\'t know how to define action listener for this buttons.please help to solve this probl

4条回答
  •  星月不相逢
    2021-01-26 08:49

    buttons have a method called addActionListener, use that for adding the action listener that you can implement for the click...

    Example:

    dummyButton = new JButton("Click Me!"); // construct a JButton
    add(dummyButton); // add the button to the JFrame
    dummyButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(" TODO Auto-generated method stub");
        }
    });
    

提交回复
热议问题