在使用形状(多边形)设置 JLabel 的 Clip 之前,getClip() 仍然返回 null



在我的程序中,我试图设置JLabel的Clip,如下所示:

JLabel  jl = new JLabel();
jl.setVisible(true);
jl.setSize( 50, 50);
componentWhereIPutEverything.add( jl);
jl.setLocation( 50 , 50);
int xx[] = { (int) UpLeft.getX(), (int) UpRigth.getX(), (int) BottomRigth.getX(), (int) BottomLeft.getX()   };
int yy[] = { (int) UpLeft.getY(), (int) UpRigth.getY(), (int) BottomRigth.getY(), (int)  BottomLeft.getY()  };
Polygon poly = new Polygon(xx, yy, xx.length);
jlabel.getGraphics().setClip( poly );

之后,我尝试:

jl.getGraphics().getClip()

并且返回null。我该怎么修?错误在哪里?谢谢;)

仅仅因为在一个图形对象上设置了剪辑并不意味着它将用于所有图形对象。你最好的解决方案是覆盖getGraphics()并在那里添加你的setClip

public Graphics getGraphics() { Graphics g = super.getGraphics(); g.setClip(...); return g; }

最新更新