如何设置 class 对其他人可见?

How to set a class visible from another?

我有一个画板,上面有一些圆形、三角形和矩形。我希望能够将它们设置为可见和不可见。

类的名字分别是CircleTriangleRectangle并在延伸JComponent,它们都具有独特的颜色。

有人建议 Triangle c = null; 然后使用 c.setVisible(true) 使其可见 - 但它只给出 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 引用 c.setVisible(true)

有人知道解决这个问题的办法吗?

请使用:

Triangle c = new Triangle();

c.setVisible(true);

而不是 null

具有 null 值的引用变量在您使用点 (.) 运算符调用它时将始终为您提供 NullPointerException。