java - 如何在匿名内部类中访问容器类的私有(private)类成员?

标签 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);
    }

最佳答案

尝试,

ClassName.this.foo

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

关于java - 如何在匿名内部类中访问容器类的私有(private)类成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574865/

相关文章:

java - 如何更改 JSeparator 的颜色?

javascript - 在可变时间启动计时器,而不是从头开始

java - RX java retryWhen 与计时器不重新订阅可观察

c - 格式字符串未使用数据参数 - 输出长整型

java - 添加到 ArrayList 时如何避免 "incompatible parameter types in lambda expression"?

java - Hadoop - 不支持的 major.minor 版本 51.0

java - 如何在 SOA 架构中使用事务和回滚

java - 按键绑定(bind)不起作用 - swing

java - 如何使用单个语句设置可见(真或假)多个组件

java - 使用 BufferStrategy 在 Java 中滑动图像