Java中swing如何背景上画图 30
需要在JLabel上面画个图,这个图来作为Label的背景,应该如何做呢,要让文字始终在图片的下方,应该怎么做呢publicvoidpaint(Graphicsg){su...
需要在JLabel上面画个图,这个图来作为Label的背景,应该如何做呢,要让文字始终在图片的下方,应该怎么做呢
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.black);
setHorizontalAlignment(JLabel.CENTER);
//解决边缘锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(1.0f));
g2d.drawRoundRect(1, 1, getWidth()-2, getHeight()-2, 6, 6);
//g2d.drawString("确定", 20, 20);
}
这个方法是在前景色绘制 展开
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.black);
setHorizontalAlignment(JLabel.CENTER);
//解决边缘锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(1.0f));
g2d.drawRoundRect(1, 1, getWidth()-2, getHeight()-2, 6, 6);
//g2d.drawString("确定", 20, 20);
}
这个方法是在前景色绘制 展开
3个回答
展开全部
所有的JComponent都有setBackgroundColor(Color c). 但是这个只是单一的颜色.
如果你想要放置自定义的颜色或者图片的话
你可以继承一个JPanel, override他的paintComponent, 加上drawImage()来画自己的图片上去.
class BackGroundPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer)
}
}
JButton btn = new JButton();
BackGroundPanel bkPanel = new BackGroundPanel ();
btn.setOpaque(false);//这句使得button背景变得透明, 漏出bkPanel
bkPanel.add(btn, BorderLayout.Center);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询