补充:有一段少打了个括号
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == open )
open.setText("OFF");
}
});

解决方案 »

  1.   

    呵呵,再e.getSource()前面进行一下转换更好!!用这种形式:(JButton)e.getSource()
      

  2.   

    测了,楼主编译如果通不过,把open改个名字,不要用常用关键字就好了。比如jbOpen。
      

  3.   

    如果你使用好的编辑器,就会给你提示出来,或者看编译时的错误也应该是/、local variable open is accessed from within inner class;needs to be declared final所以你把open声明为final就能把问题解决了完整代码为
    import javax.swing.UIManager; 
    import java.io.*; 
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    public class test_inside 
    {
            private JButton A,open;
            public static void main(String[] args)
            {
                JButton A = new JButton("a");
                final JButton open = new JButton("open");
            
                JPanel JP = new JPanel();
                JP.setLayout(new FlowLayout());
            
                JFrame JF = new JFrame("true or false");
            
            
                JF.getContentPane().add(JP, BorderLayout.CENTER);
                JP.add(open);
                JP.add(A);
            
                JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JF.setBounds(220, 80, 250, 500);
                JF.setVisible(true);
            
                open.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        if (e.getSource() == open)   //local variable open is accessed from within inner class;needs to be declared final
                            open.setText("OFF");
                    }
                });
            
            }
    }提醒一下,类名一定要用大写
      

  4.   

    我的测试代码:import javax.swing.UIManager; 
    import java.io.*; 
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    public class test_inside extends JFrame
    {
        private JButton jbA;
        private JButton jbOpen;
        
        public test_inside()
        {
            jbA    = new JButton("a");
            jbOpen = new JButton("open");
            
            JPanel jp = new JPanel();
            jp.setLayout(new FlowLayout()); 
            
                    
            getContentPane().add(jp,BorderLayout.CENTER);
            jp.add(jbOpen);
            jp.add(jbA);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
            setBounds(220,80,250,200);
            setVisible(true);
            
            jbOpen.addActionListener
            (
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        if (e.getSource() == jbOpen )
                        {
                            if ( jbOpen.getText().equals("open") )
                                jbOpen.setText("close");
                            else
                                jbOpen.setText("open");
                        }
                        
                            
                    }
                }
            );
        }
        public static void main(String[] args)
        {
            test_inside app = new test_inside();
            
            app.addWindowListener
            (
                new WindowAdapter() 
                {
                    public void windowClosing( WindowEvent e )
                    {
                        System.exit( 0 );
                    }
                }
            );
         }
    }
      

  5.   

    1、
    将private JButton A,open;改为
    static JButton A,open;2、
    JButton A=new JButton("a");
    JButton open=new JButton("open");
    改为
    A=new JButton("a");
    open=new JButton("open");
      

  6.   

    总结:
    有3种方法都可行:
    1, 将private JButton A,open;改为    static JButton A,open;(这样就不需要 JButton source =  (JButton)e.getSource();进行强制转化了)
      
     public class test_inside extends JFrame
    {
    static JButton A,open;
    public static void main(String[] args)
    {
    A=new JButton("a");
     open=new JButton("open");2, 使用  JButton source =  (JButton)e.getSource(); 也可以解决这个问题import javax.swing.*;
    import java.awt.event.*;public class Check extends JFrame
    {
    private JButton jb1 = new JButton("Button1"); public Check()
    {
    setSize(300,200);
    ActionListener al = new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    JButton source =  (JButton)e.getSource();
    String  str = source.getText();
    if( source == jb1 )
    if(str == "Button1")
    jb1.setText("按钮1");
    else
    jb1.setText("Button1");
    }
    }; jb1.addActionListener(al);
    getContentPane().add(jb1); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    show();
    }
    public static void main(String[] args)
    {
    Check check=new Check();
    }

    }
    3, 使用final                 final JButton open = new JButton("open");
     
      

  7.   

    我还有问题,final的方法,我很新鲜,能告诉我为什么吗?
      

  8.   

    final的类和方法是终止的意思,不可以被继承!!!