import java.awt.event.*;
import javax.swing.*;
import java.awt.*;public class CursorTest
    extends JFrame {
   ChessBoard p;  private JButton b = new JButton("Void");
  public CursorTest() 
  {
    super("cursorTest");
    this.setSize(300, 200);
    p=new ChessBoard();
    Container cp = getContentPane();
    cp.add(BorderLayout.CENTER, p);
    cp.add(BorderLayout.SOUTH, b);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
  public static void main(String args[]) 
  {
    CursorTest test = new CursorTest();
  }}
 class ChessBoard extends JPanel
{
  }

解决方案 »

  1.   

    比如说JPanel里加一张图片,使得JPanel里的其他组件更美观.我的要求是在不能改变JPanel的前提.不能在JPanel里面加一个组件,在组件上显示图片,只能在JPanel下加.谢谢!!!
      

  2.   

    import java.awt.event.*;                                
    import javax.swing.*;                                   
    import java.awt.*;                                      
                                                            
    public class CursorTest  extends JFrame {                                    
       ChessBoard p;                                        
                                                            
      private JButton b = new JButton("Void");              
      public CursorTest()                                   
      {                                                     
        super("cursorTest");                                
        this.setSize(300, 200);                             
        p=new ChessBoard();                                 
        Container cp = getContentPane();                    
        cp.add(BorderLayout.CENTER, p);                     
        cp.add(BorderLayout.SOUTH, b);                      
        this.setVisible(true);                              
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }                                                    
      public static void main(String args[])                
      {                                                     
        CursorTest test = new CursorTest();                 
      }                                                     
                                                            
    }                                                       
     class ChessBoard extends JPanel                        
    {       
    public ChessBoard()
    {
    super();
    setBackground(Color.RED);  // 背景为红色
    }
                                                 
     }
      

  3.   

    overwrite这个方法
    public void paintComponent(Graphics g) {
                      String url="你放置图片的地址";
                      image = (new ImageIcon(url)).getImage();
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    if (image != null) {
    g2d.drawImage(image, 0, 0, this.WIDTH,this.HEIGHT,this);
    System.out.println("this.WIDTH: "+ this.RIGHT_ALIGNMENT);
    System.out.println("this.HEIGHT: "+ this.BOTTOM_ALIGNMENT);
    }
    }