gpt4 book ai didi

java - ImageIcon 未显示在 JPanel 中

转载 作者:行者123 更新时间:2023-12-01 13:03:16 24 4
gpt4 key购买 nike

我只是想加载 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?

<小时/>

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

在下面查找示例代码

enter image description here

关于java - ImageIcon 未显示在 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23396048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com