gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:18:35 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com