java输入键JTextField
我有一个JTextField,它有一个Actionlistener。现在,当我按下enter键时,我想让它做某些事情。我正在使用一个共享ActionListener,所以我试图在JTextField上执行getSource,但它不起作用!希望有人能帮忙
JTextField txtProductAantal = new JTextField(String.valueOf(WinkelApplication.getBasket().getProductAmount(productdelete)));
txtProductAantal.setBounds(340, verticalPosition + i * productOffset, 40, 20);
txtProductAantal.addActionListener(this);
add(txtProductAantal);
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnEmptyBasket) {
WinkelApplication.getBasket().empty();
WinkelApplication.getInstance().showPanel(new view.CategoryList());
}
if(event.getSource() == txtProductAantal){
String productgetal = txtProductAantal.getText();
txtProductAantal.setText(productgetal);
WinkelApplication.getInstance().showPanel(new view.Payment());
}
}
# 1 楼答案
Object
进行比较比如
JTextFields
(在if - else
语句中避免instanceof
)可以直接将对象强制转换为JTextField
JTextField source = (JTextField) event.getSource();
编辑
one from the next adviced 17 problems.
please to read suggestion by @Andrew Thompson, again
1) For better help sooner, post an SSCCE.
2) txtProductAantal.setBounds(..)
Use layouts to avoid the next 17 problems.
我的代码按预期工作
按回车键打印我