本程序很简单 就是鼠标点击JFrame后 显示点击的“点”
    小弟就是有个问题 MyFrame的背景色怎么设置?
Container con=this.getContentPane();
this.setBackground(Color.BLACK);似乎不管用
             求教 swing中paint的  设置背景色问题
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;public class MyMouseAdapter {
public static void main(String[] args) {
new MyFrame("paint point");
}}class MyFrame extends JFrame {
private List<Point> points=null; MyFrame(String s) {
super(s);
points = new ArrayList<Point>();
this.setBounds(200, 300, 200, 200);
this.addMouseListener(new MyMonitor());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}@Override
public void paint(Graphics g) {
for (Point p : points) {
g.setColor(Color.RED);
g.fillOval(p.x, p.y, 10, 10);
}
} public void addPoint(Point p) {
points.add(p);
}
}class MyMonitor extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
MyFrame my = (MyFrame) e.getSource();
my.addPoint(new Point(e.getX(), e.getY()));
my.repaint();
}}

解决方案 »

  1.   

    楼主你把这句放到构造函数里就可以了。下面的程序就可以了
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.util.List;public class MyMouseAdapter {
    public static void main(String[] args) {
    new MyFrame("paint point");
    }
    }class MyFrame extends JFrame {
    private List<Point> points = null; MyFrame(String s) {
    super(s);
    points = new ArrayList<Point>();
    this.setBounds(200, 300, 200, 200);
    this.addMouseListener(new MyMonitor());
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBackground(Color.BLACK);
    } @Override
    public void paint(Graphics g) {
    for (Point p : points) {
    g.setColor(Color.RED);
    g.fillOval(p.x, p.y, 10, 10);
    }
    } public void addPoint(Point p) {
    points.add(p);
    }
    }class MyMonitor extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent e) {
    MyFrame my = (MyFrame) e.getSource();
    my.addPoint(new Point(e.getX(), e.getY()));
    my.repaint();
    }
    }
      

  2.   

    import javax.swing.JFrame;
    import java.awt.*;public class textMultiFrame {
    static class MyFrame extends JFrame{
    MyFrame(){}
    MyFrame(String title,int x,int y,int width,int heigth,Color color){
    super(title);
    //Container con= this.getContentPane();
    //con.setBackground(color);
                            this.setBackground(color);
    this.setBounds(x, y, width, heigth);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    public static void main(String[] args) {
    new textMultiFrame.MyFrame("frame1",100,100,200,200,new Color(0,0,102));
    new textMultiFrame.MyFrame("frame2",300,100,200,200,Color.BLUE);
    new textMultiFrame.MyFrame("frame3",100,300,200,200,Color.CYAN);
    new textMultiFrame.MyFrame("frame4",300,300,200,200,Color.YELLOW);
    }}那为什么在这种情况下this.setBackground(color); 不起作用呢?
      

  3.   

    你这个应该用SUPER
    你这个就是this和Super之间的关系
    应该在原始的那个类里面的方法 我觉得才可以啊
      

  4.   

    你掉的代码是对的噢,下面的可以出来颜色了。
    因为JFrame一旦创建,其中已包含一个内容面板。
    一般在JFrame中添加组件时,都加在内容面板,这面板可以通过JFrame的成员方法getContentPane()取,。
    所以如果设置JFrame的背景颜色,仍会被内容面板盖住,不如设置内容面板的背景颜色当时如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况
    你可以试验下,尝试frame.getContentPane().setVisible(false)。
    将面板隐藏,那么就可以设置JFrame的背景颜色也就显示出来了。 
    import javax.swing.JFrame;
    import java.awt.*;public class textMultiFrame {
        static class MyFrame extends JFrame{
            //MyFrame(){}
            MyFrame(String title,int x,int y,int width,int heigth, Color color){
                super(title);
                Container con= this.getContentPane();
                con.setBackground(color);
             //this.setBackground(Color.BLACK);
                this.setBounds(x, y, width, heigth);
                this.setVisible(true);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        }
        public static void main(String[] args) {        new textMultiFrame.MyFrame("frame1",100,100,200,200,new Color(0,0,102));
            new textMultiFrame.MyFrame("frame2",300,100,200,200,Color.BLUE);
            new textMultiFrame.MyFrame("frame3",100,300,200,200,Color.CYAN);
            new textMultiFrame.MyFrame("frame4",300,300,200,200,Color.YELLOW);
        }}
      

  5.   

    [Quote=引用 5 楼 littlemonster 的回复:]
    你掉的代码是对的噢,下面的可以出来颜色了。
    因为JFrame一旦创建,其中已包含一个内容面板。
    一般在JFrame中添加组件时,都加在内容面板,这面板可以通过JFrame的成员方法getContentPane()取,。
    所以如果设置JFrame的背景颜色,仍会被内容面板盖住,不如设置内容面板的背景颜色当时如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况
    你可以试验下,尝试frame.getContentPane().setVisible(false)。
    将面板隐藏,那么就可以设置JFrame的背景颜色也就显示出来了。
    我知道我注释掉的代码是正确的
      我只是疑惑 什么情况要
    Container con= this.getContentPane(); 
                con.setBackground(color); 什么情况要this.setBackground(color); 你说“因为JFrame一旦创建,其中已包含一个内容面板。"  但是我一楼的程序为什么那个内容面板没有影响呢?
      

  6.   

    container是顶级容器 
    如果要向顶层容器(比如JFrame)添加其他组件或容器(如JPanel),就需要调用这个方法。 
    一般先创立一个容器类的实例,然后调用此方法 
    Container contentPane=getContentPane() 
    contentPane.add(组件名) 
      

  7.   

    container是顶级容器 
    如果要向顶层容器(比如JFrame)添加其他组件或容器(如JPanel),就需要调用这个方法。 
    一般先创立一个容器类的实例,然后调用此方法 
    Container contentPane=getContentPane() 
    contentPane.add(组件名) 你掉的代码是对的噢,下面的可以出来颜色了。 
    因为JFrame一旦创建,其中已包含一个内容面板。 
    一般在JFrame中添加组件时,都加在内容面板,这面板可以通过JFrame的成员方法getContentPane()取,。 
    所以如果设置JFrame的背景颜色,仍会被内容面板盖住,不如设置内容面板的背景颜色当时如果框架中还加有其他面板,内容面板的颜色也会被其他面板盖住,要注意一下面板的布局情况 
    你可以试验下,尝试frame.getContentPane().setVisible(false)。 
    将面板隐藏,那么就可以设置JFrame的背景颜色也就显示出来了。 
    5  。7  楼说的很明白了
      

  8.   

    按你的说话   我二楼的程序 没有在顶级的JFrame中添加任何组件啊?  为什么那种情况下要使用
    Container con= this.getContentPane(); 
                con.setBackground(color);
      

  9.   

    在JFrame好像不能直接画图吧??用paint()不能实现绘画出自身吧
    最好加个  组件
      

  10.   

        http://bbs.tsp2c.cn/?fromuid=136