制作这个画笔程序我决定我要清除面板容器(panel2在链接代码中)。我用过PanelName.setForeground(null),效果很好。我想知道的是这种方法和其他从容器中删除图形对象的方法是否存在已知的缺点。
2 回答
如果你覆盖paintComponent,你可以清除它的Graphicsusing Graphics#clearRect。但总的来说,我更喜欢你的方法,因为我的方法需要你要么 subclass JPanel,要么匿名声明一个。
The converse of @mre's answer is that "It is up to the look and feel to honor this property, some may choose to ignore it." I don't know of a PanelUI that ignores the foreground color, but it's something to be aware of when testing.
Addendum: As @kleopatra comments, the JComponent@setForeground() API links to the Component#getForeground() API, which notes the following: "if this component does not have a foreground color, the foreground color of its parent is returned."