java - 绘制方法执行多次
问题描述
为什么在我运行我的代码时这个绘图函数会执行很多次?
我试图只运行一次这段代码,但它执行了很多次,我不知道为什么会这样!
public class DrawFrame extends JFrame {
@Override
public void paint(Graphics g) {
System.out.println("hello Frame");
}
}
public class NJFrame {
public static void main(String[] args) {
DrawFrame NJFrame = new DrawFrame();
NJFrame.setSize(1000, 1000);
NJFrame.setVisible(true);
NJFrame.setLocation(400, 150);
NJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
解决方案
好吧,您的代码多次操纵 JFrame:
DrawFrame NJFrame = new DrawFrame(); // (1) Create the frame
NJFrame.setSize(1000, 1000); // (2) Resize the frame
NJFrame.setVisible(true); // (3) Show the frame
NJFrame.setLocation(400, 150); // (4) Move the frame
似乎这些操作中的每一个都会触发一个绘制事件,您的paint
方法会处理该事件。
推荐阅读
- mysql - 如何使用grails将离线mysql数据更新为应用程序中的在线mysql数据
- javascript - Javascript 防止在返回 false 的情况下重新加载
- javascript - 使用 D3 创建没有重叠的网络图的正确方法
- typescript - 如何根据条件过滤联合类型
- android - 浏览器上方和下方的空间
- go - 将“go”包上传到 AWS Lambda 后无法读取 json 文件
- javascript - angular7 中的第二个选择 url 中没有更改表单值
- php - 需要将来自 WooCommerce Webhook 的数据转发到同一站点 Wordpress REST API 自定义端点
- java - 静态嵌套接口的内联实现
- python - 如何使用硒单击只有类型和类的按钮