java 如何在处理中制作文本框功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17634070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a textbox function in processing?
提问by Dan Van
I'm looking to make about 10 textboxes for the user to type into then store that value as a variable. Is there anyway to make a textbox function with the parameters being the position?
我希望制作大约 10 个文本框供用户输入,然后将该值存储为变量。反正有没有用参数作为位置的文本框函数?
回答by Pwdr
Yes, that's possible using the library controlP5.
是的,这可以使用库controlP5。
import controlP5.*;
ControlP5 cp5;
String[] textfieldNames = {"tf1", "tf2", "tf3", "tf4", "tf5"};
void setup() {
size(700,400);
PFont font = createFont("arial",20);
cp5 = new ControlP5(this);
int y = 20;
int spacing = 60;
for(String name: textfieldNames){
cp5.addTextfield(name)
.setPosition(20,y)
.setSize(100,40)
.setFont(font)
.setFocus(true)
.setColor(color(255,0,0))
;
y += spacing;
}
textFont(font);
}
void draw() {
background(0);
}
void controlEvent(ControlEvent theEvent) {
if(theEvent.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue()
);
}
}
回答by user2468700
If you're a proficient Java programmer, you may consider using the Swing Library, the primary Java GUI widget toolkit. However, you'd also find yourself messing around with the Processing core code. Don't do that.
如果您是一名熟练的 Java 程序员,您可以考虑使用Swing 库,这是主要的 Java GUI 小部件工具包。但是,您也会发现自己在处理核心代码。不要那样做。
The main rule when using Java code [in a Processing sketch]: you cannot use most of the AWT or Swing (which is built on the AWT), because it will interfere with the graphics model. If you want to add scroll bars and buttons to your projects, you should make them using Processing code, or embed your Processing applet inside another Swing or AWT application. Even if they appear to work, such sketches will usually break when you try to run on other operating systems or other versions of Java. – Processing FAQ
使用 Java 代码 [在处理草图中] 时的主要规则:不能使用大部分 AWT 或 Swing(构建在 AWT 上),因为它会干扰图形模型。如果您想向您的项目添加滚动条和按钮,您应该使用 Processing 代码制作它们,或者将您的 Processing 小程序嵌入另一个 Swing 或 AWT 应用程序中。即使它们看起来有效,当您尝试在其他操作系统或其他版本的 Java 上运行时,这些草图通常也会中断。–处理常见问题
If you're not a Java programmer, stick with Processing libraries or make your own text field class.
如果您不是 Java 程序员,请坚持使用 Processing 库或制作您自己的文本字段类。
The popular ControlP5GUI library has built-in classes for text fieldsand text areas. As of yet, This version has been tested with processing 2.0b7 and it may not work with the latest 2.0 release.
流行的ControlP5GUI 库具有用于文本字段和文本区域的内置类。到目前为止,此版本已经过处理 2.0b7 的测试,它可能不适用于最新的 2.0 版本。
You may also use the G4Plibrary and its text areaimplementation.
If it's the first time you're using external libraries, open Processing and add contributed libraries by selecting "Add Library..." from the "Import Library..." submenu within the upper bar menu.
如果这是您第一次使用外部库,请打开 Processing 并通过从上方栏菜单中的“导入库...”子菜单中选择“添加库...”来添加贡献的库。
EDIT: I've never tried it, but Interfascia(alpha release) has a text fieldclass too. The documentationseems easy to read and the code easy to use.
编辑:我从未尝试过,但Interfascia(alpha 版本)也有一个文本字段类。文档看起来很容易阅读,代码也很容易使用。