我在程序中使用ControlP5 ListBox时遇到了一个奇怪的问题。我从ControlP5库的官方页面找到了一个ListBox使用示例:http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5listBox/ControlP5listBox.pde我做了一些简单的更改,比如更改列表框的宽度和高度,设置不同的颜色。主要区别在于我使用字符串的数组列表来填充列表框。以下是此scetch的完整代码:
import javax.swing.*;
PFrame f;
public class PFrame extends JFrame {
public SecondApplet s;
public PFrame(int width, int height,ArrayList<String> companiesTxt) {
setBounds(100, 100, width, height);
s = new SecondApplet(companiesTxt);
add(s);
s.init();
show();
}
}
public class SecondApplet extends PApplet {
ArrayList<String> companiesText;
ControlP5 cp5;
ListBox l;
ControlWindow controlWindow;
SecondApplet(ArrayList<String> cmpTxt){
companiesText = cmpTxt;
}
void setup(){
size(400, 720);
ControlP5.printPublicMethodsFor(ListBox.class);
cp5 = new ControlP5(this);
l = cp5.addListBox("myList")
.setPosition(0, 0)
.setSize(382, 720)
.setItemHeight(18)
.setBarHeight(18)
.setColorBackground(color(243, 45,98))
.setColorActive(color(0))
.setColorForeground(color(255, 100,0))
;
l.captionLabel().toUpperCase(true);
l.captionLabel().set("Companies");
l.captionLabel().setColor(0xffff0000);
l.captionLabel().style().marginTop = 3;
l.valueLabel().style().marginTop = 3;
for (int i=0;i != companiesText.size();i++) {
ListBoxItem lbi = l.addItem(companiesText.get(i), i);
lbi.setColorBackground(color(243, 45,98));
}
}
void draw(){
}
}
当我尝试滚动此列表框时,我得到一个例外:
java.lang.ArrayIndexOutOfBoundsException: 8217
at controlP5.BitFont.getGlyph(Unknown Source)
at processing.core.PGraphics.textCharImpl(PGraphics.java:4681)
at processing.core.PGraphics.textLineImpl(PGraphics.java:4669)
at processing.core.PGraphicsJava2D.textLineImpl(PGraphicsJava2D.java:1787)
at processing.core.PGraphics.textLineAlignImpl(PGraphics.java:4659)
at processing.core.PGraphics.text(PGraphics.java:4356)
at processing.core.PGraphics.text(PGraphics.java:4307)
at processing.core.PApplet.text(PApplet.java:13183)
at controlP5.ControlFont.draw(Unknown Source)
at controlP5.Label.draw(Unknown Source)
at controlP5.Label$SinglelineLabel.draw(Unknown Source)
at controlP5.Label.draw(Unknown Source)
at controlP5.Button$ButtonView.display(Unknown Source)
at controlP5.Button$ButtonView.display(Unknown Source)
at controlP5.Controller.draw(Unknown Source)
at controlP5.ControllerGroup.drawControllers(Unknown Source)
at controlP5.ControllerGroup.draw(Unknown Source)
at controlP5.ControllerGroup.drawControllers(Unknown Source)
at controlP5.ControllerGroup.draw(Unknown Source)
at controlP5.ControlWindow.draw(Unknown Source)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1236)
at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1229)
at processing.core.PApplet.handleMethods(PApplet.java:1423)
at processing.core.PApplet.handleDraw(PApplet.java:2401)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Unknown Source)
我几乎绝望,因为我不明白在这种情况下会出现什么问题?拜托,你能给我任何建议吗?
答案 0 :(得分:1)
问题解决了。我使用的是一些不是aschii字符的字符串。 GetGlyph函数如下所示:
public Glyph getGlyph(char c) {
return glyphs[(int) (c)];
}
因此,当您想使用列表框将字符转换为aschii时:
for (int i=0;i < companiesText.size();i++) {
try{
ListBoxItem lbi = l.addItem(newString(companiesText.get(i).getBytes("US-ASCII"),"US-ASCII"), i);
lbi.setColorBackground(color(243, 45,98));
}catch(Exception e){
}
}