我只是想加载9个相同的.gif的总督。这就是我想要做的。请帮忙!

这是针对井字游戏的修改版本的项目,由于某种原因,我无法使图像显示在窗口中。帮帮我!

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();
   }

}

最佳答案

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

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

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


请再次检查图像位置。

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

How to retrieve image from project folder?



为什么在这个GridLayout游戏中不简单使用tic-tac-toe

在下面找到示例代码


Java TicTacToe game winCondition?





Tic Tac Toe java

10-06 07:28