2012-04-16 12:27:15
4
private void pushButtonActionPerformed(java.awt.event.ActionEvent evt)
{
final int c=0;
final JDialog d=new JDialog();
JLabel l=new JLabel("Enter the Element :");
JButton but1=new JButton("OK");
JButton but2=new JButton("Cancel");
final JTextField f=new JTextField(10);
JPanel panel = new JPanel();
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c=Integer.parseInt(f.getText());
d.setVisible(false);
d.dispose( );
}
});
but2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
d.dispose( );
}
});
}
I am using netbeans 7.1.1. This is my code here i have declared 'c' as "final int" but the line "c=Integer.parseInt(f.getText());" i am getting an error "cannot assign a value to a final variable". If i am deleting the word final from the declaration and making it just as "int c" then in the same line i get an error "local variable c cannot be accessed from within a class;needs to be declared final". can anyone tell me why is this happening ?
在Java编程中,遇到一个关于final变量的问题。在事件监听器中尝试将`JTextField`的输入值赋给声明为final的变量`c`时,出现错误提示。如果去掉final关键字,又会导致局部变量不能在匿名内部类中访问。这个问题涉及到Java中final变量的作用域和使用规则。解决方案可能涉及到理解final变量如何在匿名内部类中使用。
5664

被折叠的 条评论
为什么被折叠?



