0

我是编程 GUI 和处理的新手。我的问题是如何获得可以滚动浏览的复选框列表?我想要的正是右边的国家列表(http://goo.gl/MIKHi4)。

我查看了 ControlP5 库并能够找到复选框,但我不知道如何制作它们的可滚动列表。

谢谢你。

4

2 回答 2

1

上周我也一直在寻找这个,希望有一个现成的库可以让我轻松地将可滚动的复选框添加到我的应用程序中,但最后我没有运气。最后,我所做的是实现我自己的可滚动复选框列表。

首先,我添加了一个 ControlP5 滑块作为滚动条,然后在每一帧,从滑块中获取值并根据该值绘制特定的复选框。

假设您有一个包含 200 个国家的列表供用户选择。然后代码将如下所示:

ControlP5 cp5;
Slider scrollBar;
PFont fLabel;
int boxOver = -1; //Indicate mouse is over which checkbox
boolean[] boxSelected; //Checkbox selected or not
void setup() {
    size(1024, 800);
    colorMode(HSB, 360, 100, 100);
    cp5 = new ControlP5();
    scrollbar = cp5.addSlider("scrollbar")
         .setPosition(1005, 110)
         .setRange(0, 180)
         .setSize(15, 490)
         .setHandleSize(30)
         .setSliderMode(Slider.FLEXIBLE)
         .setValue(180); //Put handler at top because 0 value is at bottom of slider
    fLabel = createFont("Arial", 12, false);
    boxSelected = new boolean[200];
    for(int i=0;i<200;i++) {
        boxSelected[i] = false;
    }
}

void draw() {
    noFill();
    stroke(200, 255);
    rect(820, 110, 200, 490); //The outline of the scrollable box

    stroke(150, 255);
    int count = 0;

    //Suppose that you want to display 20 items each time
    for(int i=180-(int)scrollBar.getValue();i<180-(int)scrollBar.getValue()+20;i++) {
        if(boxOver < 0) {
            if(mouseX>=825 && mouseX<837 && mouseY >= 120+count*24 && mouseY <= 132+count*24) {
                boxOver = i;
                cursor(HAND);
            }
        }
        if(boxSelected[i]) {
            fill(50);  //If the box is selected, fill this box
        } else {
            fill(360);
        }
        rect(825, 120+count*24, 12, 12); //Draw the box

        //Draw the label text
        textFont(fLabel);
        fill(50);
        text(countries[i], 843, 132+count*24); //Suppose the country names are stored in countries[]

        count++;
    }
}

void mousePressed() {
    if(boxOver >=0) {
        boxSelected[boxOver] = !boxSelected[boxOver]; //Toggle selection
    }
}

希望这对您或将来可能遇到相同问题的任何人有所帮助。

于 2013-10-09T16:13:19.250 回答
0

现在在实验例子中有一个例子叫做ControlP5SliderList

于 2015-12-16T12:32:40.003 回答