我是新来的java,我有以下问题: 我添加了一个ActionListener到一个按钮,我想从它访问一个数字,但它不工作的方式。我找到了它,但我找不到答案。 代码看起来像现在这样:如何从ActionListener获取整数?
public class example extends JPanel{
int text;
public example(){
JButton button = new JButton("x");
JTextField textField = new JTextField();
add(textField);
add(button);
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
text = Integer.parseInt(textField.getText());
}
}
button.addActionListener(al);
system.out.println(text);
}
}
问题与逻辑有关。 – Dungnbhut
是的,它可以工作,但是我怎样才能得到ActionListener外部的Integer? – Andy
检查编辑。我想你应该看看Java的基础知识 – npk