ImageIcon没有出现在JPanel中

时间:2014-04-30 19:02:54

标签: java swing jframe jpanel imageicon

我只想尝试加载9个相同的.gif的共享。这就是我想做的一切。请帮忙!

对于tic-tac-toe的修改版本的项目,由于某种原因,我无法将图像显示在我的窗口中。救命啊!

package jerryTacToe;
import javax.swing.*;

import java.awt.*; 
import java.awt.event.*;

import java.io.IOException;

class picture extends JPanel
{
  /**
 * 
 */
private static final long serialVersionUID = 1L;
ImageIcon doge;

  public picture() throws IOException
  {
    doge = new ImageIcon("dogeRotates.gif", null);
    JPanel panel = new JPanel();
    JLabel label = new JLabel("", doge, JLabel.CENTER);
    panel.add(label, BorderLayout.CENTER);
}



public void paintComponent(Graphics g) 
{
    ((Icon) g).paintIcon(this, g, 10, 10);
/*        Graphics2D g2 = (Graphics2D) g;
    Line2D line = new Line2D.Float(100, 100, 250, 260);
    g2.draw(line);*/
}

public void changeDoge(boolean human) throws IOException
{
    if(human)
        doge = new ImageIcon("./dogeIntensifies.gif", null);
    else
        doge = new ImageIcon("./ferociousDoge.gif", null);
}
}

class Closer extends WindowAdapter
{
  public void windowClosing(WindowEvent e)
  {
System.out.println("much derp");
System.exit(0);
  }
}

public class Gui extends JFrame
  implements ActionListener, MouseListener
 {
/**
 * 
 */
private static final long serialVersionUID = 1L;

boolean human;

  JButton goSecond;
  picture centerCenter; picture topLeft; picture topCenter; 
  picture topRight; picture centerLeft; picture centerRight; 
  picture bottomLeft; picture bottomCenter; picture bottomRight;

  public void mouseClicked(MouseEvent e){
  if(e.getSource()==topLeft || e.getSource()==topCenter      
  ||e.getSource()==topRight ||
            e.getSource()==centerLeft ||e.getSource()==centerCenter   
  ||e.getSource()==centerRight || 
            e.getSource()==bottomLeft ||e.getSource()==bottomCenter 
   ||e.getSource()==bottomRight)
  {
      /*
       * Call the AI here. 
       * 
       * 
       * hello?
       * yes, this is doge. 
       * */
  }
  }
 public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
   public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
     public void actionPerformed(ActionEvent e)
   {

  }

   public Gui() throws IOException
   {
addWindowListener( new Closer() );
setTitle("DOGE-TAC-TOE (very0.0.4)");
setVisible(true);
setSize(1600,900);

goSecond=new JButton("wow second");
topLeft=new picture();
topCenter=new picture();
topRight=new picture();
centerLeft=new picture();
centerCenter=new picture();
centerRight=new picture();
bottomLeft=new picture();
bottomCenter=new picture();
bottomRight=new picture();



centerCenter.addMouseListener(this);
topLeft.addMouseListener(this);
topCenter.addMouseListener(this);
topRight.addMouseListener(this);
centerLeft.addMouseListener(this);
centerRight.addMouseListener(this);
bottomRight.addMouseListener(this);
bottomCenter.addMouseListener(this);
bottomLeft.addMouseListener(this);
goSecond.addActionListener(this);

Container stuff=getContentPane();

stuff.setLayout( new BoxLayout(stuff, BoxLayout.PAGE_AXIS) );

JPanel panelTop=new JPanel();
panelTop.setLayout(new GridLayout(1, 3, 300, 100));
panelTop.add(topLeft);
panelTop.add(topCenter);
panelTop.add(topRight);

JPanel panelCenter=new JPanel();
panelCenter.setLayout(new GridLayout(1, 3, 150, 100));
panelCenter.add(centerLeft);
panelCenter.add(centerCenter);
panelCenter.add(centerRight);

JPanel panelBottom=new JPanel();
panelBottom.setLayout(new GridLayout(1, 3, 300, 100));
panelBottom.add(bottomLeft);
panelBottom.add(bottomCenter);
panelBottom.add(bottomRight);

stuff.add(goSecond);
stuff.add(panelTop);
stuff.add(panelCenter);
stuff.add(panelBottom);
  }

  public static void main(String [] args)throws IOException
  {
    JFrame it=new Gui();
   }

}

1 个答案:

答案 0 :(得分:1)

您正以不同方式访问图像,如下所示:

doge = new ImageIcon("dogeRotates.gif", null);

doge = new ImageIcon("./dogeIntensifies.gif", null);

请再次检查图片展示位置。

如果您在访问图片时遇到问题,请查看我的帖子

How to retrieve image from project folder?


为什么不在GridLayout游戏中使用tic-tac-toe

在下面找到示例代码

enter image description here