在 JPanel 中调整 PApplet 的大小

发布于 2024-11-18 22:06:14 字数 1906 浏览 3 评论 0原文

我一直在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-11-25 22:06:14

事实上,答案很简单。看来除非使用绘图,否则 PApplet 不会调整大小。 一样简单

PApplet pa = new PApplet();

就像将其转变为

PApplet pa = new PApplet()
{
public void draw(){};
};

或用适当扩展的 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

PApplet pa = new PApplet();

into

PApplet pa = new PApplet()
{
public void draw(){};
};

or replacing it with a properly extended PApplet.

I just didn't make my personal proof-of-concept detailed enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文