swing之jpanel设置背景色和透明度的方法

package com.chcbz;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Graphics;


public class Chart {
public static void main(String[] args){
JFrame jf = new JFrame("Test");
Wall w = new Wall();
Container contentPane = jf.getContentPane();
contentPane.add("Center",w);
jf.setSize(100,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}

class Wall extends JPanel{
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
setBackground(Color.blue);
g2d.setColor(Color.red);
g2d.setStroke(new BasicStroke(4f,0,0));
g2d.drawRect(10, 10, 50, 50);
g2d.drawLine(10, 40, 60, 40);
g2d.drawLine(35, 10, 35, 40);
}
}
我是想将JPanel背景颜色改成黑色,而不是在JFrame设,为什么显示不了背景颜色啊?请各位高手赐教.
更多 0 分享到:
相关主题推荐: string class 2d

对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 回复次数:6

jingtao8177
jingtao8177
等级:
#1 得分:30 回复于: 2007-08-07 19:39:28
改下你那个Wall类

class Wall extends JPanel{
public void paintBorder(Graphics g){
Graphics2D g2d = (Graphics2D)g;
setBackground(Color.blue);
g2d.setColor(Color.red);
g2d.setStroke(new BasicStroke(4f,0,0));
g2d.drawRect(10, 10, 50, 50);
g2d.drawLine(10, 40, 60, 40);
g2d.drawLine(35, 10, 35, 40);
}
}

你可能感兴趣的:(桌面开发-Swing)