如何更改JTextfield
内的ActionLIstener
的值?
cmdAnzeigen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
/* Cannot refer to a non-final variable TextOutput inside an
inner class defined in a different method*/
TextOutput.setText("Hello");
}
});
答案 0 :(得分:0)
在这里,您可以编写另一种方法,并将TextField值更改为此方法中的代码。等,
public class Demo {
static JTextField txtName;
static JButton jbSubmit;
public Demo()
{
txtName = new JTextField(10);
jbSubmit = new JButton("Submit");
jbSubmit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
change("XYZ");
}
});
}
public static void change(String name)
{
txtName.setText(name);
}
public static void main(String[] args) {
Demo d = new Demo();
JFrame jf=new JFrame();
jf.add(txtName);
jf.add(jbSubmit);
jf.setLayout(new FlowLayout());
jf.setVisible(true);
jf.setSize(500,200);
}
}