如何将图形显示到jframe?

时间:2016-05-30 15:40:20

标签: java swing graphics

我似乎无法将我的渲染类绘制到Jframe(或JComponent)上。 JFrame出现但不是绿色,并且不向控制台输出“绿色”。

可能缺少一些非常关键的东西

public class Game_1 {

    public static JFrame frame;
    public static Render render;
    public static Game_1 game;

    public Game_1() {
        frame = new JFrame("Game 1");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.add(render = new Render());
    }

    public static void main(String[] args) {
        game = new Game_1();
        render.repaint();
    }
}


public class Render extends JComponent {
    public void render(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 500);

        System.out.println("Green");
    }
}

0 个答案:

没有答案