且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用paintComponent()在JFrame中绘制矩形

更新时间:2023-12-05 18:03:52

您的班级 DrawPanel 仅限于 main 方法,对你的构造函数不可见。

Your class DrawPanel is confined to the scope of your main method and is not visible to your constructor.

你需要移动 DrawPanel 退出方法,然后将其添加到 JFrame

You need to move DrawPanel out of your main method, then add it to your JFrame:

frame.add(panel);

此外,***调用 frame.setVisible(true)添加完所有组件后。

Also, better to call frame.setVisible(true) after all components have been added.