<small id='lcFHb'></small><noframes id='lcFHb'>

        <bdo id='lcFHb'></bdo><ul id='lcFHb'></ul>

      <legend id='lcFHb'><style id='lcFHb'><dir id='lcFHb'><q id='lcFHb'></q></dir></style></legend>
    1. <i id='lcFHb'><tr id='lcFHb'><dt id='lcFHb'><q id='lcFHb'><span id='lcFHb'><b id='lcFHb'><form id='lcFHb'><ins id='lcFHb'></ins><ul id='lcFHb'></ul><sub id='lcFHb'></sub></form><legend id='lcFHb'></legend><bdo id='lcFHb'><pre id='lcFHb'><center id='lcFHb'></center></pre></bdo></b><th id='lcFHb'></th></span></q></dt></tr></i><div id='lcFHb'><tfoot id='lcFHb'></tfoot><dl id='lcFHb'><fieldset id='lcFHb'></fieldset></dl></div>
      <tfoot id='lcFHb'></tfoot>
    2. 访问java内部类中的变量

      时间:2023-09-29
      <tfoot id='SnhJ0'></tfoot>

      <legend id='SnhJ0'><style id='SnhJ0'><dir id='SnhJ0'><q id='SnhJ0'></q></dir></style></legend>
          <bdo id='SnhJ0'></bdo><ul id='SnhJ0'></ul>
            1. <i id='SnhJ0'><tr id='SnhJ0'><dt id='SnhJ0'><q id='SnhJ0'><span id='SnhJ0'><b id='SnhJ0'><form id='SnhJ0'><ins id='SnhJ0'></ins><ul id='SnhJ0'></ul><sub id='SnhJ0'></sub></form><legend id='SnhJ0'></legend><bdo id='SnhJ0'><pre id='SnhJ0'><center id='SnhJ0'></center></pre></bdo></b><th id='SnhJ0'></th></span></q></dt></tr></i><div id='SnhJ0'><tfoot id='SnhJ0'></tfoot><dl id='SnhJ0'><fieldset id='SnhJ0'></fieldset></dl></div>

                  <tbody id='SnhJ0'></tbody>
              1. <small id='SnhJ0'></small><noframes id='SnhJ0'>

                本文介绍了访问java内部类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试创建一个 JLabels 数组,单击时它们都应该不可见.当试图通过需要访问用于声明标签的循环的迭代变量的内部类来设置鼠标侦听器时,就会出现问题.代码不言自明:

                I'm trying to create an array of JLabels, all of them should go invisible when clicked. The problem comes when trying to set up the mouse listener through an inner class that needs access to the iteration variable of the loop used to declare the labels. Code is self-explanatory:

                    for(int i=1; i<label.length; i++) {
                       label[i] = new JLabel("label " + i);
                       label[i].addMouseListener(new MouseAdapter() {
                          public void mouseClicked(MouseEvent me) {
                             label[i].setVisible(false);   // compilation error here
                          }
                       });
                       cpane.add(label[i]);
                    }
                

                我认为我可以通过使用 this 或者 super 而不是调用 label[i] 来克服这个问题内部方法,但我一直无法弄清楚.

                I thought that I could overcome this by the use of this or maybe super instead of the call of label[i] within the inner method but I haven't been able to figure it out.

                编译错误是:局部变量i是从内部类中访问的;需要声明为final`

                The compilation error is: local variable i is accessed from within inner class; needs to be declared final`

                我确定答案一定是我没有想到的非常愚蠢的事情,或者我犯了一些小错误.

                I'm sure that the answer must be something really silly I haven't thought of or maybe I'm making some small mistake.

                任何帮助将不胜感激

                推荐答案

                您的局部变量必须是 final 才能从内部(和匿名)类访问.

                Your local variable must be final to be accessed from the inner (and anonymous) class.

                您可以将代码更改为以下内容:

                You can change your code for something like this :

                for (int i = 1; i < label.length; i++) {
                    final JLabel currentLabel =new JLabel("label " + i); 
                    currentLabel.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent me) {
                            currentLabel.setVisible(false);   // No more compilation error here
                        }
                    });
                    label[i] = currentLabel;
                }
                

                来自 JLS:

                任何使用但未在内部类中声明的局部变量、形参或异常参数都必须声明为final.

                Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

                任何使用但未在内部类中声明的局部变量必须明确分配 (§16) 在内部类的主体之前.

                Any local variable used but not declared in an inner class must be definitely assigned (§16) before the body of the inner class.

                <小时>

                资源:

                • JLS - 内部类和封闭实例

                这篇关于访问java内部类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在我的 SWT 应用程序中检测 ctrl-f 下一篇:Java - Swing 中的 mouseMoved() 事件处理

                相关文章

                最新文章

                <tfoot id='4jaq5'></tfoot>

              2. <legend id='4jaq5'><style id='4jaq5'><dir id='4jaq5'><q id='4jaq5'></q></dir></style></legend>

                    <small id='4jaq5'></small><noframes id='4jaq5'>

                    • <bdo id='4jaq5'></bdo><ul id='4jaq5'></ul>
                    <i id='4jaq5'><tr id='4jaq5'><dt id='4jaq5'><q id='4jaq5'><span id='4jaq5'><b id='4jaq5'><form id='4jaq5'><ins id='4jaq5'></ins><ul id='4jaq5'></ul><sub id='4jaq5'></sub></form><legend id='4jaq5'></legend><bdo id='4jaq5'><pre id='4jaq5'><center id='4jaq5'></center></pre></bdo></b><th id='4jaq5'></th></span></q></dt></tr></i><div id='4jaq5'><tfoot id='4jaq5'></tfoot><dl id='4jaq5'><fieldset id='4jaq5'></fieldset></dl></div>