为什么我的这段代码不显示  ----圆   ????
import java.awt.*;
import javax.swing.*;public class Test {    public static void main (String[] args) {
     JFrame win;
Container contentpane;
Graphics g;
     win = new JFrame("TimeKeeper");
     win.setSize(500,500);
     win.setLocation(100,100);
     win.setVisible(true);
    
     contentpane = win.getContentPane();
     g = contentpane.getGraphics();
     g.setColor(Color.BLACK);
     g.drawLine(50,50,100,100);
    }
    
    
}

解决方案 »

  1.   


    import java.awt.*;
    import javax.swing.*;public class Test
    {
    public static void main (String[] args)

    JFrame win;
    Container contentpane;
    MyPanel panel = new MyPanel();
    win = new JFrame("TimeKeeper"); 
    win.setSize(500,500); 
    win.setLocation(100,100); 
    contentpane = win.getContentPane(); 
    contentpane.add(panel);
    win.setVisible(true); 
    }
    }class MyPanel extends JPanel
    {
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.setColor(Color.BLACK); 
    g.drawLine(50,50,100,100);
    }
    }
    你原来那样,因为显示会调用到paint方法,所以你画的直线又看不到了