
例如:textfield
txt=new
textfield("请输入姓名")
txt.setforeground(color.red)
本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics g)
这个方法,所以setBackground这个方法即使你用了也看不到很大的效果。但是有一种取代的方法就是在paintComponent(Graphics g)方法中首先就用Graphics 所含有的方法g.setColor(Color.black)来设置背景颜色再用g.fillRect(0, 0, this.getWidth(), this.getHeight())来填满整个容器,这就达到了设置背景目的。然后你再g.setColor(其他颜色)来绘制其它图形.
具体代码:(在你以上的代码上修改了点)
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g
g.setColor(Color.black)//这里设置背景颜色
g.fillRect(0, 0, this.getWidth(), this.getHeight())//这里填充背景颜色
double x=100
double y=100
double w=200
double h=150
Rectangle2D rect=new Rectangle2D.Double(x,y,w,h)
g2.setPaint(Color.white)//这里是你设置其他笔触颜色
g2.draw(rect)
Ellipse2D ellipse=new Ellipse2D.Double()
ellipse.setFrame(rect)
g2.draw(ellipse)
Point2D p1=new Point2D.Double(x-40,y-30)
Point2D p2=new Point2D.Double(x+w+40,y+h+30)
g2.draw(new Line2D.Double(p1,p2))
double centerx=rect.getCenterX()
double centery=rect.getCenterY()
double radius=150
Ellipse2D circle=new Ellipse2D.Double()
circle.setFrameFromCenter(centerx,centery,centerx+125,centery+125)
g2.draw(circle)
}
测试结果图
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)