How to set Action Listener to 3 buttons

后端 未结 3 865
执念已碎
执念已碎 2021-01-21 23:59

I\'m trying to make a stopwatch with three buttons, \"Start\", \"Pause\", and \"Stop\". My instructor only taught us how to set Action Listeners to two buttons. How do I set u

3条回答
  •  逝去的感伤
    2021-01-22 00:31

    If you don't want to implement ActionListener you can add anonymous listener to your button like this:

     JButton startButton = new JButton("Start");
     JButton stopButton = new JButton("Stop");
     JButton pauseButton = new JButton("Pause");
     startButton.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                //start action logic here
            }
        });
     stopButton.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                //stop action logic here
            }
        });
     pauseButton.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                //action logic here
            }
        });
    

    And this solution have to work :)

提交回复
热议问题