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

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

      <bdo id='ILYcD'></bdo><ul id='ILYcD'></ul>
  • <tfoot id='ILYcD'></tfoot>

        <legend id='ILYcD'><style id='ILYcD'><dir id='ILYcD'><q id='ILYcD'></q></dir></style></legend>
      1. Java - 如何更改“本地"?事件侦听器中的变量

        时间:2023-09-30

          <tbody id='EkNka'></tbody>

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

              <tfoot id='EkNka'></tfoot>

              1. <small id='EkNka'></small><noframes id='EkNka'>

                • 本文介绍了Java - 如何更改“本地"?事件侦听器中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有个小问题,希望有人能回答.基本上我有一个字符串变量,它需要根据组合框中的值进行更改,该组合框中附加了一个事件侦听器.但是,如果我将字符串设为 final,则无法更改,但如果我不将其设为 final,则 eclipse 会抱怨它不是最终的.最好(和最简单)的解决方法是什么?

                  Got a quick question which I hope someone can answer. Basically I have a String variable which needs changing based upon the value in a combo box which has an event listener attached to it. However if I make the string final then it cant be changed, but if i don't make it final then eclipse moans that it isn't final. Whats the best (and simplest) work around?

                  代码如下

                  final String dialogOutCome = "";
                  
                  //create a listener for the combo box
                  Listener selection = new Listener() {
                  
                      public void handleEvent(Event event) {
                      //get the value from the combo box
                      String comboVal = combo.getText();
                  
                      switch (comboVal) {
                           case "A": dialogOutCome = "a";
                           case "B": dialogOutCome = "b";
                           case "C": dialogOutCome = "c";
                           case "D": dialogOutCome = "d";
                      }
                     }
                  };
                  

                  推荐答案

                  你不能.

                  考虑一下:

                  • 只要声明的方法运行,局部变量就存在.
                  • 只要方法调用结束(通常是因为方法存在),变量就会消失
                  • 听众可以(而且通常确实)活得更久

                  那么当方法已经返回并且监听器尝试修改局部变量时会发生什么?

                  So what should happen when the method returned already and the listener tries to modify the local variable?

                  因为这个问题没有很好的答案,他们决定通过不允许访问非final 局部变量来使这种情况变得不可能.

                  Because this question does not have a really good answer, they decided to make that scenario impossible by not allowing access to non-final local variables.

                  有两种方法可以解决这个问题:

                  There are two ways around this problem:

                  • 尝试更改字段而不是局部变量(这可能也更适合监听器的生命周期)
                  • 使用 final 局部变量,您可以更改其中的 content(例如 ListString[] 使用单个元素).
                  • try to change a field instead of a local variable (this probably also fits better with the life-time of the listener) or
                  • use a final local variable, of which you can change the content (for example a List or a String[] with a single element).

                  这篇关于Java - 如何更改“本地"?事件侦听器中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:没有任何事件发生 下一篇:如何正确地将 MouseHandler 添加到我的 JFreeChart-FX

                  相关文章

                  最新文章

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

                    <legend id='slrYN'><style id='slrYN'><dir id='slrYN'><q id='slrYN'></q></dir></style></legend>

                      • <bdo id='slrYN'></bdo><ul id='slrYN'></ul>