如何从另一个 class 添加 Canvas 到 JFrame?

How to add Canvas to JFrame from another class?

我知道我可以只在 JFrame class 中编写 Canvas,但是如果我想要一个单独的 canvas class 怎么办?

我是 java 编程新手。很抱歉,如果这只是一个简单的问题。

public class TheFrame {

    private JFrame theframe;
    private String title = "Test";
    private int width = 1024;
    private int height = 576;

    public TheFrame(){
        theframe = new JFrame(title);
        theframe.setSize(width, height);
        theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theframe.setLocationRelativeTo(null);
        theframe.setVisible(true);

        //call the canvas
        TheCanvas thecanvas = new TheCanvas(width, height);

        //!
        theframe.add(thecanvas);
    }    
}
public class TheCanvas {

    private int width;
    private int height;
    private Canvas thecanvas;
    public TheCanvas(int width, int height){
        this.width = width;
        this.height = height;
        thecanvas = new Canvas();
        thecanvas.setPreferredSize(new Dimension(width, height));
        thecanvas.setMaximumSize(new Dimension(width, height));
        thecanvas.setMinimumSize(new Dimension(width, height));

        //!
        theframe.add(thecanvas);
    }
}

单独的方法示例:这适用于 "private canvas thecanvas" 对象。

    public TheFrame(){
        theframe = new JFrame(title);
        theframe.setSize(width, height);
        theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theframe.setLocationRelativeTo(null);
        theframe.setVisible(true);

        //call the canvass
        TheCanvasX(width, height);

        theframe.add(thecanvas);
    }    

    public void TheCanvasX(int width, int height){
        this.width = width;
        this.height = height;
        thecanvas = new Canvas();
        thecanvas.setPreferredSize(new Dimension(width, height));
        thecanvas.setMaximumSize(new Dimension(width, height));
        thecanvas.setMinimumSize(new Dimension(width, height));
    }

Canvas 未分隔示例:这行得通。最常用的加法canvas.

    public TheFrame(){
        theframe = new JFrame(title);
        theframe.setSize(width, height);
        theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theframe.setLocationRelativeTo(null);
        theframe.setVisible(true);

        thecanvas = new Canvas();
        thecanvas.setPreferredSize(new Dimension(width, height));
        thecanvas.setMaximumSize(new Dimension(width, height));
        thecanvas.setMinimumSize(new Dimension(width, height));        

        theframe.add(thecanvas);
    }

解决了。我添加了一个 getter 方法。

public class TheFrame {

    private JFrame theframe;
    private String title = "Test";
    private int width = 1024;
    private int height = 576;

    public TheFrame(){
        theframe = new JFrame(title);
        theframe.setSize(width, height);
        theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theframe.setLocationRelativeTo(null);
        theframe.setVisible(true);

        //call the canvas
        TheCanvas thecanvas = new TheCanvas(width, height);

        //!
        theframe.add(thecanvas.getTheCanvas());
    }    
}
public class TheCanvas {

    private int width;
    private int height;
    private Canvas thecanvas;
    public TheCanvas(int width, int height){
        this.width = width;
        this.height = height;
        thecanvas = new Canvas();
        thecanvas.setPreferredSize(new Dimension(width, height));
        thecanvas.setMaximumSize(new Dimension(width, height));
        thecanvas.setMinimumSize(new Dimension(width, height));
    }

    public Canvas getTheCanvas(){
        return thecanvas;
    }
}