java:Cannot refer to a non-final variable tx inside an inner class defined in a different method
publicclassActionIfextendsJFrame{privatestaticfinallongserialVersionUID=1L;Useru=null...
public class ActionIf extends JFrame {
private static final long serialVersionUID = 1L;
User u=null;
JTextField tx=null;
JButton jb=null;
private void showtext(){
JTextField tx=new JTextField();
User u=new User();
JButton jb=new JButton();
this.setSize(300,300);
this.setLayout(null);
tx.setSize(50,50);
tx.setBounds(50,50,50,50);
add(tx);
jb.setBounds(100,100,50,50);
jb.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e) {
String str=tx.getText();
}
});
tx不能写在那里吗? 展开
private static final long serialVersionUID = 1L;
User u=null;
JTextField tx=null;
JButton jb=null;
private void showtext(){
JTextField tx=new JTextField();
User u=new User();
JButton jb=new JButton();
this.setSize(300,300);
this.setLayout(null);
tx.setSize(50,50);
tx.setBounds(50,50,50,50);
add(tx);
jb.setBounds(100,100,50,50);
jb.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e) {
String str=tx.getText();
}
});
tx不能写在那里吗? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询