如何使用Java和Jframe类绘制矩形?
我现在要学习Java(C ++之后),我正在尝试使用GUI编程。
我在此代码中的目标是将一些简单的东西绘制到使用Jframe库创建的窗口中。
这是我的代码:
import gui.Gui;
public class Main
{
public static void main (String[] args)
{
Gui g = new Gui();
g.paint(null);
}
}
package gui;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.Color;
public class Gui extends JFrame
{
public Gui()
{
JFrame jf = new JFrame("Beispiel JFrame");
jf.setTitle("Draw Something");
jf.setSize(960, 960);
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("./img.png");
jf.setIconImage(img.getImage());
}
public void paint(Graphics g)
{
g.drawRect(480, 480, 200, 100);
}
}
它没有错误消息编译,但什么也没绘制。 我找不到这样做的原因,谁能帮我:)?
(并且,如果格式不是“完美”的,我是堆叠的新手。)
Im about to learn Java (after C++) now,and I am trying myself in Gui Programming.
My goal in this code was to draw something simple into a window created with the JFrame library.
This is my code:
import gui.Gui;
public class Main
{
public static void main (String[] args)
{
Gui g = new Gui();
g.paint(null);
}
}
package gui;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Graphics;
import java.awt.Color;
public class Gui extends JFrame
{
public Gui()
{
JFrame jf = new JFrame("Beispiel JFrame");
jf.setTitle("Draw Something");
jf.setSize(960, 960);
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon("./img.png");
jf.setIconImage(img.getImage());
}
public void paint(Graphics g)
{
g.drawRect(480, 480, 200, 100);
}
}
it compiles without an error message, but it draws nothing.
I am unable to find the reason for that, could anyone help me :) ?
(And sry if the formatting is not "perfect", I am new to StackOverflow.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常建议您不要从顶级容器延伸,例如
jframe
。您不会在课程中添加任何新功能,而是将自己锁定到一个用例中。jframe
也是一个复合组件,也就是说,它包含许多构成其核心功能的儿童组件,并且由于绘画系统的工作方式,因此儿童组件有可能进行绘画与父容器/组件无关,在您的情况下,这将产生很多问题。我建议您首先阅读在AWT和AWT和绘画中摇摆更好地了解油漆系统的工作原理和执行自定义绘画更好地了解如何使用它。
您可能还想阅读 2D Graphics trail 。
另外,作为一般的经验法则,除非您愿意承担所覆盖方法的全部责任,否则您应该调用其
super
实现。在这种情况下,绘画是复杂的工作流程,因此您基本上已经完全规避了它,这将无法很好地结束。回到您的问题,而不是使用
jframe
和覆盖paint
(通常是不符合要求的),您应该从jpanel
和OverridePaintComponent
而不是... /I.sstatic.net/cygjw.png“ alt =”在此处输入图像说明”>
It's generally recommend that you NOT extend from a top level container like
JFrame
. You're not adding any new functionality to the class and you're locking yourself into a single use case.JFrame
is also a compound component, that is, it contains a number of child components which make up its core functionality, and, because of the way the painting system works, it's possible for a child component to painted independently of the parent container/component, which, in your case, is going to produce a lot of issues.I would recommend you start by reading through Painting in AWT and Swing to get a better understanding of how the paint system works and Performing Custom Painting to better understand how you should work with it.
You probably also want to read through the 2D Graphics trail.
Also, as a general rule of thumb, unless you're willing to take over the full responsibility of the method you're overriding, you should be calling its
super
implementation. In this case, painting is complex workflow, so you've basically completely circumvented it, which is not going to end well.Back to your problem, instead of using
JFrame
and overridingpaint
(which is generally un-recommended), you should start with something likeJPanel
and overridepaintComponent
instead, for example...