【发布时间】:2017-04-27 22:08:09
【问题描述】:
我正在做一个任务,我必须按照给我的模板来完成这个应用程序。
给了我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());
}
}
}
提前谢谢你!
【问题讨论】:
标签: java arrays swing jtextfield jslider