gpt4 book ai didi

java - 如何从另一个类将 Canvas 添加到 JFrame?

转载 作者:行者123 更新时间:2023-12-02 00:47:55 25 4
gpt4 key购买 nike

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

我是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;
}
}

关于java - 如何从另一个类将 Canvas 添加到 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57883743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com