[英]What is the simplest way to display a graphics drawing in Java AWT?
I want to use a parent class, preferably Component (being the highest up AWT gui class I'm aware of).我想使用父类,最好是 Component (是我所知道的最高的 AWT gui 类)。 I'm not a newbie to programming but I am self taught and am having problems getting example code for my question.我不是编程的新手,但我是自学的,并且在为我的问题获取示例代码时遇到问题。 Here's my code:这是我的代码:
import java.awt.Frame;
public class DrawSquare extends Frame{
public DrawSquare(){
setBounds(0,0,100,100);
setVisible(true);
}
public static void main(String[]args){
new DrawSquare();
}
}
Basically I want to do the same thing with a parent class to 'possibly' have quicker and more efficient code.基本上,我想对父类做同样的事情,以“可能”拥有更快、更高效的代码。 I'm assuming that Frame has more in built options such as a border while Window (a parent class) does not, but I have only been able to make this simple code with Frame.我假设 Frame 具有更多内置选项,例如边框,而 Window(父类)没有,但我只能使用 Frame 制作这个简单的代码。
I want to use AWT, not swing btw.我想使用 AWT,而不是摆动 btw。
According to my tests:根据我的测试:
class C extends Component {
public C() { setSize(200, 200); setVisible(true); }
public void paint(Graphics g) {
g.drawRect(0, 0, 100, 100);
}
}
will not be displayed because presumably it needs something to be added to.不会显示,因为大概需要添加一些东西。
class C extends Container {
public C() { setSize(200, 200); setVisible(true); }
public void paint(Graphics g) {
g.drawRect(0, 0, 100, 100);
}
}
will not be displayed because it is a container so it needs other things to be added to it.不会显示,因为它是一个容器,所以需要添加其他东西。
class C2 extends Window {
public C2() { super(new Frame()); setLocation(100, 100); setSize(200, 200); setVisible(true); }
public void paint(Graphics g) {
g.drawRect(0, 0, 100, 100);
}
}
can be displayed but has nothing to grab around and it even doesn't show on the task bar so its a useless thing unless you add it somewhere.可以显示但没有什么可抓取的,它甚至不会显示在任务栏上,因此除非您将其添加到某处,否则它是无用的东西。
So所以
class C extends Frame {
public C() { setSize(200, 200); setVisible(true); }
public void paint(Graphics g) {
g.drawRect(0, 0, 100, 100);
}
}
is your solution as the first that is both a Component and a Container.作为第一个既是组件又是容器的解决方案是您的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.