Frage

I need to program a button which sets value when it's clicked. How to write an actionPerformed method to work so?

int a = 0;

void setValue(){
  a = 5;
}

public void actionPerformed(ActionEvent e) {
  /*when*/ e.getActionCommand() /*call setValue()*/
}
War es hilfreich?

Lösung

  JButton button = new JButton(" >> JavaProgrammingForums.com <<");
        //Add action listener to button
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                a = 5 ;
                System.out.println("You clicked the button");
            }
        }); 

Andere Tipps

Make a button and then add ActionListener to it like this

private static JButton btn;
private statin int a;
btn = new JButton();
int a = 0;
btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
    a = 15;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top