为什么没有为此代码显示任何GUI?

时间:2016-08-09 14:12:06

标签: java multithreading user-interface

大家好,非常感谢您抽出宝贵时间帮我解决问题。

出于某种原因,假设打开GUI的这行代码没有运行,我慢慢地从youtube上的教程中复制了确切的代码(链接https://www.youtube.com/watch?v=PrPwCKr6WNI

这是代码。

package game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

public class Main extends JFrame {

    int GWIDTH = 400;
    int GHEIGHT = 300;

    int x, y;

    private Image dbImage;
    private Graphics dbg;

    public Main() {
        setSize(GWIDTH, GHEIGHT);
        setTitle("Game");
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addMouseListener(new Mouse());

        x = 15;
        y = 15;
    }

    public class Mouse extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            int xCoord = e.getX();
            int yCoord = e.getY();

            x = xCoord-7;
            y = yCoord-7;
        }

        @Override
        public void mouseReleased(MouseEvent e){

        }
        public void mouseEntered(MouseEvent e){

        }
        public void mouseExited(MouseEvent e){

        }
    }

    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent(Graphics g){
        g.fillOval(x, y, 15, 15);

        repaint();
    }

    public static void main(String[] args) {
        Main main = new Main();
    }

}

0 个答案:

没有答案