标题:java初学者请教在JFrame中显示JPanel对象问题,为什么只能显示BUTTON,不能显 ...
只看楼主
LI1152520146
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2011-10-6
 问题点数:0 回复次数:3 
java初学者请教在JFrame中显示JPanel对象问题,为什么只能显示BUTTON,不能显示圆
代码如下:
ChangeBallColorTest.java代码:
import java.awt.Color;
import javax.swing.*;

public class ChangeBallColorTest {
    public static void main(String[] args) {
        ChangeBallColorFrame cbcf = new ChangeBallColorFrame();
        cbcf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cbcf.setLocation(300,200);
        cbcf.setTitle("ChangeBallColor");
        cbcf.setVisible(true);
    }
}

ChangeBallColorFrame.java代码:
import java.awt.*;
import javax.swing.*;
class ChangeBallColorFrame extends JFrame {
        public ChangeBallColorFrame() {
            JButton cbcb1 = new JButton("red");
            JButton cbcb2 = new JButton("green");
            ChangeBallColorPanel cbcp = new ChangeBallColorPanel();
            setLayout(null);
            setSize(300,400);
            setBackground(Color.white);
            cbcb1.setBackground(Color.red);
            cbcb1.setBounds(160, 20, 75, 30);
            add(cbcb1);
            cbcb2.setBackground(Color.green);
            cbcb2.setBounds(70, 20, 75, 30);
            add(cbcb2);
            this.getContentPane().add(cbcp);
            
        }
    }

ChangeBallColorPanel.java代码:
import java.awt.*;
import javax.swing.*;
class ChangeBallColorPanel extends JPanel{
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        Graphics g1 = (Graphics) g;
        Color c = g1.getColor();
        g1.setColor(Color.red);
        g1.fillOval(120, 150, 20, 20);
        g1.setColor(c);
    }
}
搜索更多相关主题的帖子: java public import class 
2011-10-06 18:46
LI1152520146
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2011-10-6
得分:0 
各位大侠给点回复啊!新手分比较少,请见谅!
2011-10-07 21:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-351703-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.196104 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved