gpt4 book ai didi

java - 为什么我的 JFrame 无法绘制?

转载 作者:行者123 更新时间:2023-12-01 09:17:04 25 4
gpt4 key购买 nike

我正在尝试使用 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

}

}

最佳答案

尝试从 JFrame 类而不是 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);
}

关于java - 为什么我的 JFrame 无法绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com