public class T {
public static void main(String[]args){
JFrame t = new JFrame();
t.setVisible(true);
t.setSize(800,600);
Graphics g = t.getGraphics();
for(int i = 1 ; i < 100 ; i++){
g.drawLine(0, 0, 50, 50);
g.drawString("Hello word", 200, 200);
                }
}
}
没循环 不会绘画 我想应该是 线程的问题吧 如何 同步呀? 求解 
我仅仅是个新手 求白话 或者 非复杂代码 ```

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;/**
     * Created by IntelliJ IDEA.
     * User: gaoyong
     * Date: 2011-10-14
     * Time: 20:20:45
     * To change this template use File | Settings | File Templates.
     */
    public class T {
        public static void main(String[]args){
            JFrame t = new JFrame();
            JPanel panel=new MyPanel();
            t.setContentPane(panel);
            t.setVisible(true);
            t.setSize(800,600);    }
    }
    class MyPanel extends JPanel{
        @Override
        public void paintComponent(Graphics g){
    //         Graphics g = t.getGraphics();
            super.paintComponent(g);
            g.drawLine(0, 0, 50, 50);
            g.drawString("Hello word", 200, 200);    }
    }
      

  2.   

    以前 我一直 这么用了 但是 假如 我现在 想作一个 屏幕管理器 JFrame 或者 Window 对象 不是受我控制的 我仅仅能得到引用 并且不能添加任何东西 囧 麻烦楼上 费心 在告诉下弟 下 O(∩_∩)O谢谢啦  刚才没描述明白 o(╯□╰)o
      

  3.   

    O(∩_∩)O哈哈~   解决了  EventQueue.invokeAndWait(new Runnable(){}); \(^o^)/~
      

  4.   

    嗯,是这样处理,贴下代码
    import javax.swing.*;
    import java.awt.*;public class T {
        public static void main(String[]args)throws Exception{
            final JFrame t = new JFrame();
            t.setVisible(true);
            t.setSize(800,600);        EventQueue.invokeAndWait(new Runnable(){
                public void run(){
                    Graphics g = t.getGraphics();
                    g.drawLine(0, 0, 50, 50);
                    g.drawString("Hello word", 200, 200);
                }
            });
            t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }