如何在匿名内部类中访问容器类的私有类成员?

时间:2011-09-27 19:50:18

标签: java swing timer anonymous-class anonymous-inner-class

如何从AbstractActionClass中访问包含函数initTimer()的类的所有成员字段?
感谢

private void initTimer()
    {
       Action updateClockAction = new AbstractAction() {
                public void actionPerformed(ActionEvent e){
                    JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();
                    secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
                    secLabel.setForeground(Color.red);
                    secLabel.setText(Integer.toString(m_TimerSeconds));
                    if(m_TimerSeconds >0)
                    {
                        m_TimerSeconds--;
                    }
                    else if (m_TimerSeconds == 0)
                    {
                        m_Timer.stop();
                        m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds().setText("0");
                        m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish().setVisible(false);
                        //Checking whether time ended for both players and no solution was recieved
                        if(!m_WasGameDecisived)
                        {
                            System.out.println("Tie - No one had a solution in the given time");
                            //askUserForAnotherRoundLeaveTableOrExitProgram();//////////////////////////////////////////////To implement
                        }
                    }
                }
            };
            m_Timer = new Timer(1000, updateClockAction);
    }

2 个答案:

答案 0 :(得分:4)

尝试,

ClassName.this.foo

其中foo是一个类成员。有关详细信息,请参阅JLS §15.8.4 Qualified this

答案 1 :(得分:2)

假设您的外部课程被称为OuterClass,那么OuterClass.this.whatever