在 JPanel 中调整 PApplet 的大小
我一直在尝试将 PApplet 粘贴在 JFrame 中,并在用户更改 JFrame 的大小时调整其大小,但文档(当存在时)不清楚。 这里 我被告知要使用
void setup() {
frame.setResizable(true);
}
void resize(int w, int h) {
super.resize(w,h);
frame.setSize(w,h);
}
,但当我尝试时,它似乎是框架为空,而且我不清楚如何确保调用 resize get 。
有人让它发挥作用吗?
编辑:简化的代码。
这是我的一些代码,基于 http://wiki.processing.org/w/Swing_JSliders:
public class MyPanel extends JPanel
{
public MyPanel()
{
//have tried both BoxLayout and BorderLayout
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//if, instead of a PApplet I use a JPanel, this resizes fine
// it's only when using a PApplet that it won't resize
//add our processing window
PApplet pa = new PApplet();
pa.init();
add(pa);
}
// create external JFrame
private static void createGui()
{
// create new JFrame
JFrame jf = new JFrame("test");
// this allows program to exit
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You add things to the contentPane in a JFrame
jf.getContentPane().add(new MyPane());
// keep window from being resized
//jf.setResizable(false);
// size frame
jf.pack();
// make frame visible
jf.setVisible(true);
}
public static void main(String[] args)
{
// threadsafe way to create a Swing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGui();
}
}
);
}
}
谢谢,非常感谢任何帮助。
I've been trying to stick a PApplet in a JFrame and have it resize when the user changes the size of the JFrame, but the documentation, when it exists, is unclear. Here I am told to use
void setup() {
frame.setResizable(true);
}
void resize(int w, int h) {
super.resize(w,h);
frame.setSize(w,h);
}
but when I try that it seems frame is null, and I'm not clear on how to make sure that resize get's called, anyway.
Has anyone gotten this to work?
EDIT: simplified code.
Here is some of my code, based on http://wiki.processing.org/w/Swing_JSliders:
public class MyPanel extends JPanel
{
public MyPanel()
{
//have tried both BoxLayout and BorderLayout
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//if, instead of a PApplet I use a JPanel, this resizes fine
// it's only when using a PApplet that it won't resize
//add our processing window
PApplet pa = new PApplet();
pa.init();
add(pa);
}
// create external JFrame
private static void createGui()
{
// create new JFrame
JFrame jf = new JFrame("test");
// this allows program to exit
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You add things to the contentPane in a JFrame
jf.getContentPane().add(new MyPane());
// keep window from being resized
//jf.setResizable(false);
// size frame
jf.pack();
// make frame visible
jf.setVisible(true);
}
public static void main(String[] args)
{
// threadsafe way to create a Swing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGui();
}
}
);
}
}
thanks, any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,答案很简单。看来除非使用绘图,否则 PApplet 不会调整大小。 一样简单
就像将其转变为
或用适当扩展的 PApplet 替换它
。我只是没有让我的个人概念验证足够详细。
The answer is quite simple, actually. It seems that unless draw is being used the PApplet won't get resized. It's as simple as turning
into
or replacing it with a properly extended PApplet.
I just didn't make my personal proof-of-concept detailed enough.