如何用 Processing 打开多个 windows?
How to open multiple windows with Processing?
我正在尝试使用 Processing 创建两个 windows。在您将此标记为重复之前,因为还有其他类似的问题,我有一个特定的错误,我找不到解决方案。当我尝试 add(s)
时,出现错误 The method add(Component) in the type Container is not applicable for the arguments (evolution_simulator.SecondApplet)
我不确定如何解决这个问题,如有任何帮助,我们将不胜感激。这是代码:
import javax.swing.*;
PFrame f;
void setup() {
size(320, 240);
f = new PFrame();
}
void draw() {
}
public class PFrame extends JFrame {
SecondApplet s;
public PFrame() {
setBounds(100,100,400,300);
s = new SecondApplet();
add(s); // error occurs here
s.init();
show();
}
}
public class SecondApplet extends PApplet {
public void setup() {
size(400, 300);
noLoop();
}
public void draw() {
}
}
错误消息的原因是因为 add()
函数需要 Component
,而 PApplet
不是 Component
。这是因为从处理 3 开始,PApplet
不再扩展 Applet
,因此将它用作 Component
的旧代码将不再有效。
相反,您可以创建一个扩展 PApplet
的 class 作为第二个 window,然后使用第二个 PApplet
调用 PApplet.runSketch()
参数:
void setup() {
size(100, 100);
String[] args = {"TwoFrameTest"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
public class SecondApplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
我正在尝试使用 Processing 创建两个 windows。在您将此标记为重复之前,因为还有其他类似的问题,我有一个特定的错误,我找不到解决方案。当我尝试 add(s)
时,出现错误 The method add(Component) in the type Container is not applicable for the arguments (evolution_simulator.SecondApplet)
我不确定如何解决这个问题,如有任何帮助,我们将不胜感激。这是代码:
import javax.swing.*;
PFrame f;
void setup() {
size(320, 240);
f = new PFrame();
}
void draw() {
}
public class PFrame extends JFrame {
SecondApplet s;
public PFrame() {
setBounds(100,100,400,300);
s = new SecondApplet();
add(s); // error occurs here
s.init();
show();
}
}
public class SecondApplet extends PApplet {
public void setup() {
size(400, 300);
noLoop();
}
public void draw() {
}
}
错误消息的原因是因为 add()
函数需要 Component
,而 PApplet
不是 Component
。这是因为从处理 3 开始,PApplet
不再扩展 Applet
,因此将它用作 Component
的旧代码将不再有效。
相反,您可以创建一个扩展 PApplet
的 class 作为第二个 window,然后使用第二个 PApplet
调用 PApplet.runSketch()
参数:
void setup() {
size(100, 100);
String[] args = {"TwoFrameTest"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
public class SecondApplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}