How can I find out which button was clicked?

前端 未结 4 895
情话喂你
情话喂你 2020-12-01 12:51

I\'ve got my buttons working right, and I\'m a listener to each button like this:

for(int i = 0; i <= 25; ++i) {
    buttons[i] = new Button(Character.toS         


        
相关标签:
4条回答
  • 2020-12-01 13:27

    try this

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand());
        }
    };
    
    0 讨论(0)
  • 2020-12-01 13:33
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        l1.setText("");//name of level what you want
    
        t1.setText(null);//text field what you want 
    
        t2.setText(null);//text field what you want
    }
    
    0 讨论(0)
  • 2020-12-01 13:41

    In order to get label, try this.

    ActionListener actionListener = new ActionListener()
    {
          public void actionPerformed(ActionEvent actionEvent) {
                JButton button = (JButton)actionEvent.getSource();
                String label = button.getLabel(); //Deprecated 
    
                String label2 = button.getText();
         }
    };
    
    0 讨论(0)
  • 2020-12-01 13:43

    ActionEvent has a method getActionCommand() that will get a JButton's actionCommand String. This is usually it's text as well (for JButtons).

    0 讨论(0)
提交回复
热议问题