how to define action listener for buttons in java

后端 未结 4 543
悲哀的现实
悲哀的现实 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:38

    It's really simple.

    I suppose you have an instance of your button, right? Let's say that instance is called myButton.

    You can just add an action listener by calling addActionListener:

    myButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Do whatever you like here
        }
    });
    

    Protip: next time you don't know what method to call, just type the instance name and .. Then, your IDE will show you all the methods you can call, unless you are not using an IDE. If that is the case, download one.

    0 讨论(0)
  • 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");
        }
    });
    
    0 讨论(0)
  • 2021-01-26 09:01

    First you have to import the package java.awt.event.* to enable events. After the class name you have to add implements ActionListener so that the class can handle events. When you have created the buttons you have to add an actionlistener to each button. Since you haven't showed which code you use I make an example with a simple program that counts votes, if the user clicks the yesButton the votes are increased with 1 and if the user clicks the noButton the votes are decreased with 1.

    Here is the code to add an ActionListener to each button:

    yesButton.addActionListener(this);
    noButton.addActionListener(this);
    

    Then write the following code to handle the events:

    public void actionPerformed(ActionEvent e) {
      JButton src = (JButton) e.getSource();
      if(src.getActionCommand().equals("Yes")) {
        yesCount++;
      } else {
        noCount++;
      }
      label.setText("Difference: " + (yesCount - noCount));
    }
    

    If you have 6 buttons you need to have an if statement and then 5 "else if" statements instead of only an if and an else statement.

    0 讨论(0)
  • 2021-01-26 09:02

    Have a look at the Java tutorials on how to use ActionListeners:

    https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

    Here's a simple example:

        import java.awt.BorderLayout;
        import java.awt.Dimension;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
    
        import javax.swing.JButton;
        import javax.swing.JComponent;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
    
        public class Hello extends JPanel implements ActionListener {
    
            JButton button;
    
            public Hello() {
    
                super(new BorderLayout());
                button = new JButton("Say Hello");
                button.setPreferredSize(new Dimension(180, 80));
                add(button, BorderLayout.CENTER);
    
                button.addActionListener(this);  //  This is how you add the listener
            }
    
            /**
            * Invoked when an action occurs.
            */
            public void actionPerformed(ActionEvent e) {
                System.out.println("Hello world!");
            }
    
    
            private static void createAndShowGUI() {
    
                JFrame frame = new JFrame("Hello");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                JComponent newContentPane = new Hello();
    
                newContentPane.setOpaque(true);
                frame.setContentPane(newContentPane);
                frame.pack();
                frame.setVisible(true);
            }
    
            public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        createAndShowGUI();
                    }
                });
            }
        }
    
    0 讨论(0)
提交回复
热议问题