java - JSlider 和 JTextField 数组更改监听器 - 从内部类引用的局部变量必须是最终的或有效的最终

标签 java arrays swing jtextfield jslider

我正在做一项作业,我必须按照给我的模板来完成此申请。 我得到了 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


       }
    });`

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

//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());
  }
}
}

提前谢谢您!

最佳答案

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

我的一般建议是使用 HashMap映射JTextFieldJSlider

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 很容易s 不同步,所以我更喜欢 Map对于这种事情

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

哎呀。我们以艰难的方式做到了这一点。

基本上,您可以引用 JSlider哪个触发了事件,我们需要知道它是 indexcsld这样我们就可以查找对应的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;
            }
        }

    }
});

关于java - JSlider 和 JTextField 数组更改监听器 - 从内部类引用的局部变量必须是最终的或有效的最终,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43668708/

相关文章:

java - 为什么在 Java 1.8 中这样排序

java - 在多个安全检查之间传递安全上下文数据

python - 在 Python 中循环遍历多波段栅格中的每个像素

java - 值没有存储在数组中?

java - 单击 JButton 后不出现 JPanel

java - 正则表达式用于搜索和替换字符串中的文本

java - put 不适用于 HashMap 类型

php - 在 BD Laravel 中插入数组

java - JTextArea append() 方法写入两次?

java - 在Java中获取资源