use*_*475 1 java eclipse macos swing paintcomponent
我试图在Java中使用swing绘制一个令人难以置信的基本形状,但由于某种原因它似乎没有工作.这是我从讲师那里下载的代码,他在演讲中向我们展示了这些代码,但是当我运行它时窗口打开但没有画出来,我不明白为什么.
package graphicsEx;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Lecture1Example extends JPanel{
// This is where the JPanel gets (re-)painted when the screen is refreshed.
public void paintComponent(Graphics g) {
// Cast to Graphics2D for more features.
Graphics2D g2D = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(20,30,40,50);
g2D.setColor(Color.red);
g2D.draw(rect);
g2D.fill(rect);
}
public static void main(String args[]) {
JFrame frame = new JFrame("Playing with Graphics");
frame.setSize(500, 400);
frame.setVisible(true);
frame.setContentPane(new Lecture1Example());
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Eclipse IDE.
尊敬的用户1821475的讲师:
应该仅在事件派发线程上构造和操作Swing GUI对象.
"具有UI委托(相对于直接子类JComponent)的Swing组件的子类应super.paintComponent()在其paintComponent覆盖内调用.
"作为一种便利add和它的变种,remove并setLayout已被覆盖,以便在必要时前进contentPane."
最外层Container应setVisible()仅在调用pack()和影响几何的其他方法之后.
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |