JSlider 和 JTextField 数组更改侦听器 - 从内部 class 引用的局部变量必须是最终的或实际上是最终的

JSlider and JTextField array change listener - Local variables referenced from inner class must be final or effectively final

我正在做作业,我必须按照提供给我的模板来完成此申请。 我有 2 个数组,一个是 JTextField[],另一个是 JSlider[].

这是报告错误的地方:

for (ix = 0; ix < cc.length; ++ix)
  {
    gbc.gridy = ix;
    gbc.gridx = 0;
   JLabel label = new JLabel(cc[ix]);
   panel.add(label, gbc);


    /* create a slider, set its options, and add it to the panel */
    csld[ix] = new JSlider(0, 255);


       csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(ChangeEvent e) {

              txt[ix].setText(Integer.toString(e.getSource())); //ERROR

        }
    });

    panel.add(csld[ix], gbc);

    gbc.gridx = 2;
    /* create a JTextField, set its options, and add it to the panel */

    txt[ix] = new JTextField();

    txt[ix].setText(Integer.toString(csld[ix].getValue()));
    System.out.println(Integer.toString(csld[ix].getValue()));
    panel.add(txt[ix], gbc);

    /* add a change listener  */

    txt[ix].addKeyListener(new KeyAdapter() {
       @Override
       public void keyReleased(KeyEvent e) {

           JTextField jtf = (JTextField)e.getSource();
           String typed = jtf.getText();

                if(!typed.matches("\d+") || typed.length() > 3) {
                    return;
                }
                int value = Integer.parseInt(typed);
                csld[ix].setValue(value); //ERROR


       }
    });`

我不确定为什么会显示此错误,并且没有其他 Whosebug 答案可以帮助我诊断它。我将 post 相关代码放在这里:

//imports

public class AssignRGBSliders implements Runnable
{
  static int width = 320;
  static int height = 128;
  static String title = "RGB Sliders";
  static int FIELD_WIDTH = 3;
  static int RED = 0;
  static int GREEN = 1;
  static int BLUE = 2;
  static int MIN = 0;
  static int MAX = 1;
  static String[] cc = {"Red", "Green", "Blue"};
  static int[][] RGB_MNMX = {{0, 255}, {0, 255}, {0, 255}};

  JFrame application;
  JTextField[] txt;
  JSlider[] csld;
  JPanel colorPanel;
  int[] curColor = {0, 0, 0};

public void run()
{
  JPanel panel, panel2;
  int ix;
  GridBagConstraints gbc = new GridBagConstraints();
  gbc.anchor = GridBagConstraints.FIRST_LINE_START;
  application = new JFrame();
  application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  application.setTitle(title);
  panel = new JPanel();
  panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  panel.setLayout(new GridBagLayout());
  application.add(panel);

   csld = new JSlider[cc.length];
   txt = new JTextField[cc.length];

  for (ix = 0; ix < cc.length; ++ix)
  {
    gbc.gridy = ix;
    gbc.gridx = 0;
   JLabel label = new JLabel(cc[ix]);
   panel.add(label, gbc);

    gbc.gridx = 1;
    /* create a slider, set its options, and add it to the panel */
    csld[ix] = new JSlider(0, 255);


       csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(ChangeEvent e) {
             JSlider slider = (JSlider)e.getSource();
            txt[ix].setText(Integer.toString(e.getSource()));

        }
    });

    panel.add(csld[ix], gbc);

    gbc.gridx = 2;
    /* create a JTextField, set its options, and add it to the panel */



    txt[ix] = new JTextField();

    txt[ix].setText(Integer.toString(csld[ix].getValue()));
    System.out.println(Integer.toString(csld[ix].getValue()));
    panel.add(txt[ix], gbc);

    /* add a change listener  */

    txt[ix].addKeyListener(new KeyAdapter() {
       @Override
       public void keyReleased(KeyEvent e) {

           JTextField jtf = (JTextField)e.getSource();
           String typed = jtf.getText();

                if(!typed.matches("\d+") || typed.length() > 3) {
                    return;
                }
                int value = Integer.parseInt(typed);
                csld[ix].setValue(value);


       }
    });



       /* slider.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            jtf.setText(Integer.toString(slider.getValue()));


        }
    }); */


  }
  colorPanel = new ColorPanel();
  colorPanel.setPreferredSize(new Dimension(width / 2, height / 2));
  ++gbc.gridy;
  gbc.gridx = 0;
  gbc.gridwidth = 3;
  panel.add(colorPanel, gbc);
  application.setSize(width, height);
  application.setLocationRelativeTo(null);
  application.pack();
  application.setVisible(true);
}
public static void main(String[] args)
{
  AssignRGBSliders dp = new AssignRGBSliders();
  SwingUtilities.invokeLater(dp);
}
class SliderChange implements ChangeListener
{


  /* instance variables */


    /* constructor */
public SliderChange() {


}
    /* implement the interface */
  public void stateChanged(ChangeEvent e) {
      JSlider source = (JSlider) e.getSource();
      System.out.println("Sukurac: " + source);

  }
}
class ColorPanel extends JPanel {

  public void paint(Graphics gr) {
     super.paint(gr);
     gr.setColor(new Color(csld[0].getValue(), csld[1].getValue(), csld[2].getValue()));
     System.out.println(csld[0].getValue() + " " + csld[1].getValue() + " " + csld[2].getValue());
     gr.fillRect(0, 0, this.getWidth(), this.getHeight());
  }
}
}

提前致谢!

您的 txt[ix] 变量不是 final 变量,因此当您在内部 class:

中使用它时,编译器会显示该错误
csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(ChangeEvent e) {

针对您的问题,我建议您将代码更改为:

    csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                 JSlider slider = (JSlider)e.getSource();
                 JTextField t = new ...//create instance
                 t.setText(Integer.toString(e.getSource()));//set text
                 txt[ix] = t;   //add current JTextField to array
            }
     });

ix 是您尝试使用它的上下文中的局部变量,编译器无法确定它在未来某个时刻的值,即使它可以,它也会可能是 cc.length - 1

我的一般建议是使用 HashMapJTextField 映射到 JSlider

Map<JSlider, JTextField> fieldMap;
//...
fieldMap = new HashMap<>();
//...
JSlider slider = new JSlider(0, 255);
JTextField field = new JTextField(10);
fieldMap.put(slider, field);

slider.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        JTextField field = fieldMap.get(slider);
        field.setText(Integer.toString(slider.getValue()));
    }
});

We haven't learnt/aren't allowed to use Map/HashMap

嗯,在那种情况下,我建议改用某种 List,因为它提供了有用的功能来确定 indexOf 元素

List<JTextField> fieldList;
List<JSlider> sliderList;
//...
JSlider slider = new JSlider(0, 255);
JTextField field = new JTextField(10);
fieldList.add(field);
sliderList.add(slider);

slider.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        int index = sliderList.indexOf(slider);
        JTextField field = fieldList.get(index);
        field.setText(Integer.toString(slider.getValue()));
    }
});

这个问题是 List 很容易不同步,所以我更喜欢 Map 这种事情

We haven't learnt/aren't allowed to use List/ArrayList

好吧。我们努力做到这一点。

基本上,您有对触发事件的 JSlider 的引用,我们需要知道它在 csld 中是 index 以便我们可以查找相应的 JTextField...

csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        for (int index = 0; index < csld.length; index++) {
            if (csld[index] == slider) {
                txt[index].setText(Integer.toString(slider.getValue()));
                break;
            }
        }

    }
});