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

时间:2017-04-27 22:08:10

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

提前谢谢!

2 个答案:

答案 0 :(得分:1)

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

我们尚未学习/不允许使用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这类事情

  

我们尚未学习/不允许使用List / ArrayList

Oookaay。我们这样做很难。

基本上,您引用了引发该事件的JSlider,我们需要在index中知道csld,因此我们可以查找相应的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;
            }
        }

    }
});

答案 1 :(得分:0)

您的txt[ix]变量不是final变量,以便编译器在内部类中使用它时显示错误:

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