我需要编写一个按钮,在点击它时设置值。如何写一个actionPerformed方法来实现呢?
int a = 0;
void setValue(){
a = 5;
}
public void actionPerformed(ActionEvent e) {
/*when*/ e.getActionCommand() /*call setValue()*/
}
答案 0 :(得分:0)
创建一个按钮,然后像这样添加ActionListener
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;
}
}
答案 1 :(得分:0)
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");
}
});