在JPanel中的JtextArea上显示图像

时间:2011-08-17 12:06:34

标签: java image swing

不确定为什么这不起作用我使用netbeans来创建它我在jcreator中有类似的代码并且它有效。任何帮助非常感谢

    public void BoardGUI(){
         panel = new JPanel();
         setIconImage(new ImageIcon("images/die.bmp").getImage());

         Container cPane;
         cPane = getContentPane();
         cPane.setBackground(Color.red);

         setTitle("ITT Game Of Life");
         setDefaultCloseOperation(EXIT_ON_CLOSE);

         panel.setOpaque(false);                         //the frame
         setSize(800,650);
         setLocation(500,100);
         boardArea = new JTextArea(50,100)  {

         ImageIcon image = new ImageIcon( "/images/Board.jpg" );

     @Override
     public void paint( Graphics g ) {
        g.drawImage( image.getImage(), 0, 0, this);
        super.paint(g);
     }
   };

   boardArea.setOpaque(false);
   boardArea.setEditable(true);
   boardArea.setBounds(100,50,200,200);
   cPane.add(boardArea);

2 个答案:

答案 0 :(得分:1)

setIconImage(new ImageIcon("images/die.bmp").getImage()); 

ImageIcon image = new ImageIcon( "/images/Board.jpg" ); 

您使用了两种不同的方法来指定图像的位置。我猜第一个是正确的。你的框架图标是否改变了?

此外,How to Use Icons教程通过图像作为资源提供了更好的示例。

答案 1 :(得分:0)

尝试更改netbeans的图像文件夹的位置。我过去常常在学生生活中使用netbeans。我记得的事情,可能是图像相对路径的问题。

这可能是不与netbeans合作的原因。