0

嗨,我有一个 JFrame,上面有两个 JPanel。我的目的是利用 JPanel。任何人都可以分享任何Java代码吗?

4

2 回答 2

5

All the JComponents ( of which JPanel inherits from ) have a paintComponent(Graphics g ) method that you can override.

Basically... oh.. well, I think this would be more appropiate:

http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart1.html

Naive sample:

sample

Source code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    public class X  {
        public static void main( String [] args ) {
             JFrame frame = new JFrame();
             frame.add( new JPanel() {
                 public void paintComponent( Graphics g ) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D)g;

                    Line2D line = new Line2D.Double(10, 10, 40, 40);
                    g2.setColor(Color.blue);
                    g2.setStroke(new BasicStroke(10));
                    g2.draw(line);
                 }
            });
            frame.setVisible( true );
        }
    }
于 2010-09-09T01:02:17.243 回答
2

查看Java 教程页面。从 2D 图形教程开始。

于 2010-09-09T01:36:56.410 回答