为什么我的JFrame不会画画?

时间:2016-11-07 15:38:33

标签: java jframe

我正在尝试使用Graphics绘制到我的JFrame。出于某种原因,它不会画任何东西。我究竟做错了什么?我添加了我的绘画方法,导入了所有内容。我把方法放在错误的地方吗?

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

public class Main extends Canvas implements MouseListener, KeyListener {

    public int WIDTH = 1080;
    public static Main main;
    public boolean playing = false;

    public Main() {
        addMouseListener(this);
        addKeyListener(this);

        JFrame frame = new JFrame("Clicker");
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(50, 50, 100, 100);
    }

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

    public void MainScreen() {

    }

    @
    Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void mouseClicked(MouseEvent evt) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void mouseEntered(MouseEvent evt) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void mouseExited(MouseEvent evt) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void mousePressed(MouseEvent evt) {
        // TODO Auto-generated method stub

    }

    @
    Override
    public void mouseReleased(MouseEvent evt) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:1)

尝试从Canvas类而不是public class Main extends JFrame implements ... 类扩展:

public Main() {
    super("Clicker");

    addMouseListener(this);
    addKeyListener(this);

    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);
}

您还必须编辑构造函数,然后像:

let phoneNumber: String = "1234567890"
let phoneNumberString: String = "sms:\(phoneNumber)"
let url: NSURL = NSURL(string: phoneNumberString)!
if UIApplication.sharedApplication().canOpenURL(url) {
    UIApplication.sharedApplication().openURL(url)
}