[英]how do get the account id when clicking submit button then display the client details on the label?
// submit button
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btnSubmit = new JButton("Submit");
//label that used to display the name, house number, postcode
JLabel lblNameDisplay = new JLabel("-");
JLabel lblHouseNoDisplay = new JLabel("-");
JLabel lblPostCodeDisplay = new JLabel("-");
Object accID = e.getSource();
//when clicking the submit, should get account id and display the client details on the label
if(accID==btnSubmit){
}
}
In your code you have two btnSubmit variables 在您的代码中,您有两个btnSubmit变量
JButton btnSubmit = new JButton("Submit"); <-----
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btnSubmit = new JButton("Submit"); <-----
If you do if(accID==btnSubmit)
- Which one are you using? 如果执行
if(accID==btnSubmit)
-您正在使用哪一个?
But anyway, 但无论如何,
The actionListener is bound to your btnSubmit
button so getSource()
is going to return the btnSubmit
object. actionListener绑定到您的
btnSubmit
按钮,因此getSource()
将返回btnSubmit
对象。
Any code within 内的任何代码
public void actionPerformed(ActionEvent e) { ... }
will be executed when clicking the "Submit" button because you created an anonymous action listener specifically for use with the button (hence use of anonymous inner class). 单击“提交”按钮时将执行该操作,因为您创建了专门用于该按钮的匿名操作侦听器(因此使用了匿名内部类)。
Therefore, all you really need to put in the actionPerformed(...) method would be the assignment of the new labels (assuming that they already exist or you got them somewhere). 因此,您真正需要放入actionPerformed(...)方法的就是分配新标签(假设它们已经存在或您将它们放在某个地方)。
eg 例如
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblExampleLabel.setText("This is what the label will become after clicking the button");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.