论坛首页 Java企业应用论坛

在JPanel画字符串的问题

浏览 3681 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-11-21  
我的代码是这样的:
public class MainFrame extends JFrame {
    private Percent percent;
    public MainFrame() {
        super("Color Chooser");
        container = getContentPane();
        container.setLayout(new FlowLayout());
        percent = new Percent();
        percent.setBackground(Color.BLUE);
        container.add(percent);
        setSize(400,130);
        setVisible(true);		
    }
    public static void main(String args[]) {
        ColorChooser myColorChooser = new ColorChooser();
        myColorChooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
private class Percent extends JPanel {
	private float redPercent = 100;

	private float greenPercent;

	private float bluePercent;

	public void paintComponenet(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.BLACK);
		g.setFont(new Font("Serif",Font.BOLD,12));
		g.drawString("RED:" + redPercent + "%", 10, 20);
		g.drawString("GREEN:" + greenPercent + "%", 10, 100);
		g.drawString("BLUE:" + bluePercent + "%", 10, 180);
	}

	public void setPercent(int r, int g, int b) {
		int total = r + g + b;
		redPercent = 100 * (float) r / (float) total;
		greenPercent = 100 * (float) r / (float) total;
		bluePercent = 100 - redPercent - greenPercent;
		repaint();
	}

	public Dimension getPreferredSize() {
		return new Dimension(200, 200);
	}

	// used by layout manager to determine minimum size
	public Dimension getMinimumSize() {
		return getPreferredSize();
	}
}

我不会贴图,所以无法把运行结果贴出来了。口头描述一下结果:加入的Percent这个JPanel上面没有画上字符串,不知道是为什么?
能不能帮我看看代码是不是有问题?Thanks!!!
   发表时间:2006-11-21  
paintComponenet 
paintComponent

the same ?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics