我是菜鸟 刚开始学。
import javax.swing.*;
import java.awt.*;
public class HelloEclipse {


public static void main(String[] args) {
Myframe M1=new Myframe(100,200,Color.blue);
Myframe M2=new Myframe(200,200,Color.BLACK);
Myframe M3=new Myframe(300,200,Color.CYAN);

}}
 class Myframe extends JFrame{
Myframe(int x,int y,Color color){
setBackground(color);
setLayout(new BorderLayout());
setLocation(400,200);
setSize(x,y);

setVisible(true);
}

}
发现这程序 颜色不管用。没有效果。为什么呢?怎么改呢? 

解决方案 »

  1.   

    JFrame必须放在容器Container中使用,我把你的代码改了一下,加入了容器Container并把窗体放到容器中去,你可以看下效果了。
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;import javax.swing.JFrame;
    public class HelloEclipse 
    {
    public static void main(String[] args) 

    Myframe M1=new Myframe(100,200,Color.blue); 
    Myframe M2=new Myframe(200,200,Color.BLACK); 
    Myframe M3=new Myframe(300,200,Color.CYAN); 
    }

    class Myframe extends JFrame
    {
    Myframe(int x,int y,Color color)
    {
    Container container = this.getContentPane();
    container.setBackground(color); 
    setLayout(new BorderLayout()); 
    setLocation(400,200); 
    setSize(x,y); 
    setVisible(true);